awk to append data to a file

Hi

I search for certain values in a file across many directories using the following awk code

awk '/Sl.*thickness/ {Sl=$3;Tt=$NF}END{print Sl, Tt}' DILAT.DAT

What I would like to do is write out Sl and Tt obtained from these files from many directories to a single file. So for example if I'm doing this for 10 directories I would have 10 entries in the file where I write these values to.

Thanks!

You can 'print' your output found to a file, for example:

awk '/Sl.*thickness/ {Sl=$3;Tt=$NF}END{print FILENAME ":" Sl, Tt >> "data.out"}' DILAT.DAT
1 Like

spacebar is right, but to fulfill your requirement of many files, you need to supply a number of files, like awk '...' DI*.DAT . Depending on your directory structure, you may want to use e.g. find to obtain all filenames applicable.
Then, in your command, only the very last occurrence of Sl.*thickness in the last file will be reported. If there's more of them, adapt your code. To report every single occurrence, make it

awk '/Sl.*thickness/ {print FILENAME, $3, $NF}' *.DAT

To report the last occurrence in every file, you need to check for file change (e.g. FNR == 1, FILENAME != oldfn) and then print the values out.

1 Like