awk 是二维数组还是类似于存储值的东西?
Is awk 2 dimension array or something similar to store value?
大家好,我是 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++] =
b[nb++] =
}
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
代码。在 GNU awk
中编写和测试应该在任何 awk
.
中工作
awk '
/^#/{
sub(/^#/,"##### ")
print [=10=]," #####"
val1=
val2=
print val1"_"val2 ORS val2"_"val1
next
}
{
print ,val1 ORS ,val2
}
' Input_file
说明:为以上添加详细说明。
awk ' ##Starting awk program from here.
/^#/{ ##Checking condition if line starts from #.
sub(/^#/,"##### ") ##Substituting starting # with #####
print [=11=]," #####" ##Printing current line follows by ##### here.
val1= ##Creating val1 which has 2nd field in it.
val2= ##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 ,val1 ORS ,val2 ##Printing 1st field val1 ORS 1st field val2 here.
}
' Input_file ##Mentioning Input_file name here.
大家好,我是 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++] =
b[nb++] =
}
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
代码。在 GNU awk
中编写和测试应该在任何 awk
.
awk '
/^#/{
sub(/^#/,"##### ")
print [=10=]," #####"
val1=
val2=
print val1"_"val2 ORS val2"_"val1
next
}
{
print ,val1 ORS ,val2
}
' Input_file
说明:为以上添加详细说明。
awk ' ##Starting awk program from here.
/^#/{ ##Checking condition if line starts from #.
sub(/^#/,"##### ") ##Substituting starting # with #####
print [=11=]," #####" ##Printing current line follows by ##### here.
val1= ##Creating val1 which has 2nd field in it.
val2= ##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 ,val1 ORS ,val2 ##Printing 1st field val1 ORS 1st field val2 here.
}
' Input_file ##Mentioning Input_file name here.