Awk-如何使用字符串打印变量
我有这个输入,我想学习如何在=to变量之后保存字符串,并将其用于输出文件名和以“#”开头的输出的第一行 期望的输出,例如: 姓名: Flux28错误 内容:Awk-如何使用字符串打印变量,awk,Awk,我有这个输入,我想学习如何在=to变量之后保存字符串,并将其用于输出文件名和以“#”开头的输出的第一行 期望的输出,例如: 姓名: Flux28错误 内容: # FLUX 18262 2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03 2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03 2.4583
# FLUX 18262
2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
我有一个剧本:
awk '
/ROIysiz/{
second_out=$NF
}
/column2/{
close(out_file)
found=count=""
out_file=$NF second_out
next
}
/end header/{
found=1
next
}
found && out_file{
if(++count==1){
print "#" $0 > (out_file)
}
else{
print > (out_file)
}
}
' inputname
这将提供一个文件FLUX28:
#2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
我想知道如何向文件名添加另一个字符串,以及如何打印输出的第一行:
# FLUX 18262
我试过:
awk '
/ROIysiz/{
second_out=$NF
}
/column 3/{
third_part=$NF
}
/column2/{
close(out_file)
found=count=""
out_file=$NF second_out third_part
next
}
/end header/{
found=1
next
}
found && out_file{
if(++count==1){
print "#" second_out third_part > (out_file)
}
else{
print > (out_file)
}
}
' inputname
提供输出文件名FLUX28并包含:
#28
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
2.458377252462999895e+06 2.159254025049928832e-02 2.315911471112144012e-03
2.458377462405352853e+06 1.721511461149537181e-02 1.687658552459528729e-03
2.458377602279778104e+06 1.744415665326638776e-02 3.041609691486800784e-03
2.458377956590285990e+06 8.597543276201942419e-03 3.490433838852374532e-03
2.458378025015166495e+06 6.127180820289755692e-03 2.437530774283428858e-03
为什么变量第三部分没有打印在输出文件名和第一行上?谢谢请尝试以下内容。在第一行,这将添加
column2
line、RedNumDa
line和hash的值,然后用新行打印实际行
awk '
/ROIysiz/{
second_out=$NF
}
/RedNumDa/{
first_line_value=$NF
}
/c column3/{
third_part=$NF
}
/column2/{
close(out_file)
found=count=""
first_part=$NF
out_file=first_part second_out third_part
next
}
/end header/{
found=1
next
}
found && out_file{
if(++count==1){
print "#" first_part OFS first_line_value ORS $0 > (out_file)
}
else{
print > (out_file)
}
}
' Input_file
解释:添加上述代码的详细解释
awk ' ##Starting awk program from here.
/ROIysiz/{ ##Checking condition if a line contains string ROIysiz then do following.
second_out=$NF ##Creating variable second_out for output file 2nd part.
}
/RedNumDa/{ ##Checking condition if line contains RedNumDa string in it.
first_line_value=$NF ##Creating variable first_line_value for output file 1st part.
}
/c column3/{ ##Checking condition if line contains column 3 string in it.
third_part=$NF ##Creating variable third_part and setting its value to last field of current line.
}
/column2/{ ##Checking condition if line contains column2 string in it.
close(out_file) ##Closing out_file to avoid "too many files opened" error.
found=count="" ##Nullifying variable found here.
first_part=$NF ##Creating variable first_part which has last part of current line as value.
out_file=first_part second_out third_part ##Creating variable out_file which is having last field of current line and second_out variable value.
next ##next will skip all further statements from here.
}
/end header/{ ##Checking condition if string end header is found then do following.
found=1 ##Setting variable found to 1 here.
next ##next will skip all further statements from here.
}
found && out_file{ ##Checking condition if found AND out_file is SET then do following.
if(++count==1){ ##If count==1 then do following, to add # in starting of first line.
print "#" first_part OFS first_line_value ORS $0 > (out_file) ##Printing # and first_part OFS first_line_value ORS $0.
}
else{ ##Else if count is greater than 1 then do following.
print > (out_file) ##Printing current line to out_file here.
}
}
' Input_file ##Mentioning Input_file name here.
你能试试下面的吗。在第一行,这将添加
column2
line、RedNumDa
line和hash的值,然后用新行打印实际行
awk '
/ROIysiz/{
second_out=$NF
}
/RedNumDa/{
first_line_value=$NF
}
/c column3/{
third_part=$NF
}
/column2/{
close(out_file)
found=count=""
first_part=$NF
out_file=first_part second_out third_part
next
}
/end header/{
found=1
next
}
found && out_file{
if(++count==1){
print "#" first_part OFS first_line_value ORS $0 > (out_file)
}
else{
print > (out_file)
}
}
' Input_file
解释:添加上述代码的详细解释
awk ' ##Starting awk program from here.
/ROIysiz/{ ##Checking condition if a line contains string ROIysiz then do following.
second_out=$NF ##Creating variable second_out for output file 2nd part.
}
/RedNumDa/{ ##Checking condition if line contains RedNumDa string in it.
first_line_value=$NF ##Creating variable first_line_value for output file 1st part.
}
/c column3/{ ##Checking condition if line contains column 3 string in it.
third_part=$NF ##Creating variable third_part and setting its value to last field of current line.
}
/column2/{ ##Checking condition if line contains column2 string in it.
close(out_file) ##Closing out_file to avoid "too many files opened" error.
found=count="" ##Nullifying variable found here.
first_part=$NF ##Creating variable first_part which has last part of current line as value.
out_file=first_part second_out third_part ##Creating variable out_file which is having last field of current line and second_out variable value.
next ##next will skip all further statements from here.
}
/end header/{ ##Checking condition if string end header is found then do following.
found=1 ##Setting variable found to 1 here.
next ##next will skip all further statements from here.
}
found && out_file{ ##Checking condition if found AND out_file is SET then do following.
if(++count==1){ ##If count==1 then do following, to add # in starting of first line.
print "#" first_part OFS first_line_value ORS $0 > (out_file) ##Printing # and first_part OFS first_line_value ORS $0.
}
else{ ##Else if count is greater than 1 then do following.
print > (out_file) ##Printing current line to out_file here.
}
}
' Input_file ##Mentioning Input_file name here.
未经测试的代码如下:
/^c/ { X[$2] = $3 }
/^c end/ { outfile = X["column2="] X["ROIysiz="] X["column3="]
print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
编辑:现在测试代码:
$ cat x
c ROIysiz= 28
c column1= HJD
c RedNumDa= 18262
c column3= ERROR
c column2= FLUX
c end header ---------------------------------------------------------------------------------------
2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
$ awk '
/^c/ { X[$2] = $3 }
/^c end/ { outfile = X["column2="] X["ROIysiz="] X["column3="]
print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
' x
$ cat FLUX28ERROR
# FLUX 18262
2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
/^c/
模式中的第一行保存每个标题行(以字母c
开头的行)。在/^c end/
模式中,第二行和第三行使用字符串连接计算输出文件名,以连接不同的头值,然后以类似的方式写入输出文件的第一行,使用,
运算符分隔字段。第四行,在中/^c/
pattern,将每个非标题行(未更改)打印到输出文件中。关联数组X
将每个标题键存储为其索引,并将关联值存储为其值。未测试的代码如下:
/^c/ { X[$2] = $3 }
/^c end/ { outfile = X["column2="] X["ROIysiz="] X["column3="]
print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
编辑:现在测试代码:
$ cat x
c ROIysiz= 28
c column1= HJD
c RedNumDa= 18262
c column3= ERROR
c column2= FLUX
c end header ---------------------------------------------------------------------------------------
2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
$ awk '
/^c/ { X[$2] = $3 }
/^c end/ { outfile = X["column2="] X["ROIysiz="] X["column3="]
print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
' x
$ cat FLUX28ERROR
# FLUX 18262
2.458375368952875026e+06 -8.420548421860798386e-04 7.020812100561693928e-03
2.458375579737625085e+06 -5.579159672996818198e-03 1.285380720081348528e-03
2.458376278315599542e+06 -7.634101850411220518e-03 2.481065693991901019e-03
2.458376347386624664e+06 7.223482191697593166e-04 2.319993894372075760e-03
2.458376416108166799e+06 5.238757879614985152e-03 1.389030320490110878e-03
2.458376485913363751e+06 6.777606553373448882e-03 8.887787066666734273e-04
2.458377048675692175e+06 1.950435173388009522e-02 3.242344477396308117e-03
2.458377185153110884e+06 1.885754079806525874e-02 2.090836971653367571e-03
/^c/
模式中的第一行保存每个标题行(以字母c
开头的行)。在/^c end/
模式中,第二行和第三行使用字符串连接计算输出文件名,以连接不同的头值,然后以类似的方式写入输出文件的第一行,使用,
运算符分隔字段。第四行,在中/^c/
pattern,将每个非标题行(未更改)打印到输出文件中。关联数组X
将每个标题键存储为其索引,并将关联值存储为其值。缩进和空白有助于使任何程序更易于阅读/理解。缩进和空白有助于使任何程序更易于阅读/理解。