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

我试着

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

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


当前回答

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).

code

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

输出

12 23 11

我知道这是个老问题,但我想也许有人喜欢我的把戏。特别是因为这个解决方案不局限于特定数量的项目。

# Convert to an array
_ITEMS=($(echo "12|23|11" | tr '|' '\n'))

# Output array items
for _ITEM in "${_ITEMS[@]}"; do
  echo "Item: ${_ITEM}"
done

输出将是:

Item: 12
Item: 23
Item: 11

笑话?:)

如何回应“12 | | 23日11”| awk的{分裂(0美元,“|”);打印一个[3]a[2] a[1]}'

这是我的输出:

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

所以我想它还是有用的。

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

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

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

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