eleanora eleanora - 1 year ago 80
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?

Answer Source
{ 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download