I am trying to get the first and last line of a set of files in a directory.
I have a set of .log files. Want to be able to get the first and last line (there is no whitespace/gap at the top/bottom of each file).
I want to grab the first and last line from the oldest file first and keep repeating until its got to the most recent file (this to be done via modification time) and get the results from all of them into one file
e.g. the below are a set of files, I want a line before each file output so I know which line belongs from which log file. Have manually entered the log files in the order but would rather the script chose the oldest-newest automatically.
echo "S26" >> RunTimes.txt ; sed -n -e '1p;$p' S26.log >> RunTimes.txt ;
echo "S27" >> RunTimes.txt ; sed -n -e '1p;$p' S27.log >> RunTimes.txt ;
echo "S28" >> RunTimes.txt ; sed -n -e '1p;$p' S28.log >> RunTimes.txt ;
echo "S29" >> RunTimes.txt ; sed -n -e '1p;$p' S29.log >> RunTimes.txt ;
echo "S30" >> RunTimes.txt ; sed -n -e '1p;$p' S30.log >> RunTimes.txt ;
echo "S31" >> RunTimes.txt ; sed -n -e '1p;$p' S31.log >> RunTimes.txt ;
echo "S32" >> RunTimes.txt ; sed -n -e '1p;$p' S32.log >> RunTimes.txt ;
echo "S33" >> RunTimes.txt ; sed -n -e '1p;$p' S33.log >> RunTimes.txt ;
There has to be a more efficient way of doing this, any help is much appreciated.
Thanks
EDIT
Thanks to @JorgeBellon for the heads up, when I try and convert
sed -n '1p' to '1p;$p'
I receive -bash: $0 >> RunTimes.txt: command not found
This is the complete query below:
ls -t | xargs -n1 bash -c 'echo $0 >> RunTimes.txt; sed -n "1p;$p" $0 >> RunTimes.txt'
Not sure if it is because of using bash that is does not like how it is formatted?
As a workaround I tried using
head -n1 && tail -n1
In the hope of getting first and last line but no success.
If i use double quotes so "1p;$p" as oppose to '1p;$p' the query runs, but only get first line back of each log.
Regards