could you help me with bash/awk script? I have several .dat files in the directory. All of these files consist of header and from data:
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
filename - Old-file I would like for all of the files:
1) to save name of the file to variable
2) to save some information from header to variables - for instance the string after "c column3= ", "c column2= " and "c ROIysiz= "
3) using the variables with saved information from the header, I would like to rename the file - for instance "FLUX28"
4) create new file
5) to print information from variables to the first row of the new file - for instance file name of the original file, the information after "c column3= ", "c column2= "
6) print data - print the part of the original file after line starting "c end header"
7) add # to the start of the first line
#!/bin/bash
for file in *.dat; do # loop in the directory
awk -v FILE=$FILE_NAME # save file name to variable FILE
/c end header/ { in_f_format=0; next } # print file from c end header
{print $1, $2, $3} # print columns
BEGIN{printf("#")}1 # adding hashtag before the first line
; done # end of loop
desired output
files with names FLUX28
(in another file will be another number - file name will consists of strings from the header) in the files will be:
#Old-file ERROR FLUX
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
Code from the discussion:
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 "#" $0 > (out_file)
}
else{
print > (out_file)
}
}
' input