Alex
Alex

Reputation: 357

Printing strings with conditions with awk

I would like to have another question about this.

I use code:

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

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

to get:

# 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

with the name FLUX28ERROR

How to the code then there will be in the input

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

and I would like to print to outfile only 2020 instead of 2020-03-15 and b instead of blue? How to create a condition to print only the first 4 od 1 character?

The outputfile name should be FLUX2020.b

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

I do not know how to edit expression between ???

Upvotes: 1

Views: 48

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133518

In case your output file should be like FLUX2020blue going by your explanation then could you please try following.

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

In case someone wants to take care of "too many open files" error then try:

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

Upvotes: 3

Related Questions