使用awk条件打印字符串

时间:2020-02-26 04:02:04

标签: awk

我想对this提出另一个问题。

我使用代码:

awk '
/^c/ { X[$2] = $3 }
/^c end/ { outfile = X["column2="] X["ROIysiz="] X["column3="]
           print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
' input

输入:

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

获得:

# 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
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

名称为FLUX28ERROR

然后如何输入代码

c ROIysiz= 2020-03-15
c column1= HJD
c RedNumDa= 18262
c column3= blue
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

,而我只想打印2020年而不是2020-03-15,而不是蓝色?如何创建仅打印前4个od 1字符的条件?

输出文件的名称应为FLUX2020.b

**outfile = X["column2="] ???X["ROIysiz="]??? "." ???X["column3="]???**

我不知道如何在???之间编辑表达式

1 个答案:

答案 0 :(得分:3)

如果您的输出文件像FLUX2020blue那样,请按照说明进行操作。

awk '
/^c/ { X[$2] = $3 }
/^c end/ { outfile = X["column2="] substr(X["ROIysiz="],1,4) X["column3="]
           print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
'  Input_file

如果有人想处理“打开的文件过多”错误,请尝试:

awk '
/^c/ { close(outfile); X[$2] = $3 }
/^c end/ { outfile = X["column2="] substr(X["ROIysiz="],1,4) X["column3="]
           print "#", X["column2="], X["RedNumDa="] > outfile }
!/^c/ { print $0 >> outfile }
'  Input_file