我有此输入,我想学习如何将=之后的字符串保存到变量中,并将其用于输出文件名以及在以“#”开头的输出的第一行中
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
所需的输出,例如:
名称: 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
我有一个脚本:
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
为什么变量Third_part没有打印在输出文件名和第一行中?谢谢
答案 0 :(得分:2)
能否请您尝试以下。在第一行,这将添加column2
行,RedNumDa
行和哈希值的值,然后在新行中添加实际行。
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.
答案 1 :(得分:2)
未经测试的代码如下:
/^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/
模式中的第四行将未更改的每行非标题行打印到输出文件。关联数组X
存储每个标头键作为其索引,并将关联的值存储为其值。