Shell – Print first and last line of all files in folder

awkshell

I have a bunch of log files that get overwritten (file.log.1, file.log.2 etc). When I copy them from the device making them onto my local machine I lose the original time stamps. So I'd like to put them in chronological order. The problem is that I don't necessarily know which is the newest and which is the oldest.

What I'd like to be able to do is, if all the logs are in a directory, print something like this:

file: file.log.1
first line: [first line that isn't whitespace]
last line: [last line that isn't whitespace]

I can just write a python script to do this, but I'd much rather do it with linux built-ins if possible. Is this a job for awk/sed? Or would this really be better off for a scripting language? If yes to awk/sed, how woul dyou go about doing it?

I found this awk command by searching, but it only accepts one file name and will print whatever the last line is (and there can be a variable number of empty lines at the end)

awk 'NR == 1 { print }END{ print }' filename

Best Answer

So I like sed the answer can be

for file in file.log.*
do
   echo "file: $file"
   echo -n "first line: "
   cat "$file" | sed -n '/^\s*$/!{p;q}'
   echo -n "last line: "
   tac "$file" | sed -n '/^\s*$/!{p;q}'
done
Related Question