当字符串中包含管道符号|时,如何拆分字符串。 我想把它们分割成数组。

我试着

echo "12:23:11" | awk '{split($0,a,":"); print a[3] a[2] a[1]}'

这很好。如果我的字符串是像“12|23|11”,那么我如何把它们分割成一个数组?


当前回答

挑战:用空格解析和存储分割的字符串,并将它们插入变量中。

解决方案:最好的和简单的选择为您将转换字符串列表为数组,然后将其解析为具有索引的变量。下面是一个转换和访问数组的示例。

示例:解析每行的磁盘空间统计信息:

sudo df -k | awk 'NR>1' | while read -r line; do
   #convert into array:
   array=($line)

   #variables:
   filesystem="${array[0]}"
   size="${array[1]}"
   capacity="${array[4]}"
   mountpoint="${array[5]}"
   echo "filesystem:$filesystem|size:$size|capacity:$capacity|mountpoint:$mountpoint"
done

#output:
filesystem:/dev/dsk/c0t0d0s1|size:4000|usage:40%|mountpoint:/
filesystem:/dev/dsk/c0t0d0s2|size:5000|usage:50%|mountpoint:/usr
filesystem:/proc|size:0|usage:0%|mountpoint:/proc
filesystem:mnttab|size:0|usage:0%|mountpoint:/etc/mnttab
filesystem:fd|size:1000|usage:10%|mountpoint:/dev/fd
filesystem:swap|size:9000|usage:9%|mountpoint:/var/run
filesystem:swap|size:1500|usage:15%|mountpoint:/tmp
filesystem:/dev/dsk/c0t0d0s3|size:8000|usage:80%|mountpoint:/export

其他回答

echo "12|23|11" | awk '{split($0,a,"|"); print a[3] a[2] a[1]}'

我不喜欢回声“…”| awk…解决方案,因为它调用不必要的fork和execsystem调用。

我更喜欢迪米特里的解决方案

awk -F\| '{print $3 $2 $1}' <<<'12|23|11'

或者更短一点的版本:

awk -F\| '$0=$3 $2 $1' <<<'12|23|11'

在这种情况下,输出记录放在一起是一个真条件,所以它被打印出来。

在这个特定的情况下,stdin重定向可以通过设置一个awk内部变量来避免:

awk -v T='12|23|11' 'BEGIN{split(T,a,"|");print a[3] a[2] a[1]}'

我使用ksh相当长一段时间,但在bash中,这可以通过内部字符串操作来管理。在第一种情况下,原始字符串被内部终止符分割。在第二种情况下,假设字符串总是包含由一个字符分隔符分隔的数字对。

T='12|23|11';echo -n ${T##*|};T=${T%|*};echo ${T#*|}${T%|*}
T='12|23|11';echo ${T:6}${T:3:2}${T:0:2}

所有情况下的结果都是

112312

code

awk -F"|" '{split($0,a); print a[1],a[2],a[3]}' <<< '12|23|11'

输出

12 23 11

在awk中,我们使用split()函数将字符串拆分为数组:

awk '{split($0, array, ":")}'
#           \/  \___/  \_/
#           |     |     |
#       string    |     delimiter
#                 |
#               array to store the pieces

如果没有指定分隔符,则使用FS,默认为空格:

$ awk '{split($0, array); print array[2]}' <<< "a:b c:d e"
c:d

我们可以给出一个分隔符,例如::

$ awk '{split($0, array, ":"); print array[2]}' <<< "a:b c:d e"
b c

这相当于通过FS设置:

$ awk -F: '{split($0, array); print array[2]}' <<< "a:b c:d e"
b c

在GNU Awk中,你也可以提供分隔符作为regexp:

$ awk '{split($0, array, ":*"); print array[2]}' <<< "a:::b c::d e
#note multiple :
b c

甚至可以通过使用第四个参数来查看每一步的分隔符:

$ awk '{split($0, array, ":*", sep); print array[2]; print sep[1]}' <<< "a:::b c::d e"
b c
:::

让我们引用GNU awk的手册页:

split(string, array [, fieldsep [, seps ] ]) Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array. The first piece is stored in array[1], the second piece in array[2], and so forth. The string value of the third argument, fieldsep, is a regexp describing where to split string (much as FS can be a regexp describing where to split input records). If fieldsep is omitted, the value of FS is used. split() returns the number of elements created. seps is a gawk extension, with seps[i] being the separator string between array[i] and array[i+1]. If fieldsep is a single space, then any leading whitespace goes into seps[0] and any trailing whitespace goes into seps[n], where n is the return value of split() (i.e., the number of elements in array).

echo "12|23|11" | awk '{split($0,a,"|"); print a[3] a[2] a[1]}'

应该工作。