是awk 2维数组或类似的东西存储值?

  • 本文关键字:存储 2维 awk 数组 awk csh
  • 更新时间 :
  • 英文 :


大家好,我是awk的新手,我可以问我有一个这样的输入文件:

# ABC DEFG
value1 GH
value2 GH
value3 GH
# BCF SQW
value4 GH
value5 GH
# BEC YUW
value6 GH
value7 GH

欲望输出:

##### ABC DEFG #####
DEFG_ABC
ABC_DEFG
value1 ABC
value1 DFG
value2 ABC
value2 DFG
value3 ABC
value3 DFG
##### BCF SQW #####
BCF_SQW
SQW_BCF
value4 BCF
value4 SQW
value5 BCF
value5 SQF
##### BEC YUW #####
BEC_YUW
YUW_BEC
value6 BEC
value6 YUW
value7 BEC
value7 YUW

我将文件$2和$3在一行字符#中分隔成数组,像这样

awk '
/^#/ {
a[na++] = $2
b[nb++] = $3
}
END { 
for(i = 0; i < na; i ++){
print ("######" a[i] " " b[i] "#####")
print (a[i] "_" b[i])
print (b[i] "_" a[i])
}
}
' input

但是我不知道如何存储"#"之间的所有行的$1行到数组有人怎么做吗?非常感谢

使用您显示的示例,请尝试以下awk代码。在GNUawk中编写和测试应该可以在任何awk中工作。

awk '
/^#/{
sub(/^#/,"##### ")
print $0,"  #####"
val1=$2
val2=$3
print val1"_"val2 ORS val2"_"val1
next
}
{
print $1,val1 ORS $1,val2
}
' Input_file

解释:为以上内容添加详细说明。

awk '                          ##Starting awk program from here.
/^#/{                          ##Checking condition if line starts from #.
sub(/^#/,"##### ")           ##Substituting starting # with #####
print $0,"  #####"           ##Printing current line follows by ##### here.
val1=$2                      ##Creating val1 which has 2nd field in it.
val2=$3                      ##Creating val2 which has 3rd field in it.
print val1"_"val2 ORS val2"_"val1  ##Printing val1 _ val2  newline val2 _ val1.
next                         ##next will skip further statements from here.
}
{
print $1,val1 ORS $1,val2    ##Printing 1st field val1 ORS 1st field val2 here.
}
' Input_file                   ##Mentioning Input_file name here.

相关内容

  • 没有找到相关文章

最新更新