I have multiple log files in a directory and want to print first line of those files which are having pattern vanished on last line.

Could it be done with awk for *.logs having also printed name of the logfile before the first line is printed out?


something like this?

for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done

You can also do it with awk but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.


If you want to do this with awk only this could probably get you in the right direction:

for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "\n" first_line : "" }' "$f"; done

The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk

And thanks to the comments below, now we can let gawk do its work as follows:

gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log 

On recent linux systems, awk is a symlink to gawk. For example, on recent Fedora releases this is the case:

# ls -l /bin/awk 
lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk
  • 2
    With a reasonably recent GNU awk you probably could avoid the loop e.g. gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log Dec 22 '18 at 17:31
  • 2
    That wouldn't work with files names foo=bar.log for instance. You can work around it by using ./*.log and replace FILENAME with substr(FILENAME, 3). Dec 22 '18 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.