我在变量ArtTEXT中有这个(演示)文本。
1|Reporting Problems and Bugs.
2|Other freely available awk implementations.
5|Summary of installation.
8|How to disable certain gawk extensions.
3|Making Additions To gawk.
7|Accessing the Git repository.
这是一个变量,其中的行用已知的字符串分隔。
我想将它拆分为一个数组,其中行开头的数字将是使用一个命令的数组行的索引,而不是循环遍历行。
结果应为:
arr[1] => Reporting Problems and Bugs.
arr[2] => Other freely available awk implementations.
arr[5] => Summary of installation.
arr[8] => How to disable certain gawk extensions.
arr[3] => Making Additions To gawk.
arr[7] => Accessing the Git repository.
这可能吗?
答案 0 :(得分:2)
不,这是不可能的。以下是如何根据需要从变量填充数组(假设分隔行的“已知字符串”是换行符):
$ awk -v ArtTEXT='1|Reporting Problems and Bugs.
2|Other freely available awk implementations.
5|Summary of installation.
8|How to disable certain gawk extensions.
3|Making Additions To gawk.
7|Accessing the Git repository.' '
BEGIN {
split(ArtTEXT,lines,/\n/)
for (lineNr in lines) {
split(lines[lineNr],flds,/\|/)
arr[flds[1]] = flds[2]
}
for (i in arr) {
printf "arr[%d] => %s\n", i, arr[i]
}
}
'
arr[1] => Reporting Problems and Bugs.
arr[2] => Other freely available awk implementations.
arr[3] => Making Additions To gawk.
arr[5] => Summary of installation.
arr[7] => Accessing the Git repository.
arr[8] => How to disable certain gawk extensions.
答案 1 :(得分:1)
不需要使用数组,因为您只需更改字段分隔符。
awk 'BEGIN{FS="|"}{print $1, $2}' input.txt
$ 0包含整行 $ 1包含索引 判罚2美元
答案 2 :(得分:0)
如果记录分隔符是换行符,则可以使用引号扩展回显变量并像往常一样使用awk
,例如
echo "$ArtTEXT" | awk -F"|" '{ix[NR]=$1;arr[$1]=$2} END{for(i=1;i<=NR;i++) print "arr[" ix[i] "] => " arr[ix[i]]}'
arr[1] => Reporting Problems and Bugs.
arr[2] => Other freely available awk implementations.
arr[5] => Summary of installation.
arr[8] => How to disable certain gawk extensions.
arr[3] => Making Additions To gawk.
arr[7] => Accessing the Git repository.
由于awk
数组不保留顺序,因此需要为其保留单独的索引。