Home > other >  How to get file name which have error using AWK command?
How to get file name which have error using AWK command?

Time:03-12

I am using the SAC tool to read the header information but some files have no header information and it prints an error. Is there any way to use AWK to print that files if they do not have a header or error during work. I often used AWK for data manipulation but failed this time.

Here is my try:

saclst a f *2020-05*BHZ*

This is the output

GS.GS043.2020-05-18T03:52.BHZ.sac         3.37
GS.GS043.2020-05-18T09:28.BHZ.sac         3.64
GS.GS043.2020-05-18T12:09.BHZ.sac         3.42
saclst: Error determining SAC header: GS.GS043.2020-05-18T14:36.BHZ.sac
GS.GS043.2020-05-18T16:25.BHZ.sac         2.92
GS.GS043.2020-05-18T18:51.BHZ.sac         3.66

Now I want to get the file name and print it but seems like AWK does not help;

saclst a f *2020-05*BHZ* | awk '{if ($2<0) print $1;}' > ../test.dat

My output file is empty and the terminal shows this error:

Is there any way to save this error so I can later modify it?

saclst: Error determining SAC header: SC.LZB.2020-05-21T10:46.BHZ.sac
saclst: Error determining SAC header: SC.LZB.2020-05-21T11:57.BHZ.sac
saclst: Error determining SAC header: SC.LZB.2020-05-26T11:23.BHZ.sac
saclst: Error determining SAC header: SC.LZB.2020-05-28T10:44.BHZ.sac
saclst: Error determining SAC header: SC.QSC.2020-05-12T06:49.BHZ.sac

CodePudding user response:

Here's what I think you are looking for:

# just for demo, pipe SAC tool to awk for your actual use case
$ cat ip.txt
GS.GS043.2020-05-18T03:52.BHZ.sac         3.37
GS.GS043.2020-05-18T09:28.BHZ.sac         3.64
GS.GS043.2020-05-18T12:09.BHZ.sac         3.42
saclst: Error determining SAC header: GS.GS043.2020-05-18T14:36.BHZ.sac
GS.GS043.2020-05-18T16:25.BHZ.sac         2.92
GS.GS043.2020-05-18T18:51.BHZ.sac         3.66

# filter lines with Error based on number of fields or `Error` in 2nd field
$ awk 'NF != 2' ip.txt
saclst: Error determining SAC header: GS.GS043.2020-05-18T14:36.BHZ.sac
$ awk '$2 == "Error"' ip.txt
saclst: Error determining SAC header: GS.GS043.2020-05-18T14:36.BHZ.sac

# print only last field
$ awk '$2 == "Error"{print $NF}' ip.txt
GS.GS043.2020-05-18T14:36.BHZ.sac

If the saclst command puts the lines with Error on stderr, you can use this:

$ saclst a f *2020-05*BHZ* 2> error.log

CodePudding user response:

Athough awk (what you asked) works,

sed -n 's/.*Error.*:/ /p' ip.txt

would work fine as well. And

grep Error ip.txt

So, don't focus on using only awk.

  • Related