我如何拆分和QUOT; ABCD EFGH"进入" ABCD"和" EFGH"? [英] How do I split "abcd efgh" into "abcd" and "efgh"?

查看:142
本文介绍了我如何拆分和QUOT; ABCD EFGH"进入" ABCD"和" EFGH"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是真正的自我解释。我在bash shell中工作,我真正的新Shell脚本。我发现了很多有关使用 TR SED ,但所有的例子我已经发现迄今卸下分隔符和新行。我真正想要做的是相反的。我希望能够以一个空格分隔。我有一个像ABCD EFGH的字符串,我需要它是ABCD,EFGH(所有不带引号,只是为了显示分组)。

This is really self explanatory. I'm working in a bash shell and I'm really new to shell scripting. I've found a lot of information about using tr and sed but all the examples I have found so far are removing delimiters and new lines. I really want to do the opposite of that. I want to be able to separate based on a blank space. I have a string like "abcd efgh" and I need it to be "abcd" "efgh" (all without quotes, just to show groupings).

我敢肯定,这是简单得多比我做的,但我很困惑。

I'm sure this is much simpler than I'm making it, but I'm very confused.

更新问题:

我有已投入的阵列的PID的一列,但该阵列的每个元素有两个在列的PID。

I have a column of PIDs that I have put into an array, but each element of the array has both the pids in the column.

列:

1234
5678

当我打印出整个阵列,所有的不同列已被添加,所以我所有的值,但是当我打印出我的数组的单个元素我得到的是这样的:

when I print out the entire array, all the different columns have been added so I have all the values, but when I print out a single element of my array I get something like:

1234 5678

这不是我想要的。
我需要为1234和元素单独一个5678。

which is not what I want. I need to have an element for 1234 and a separate one for 5678.

这是我的code迄今:

This is my code so far:

!/bin/bash
echo "Enter the File Name"
read ips

index=0
IFS=' '
while read myaddr myname; do
    myips[$index]="$myaddr"
    names[$index]="$myname"
    index=$(($index+1))
done < $ips

echo "my IPs are: ${myips[*]}"
echo "the corresponding names are: ${names[*]}"
echo "Total IPs in the file: ${index}"

ind=0
for i in "$myips[@]}"
do
    echo $i
    pids=( $(jps | awk '{print $1}') )

    for pid in "${pids[@]}"; do
        echo $pid
    done

    echo "my PIDs are: ${pids}"

    for j in "${pids[@]}"
    do
        mypids[$ind]="$j"
        ind=$(($ind+1))
    done
done
echo "${mypids[*]}"
echo "The 3rd PID is: ${mypids[2]}"

输出示例:

Total IPs in the file: 6

xxx.xxx.xxx.xxx
5504
1268
1
xxx.xxx.xxx.xxx
5504
4352
1

xxx.xxx.xxx.xxx
5504
4340
1

5504
1268 5504
4352 5504
4340

The 3rd pid is: 5504
4340

我需要每个PID是分开的,使得数组的每个元素,是单个的pid。因此,例如,行第三pid是:需要看起来像

I need each pid to be separate, so that each element of the array, is a single pid. So for instance, the line "The 3rd pid is: " needs to look something like

The 3rd pid is: 5504

和第四要素是4340

and the 4th element would be 4340

推荐答案

尝试 剪切

$ echo "abcd efgh" | cut -d" " -f1
abcd

$ echo "abcd efgh" | cut -d" " -f2
efgh

另外,如果在某些时候你想要做的事比较复杂,不考虑 AWK 还有:

$ echo "abcd efgh" | awk '{print $1}'
abcd

$ echo "abcd efgh" | awk '{print $2}'
efgh


要解决你的问题的更新:


To address your updated question:

我有已投入的阵列的PID的一列,但该阵列的每个元素有两个在列的PID。

I have a column of PIDs that I have put into an array, but each element of the array has both the pids in the column.

如果你想一列数据加载到一个数组,你可以做这样的事情:

If you want to load a column of data into an array, you could do something like this:

$ pgrep sshd  # example command. Get pid of all sshd processes
795
32046
32225

$ A=(`pgrep sshd`) # store output of command in array A

$ echo ${A[0]}  # print first value
795

$ echo ${A[1]}  # print second value
32046


到C您发布,有关问题的原因是,你已经变 $ IFS 来一个空格(解决例如$ C $ IFS =''),这意味着这是由换行符分隔的列不再被拆分。


To address the example code you posted, the reason for your problem is that you've change $IFS to a space (IFS=' ') which means that your columns which are separated by newlines are no longer being split.

考虑这个例子:

$ A=(`pgrep sshd`)
$ echo ${A[0]}  # works as expected
795

$ IFS=' '       # change IFS to space only
$ A=(`pgrep sshd`)  
$ echo ${A[0]}  # newlines no longer used as separator
795
32046
32225

要避免这个问题,通常的做法是始终备份原始 IFS 并更换一旦你使用更新后的值来完成。例如。

To avoid this problem, a common approach is to always backup the original IFS and replace it once you've done using the updated value. E.g.

# backup original IFS
OLDIFS=$IFS

IFS=' '
# .. do stuff ...

# restore after use
IFS=$OLDIFS

这篇关于我如何拆分和QUOT; ABCD EFGH&QUOT;进入&QUOT; ABCD&QUOT;和&QUOT; EFGH&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆