2

I have a text file named test.txt in current directory.now I want to output test.txt's first line and its last line to the terminal with cat text.txt | (head -1 ; tail -1), but the result is that it only outputs the first line to the terminal.

How do I output the first line and the last line using a single command?

3 Answers 3

2

The problem with your command is that only the 1st command - head -1 - receives the stdin input, because it consumes it on reading, so that the 2nd command - tail -1 - receives no input.

In this particular case, you can use command grouping ({ ...; ...; }):

{ head -1; tail -1; } < text.txt 

Caveats:

  • The above only works with seekable input, meaning either a regular file, a here-string or here-doc.

    • It will not work with pipeline input (cat text.txt | { head -1; tail -1; }) or with input from a process substitutions ({ head -1; tail -1; } < <(cat text.txt)), because such input is not seekable (tail cannot scan from the end backward).
  • Even with seekable input this is not a generic method to send input to multiple commands at once.

    • The above only works because tail reads backwards from the end of the (seekable) input, irrespective of whether all the input has already been read or not.

As a simpler alternative that works generically, here's a sed solution:

sed -n '1p; $p' text.txt 
  • -n suppresses output of lines by default
  • 1p matches line 1 and prints it (p).
  • $p matches the last line ($) and prints it.
Sign up to request clarification or add additional context in comments.

Comments

2

You could use awk.

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

Comments

0

Using tee:

cat text.txt | tee >(head -1) >(tail -1) > /dev/null 

3 Comments

Tempting, but, unfortunately, the sequencing of the output from the two process substitution cannot be guaranteed. On occasion, the last line will be printed first (doesn't happen often, but it does happen).
Hm, that's quite true... though OP didn't say that they have to be in that order ☺
Have you considered a career in law?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.