sed Solution:
sed -e 1b -e '$!d' file
When reading from stdin
if would look like this (for example ps -ef
):
ps -ef | sed -e 1b -e '$!d'
UID PID PPID C STIME TTY TIME CMD
root 1931 1837 0 20:05 pts/0 00:00:00 sed -e 1b -e $!d
head & tail Solution:
(head -n1 && tail -n1) <file
When data is coming from a command (ps -ef
):
ps -ef 2>&1 | (head -n1 && tail -n1)
UID PID PPID C STIME TTY TIME CMD
root 2068 1837 0 20:13 pts/0 00:00:00 -bash
awk Solution:
awk 'NR==1; END{print}' file
And also the piped example with ps -ef
:
ps -ef | awk 'NR==1; END{print}'
UID PID PPID C STIME TTY TIME CMD
root 1935 1837 0 20:07 pts/0 00:00:00 awk NR==1; END{print}
wc -l
has nothing to do with outputting the last line of a file.