eleanora eleanora - 10 months ago 55
Bash Question

Pipe the heads of two files to a file

Quite often I want to take the first 1000 lines of one file, say, the first 200 of another and pipe the concatenation to a file.

To do this I can do

head -1000 fileA > fileA-1000
head -200 fileB > fileB-200

cat fileB-200 fileA-1000 > file C

Is it possible to do this all as one command?

{ head -200 fileB; head -1000 fileA; } > fileC

One can group commands together with {...}. Once the commands are grouped, then I/O redirection can be applied to the group as a whole.