Raffael
Raffael

Reputation: 20045

Concatenate multiple files skipping first line

Let's assume three files f1,f2,f3 each containing:

header
line1
line2

Now I would like to concatenate all files beginning with "f" but skip the first line in each file - the final result should be:

line1
line2
line1
line2
line1
line2

Upvotes: 6

Views: 7877

Answers (4)

Matthias Munz
Matthias Munz

Reputation: 3765

Another option is to use sed:

ls f* | xargs -I{} sed 1d {}

Upvotes: 0

Raffael
Raffael

Reputation: 20045

find f* -type f -exec tail -n +2 {} \; -exec printf "\n" \; > result

Potential empty lines in result can be taken of by:

grep -v '^$' result > result2

Upvotes: -1

rici
rici

Reputation: 241861

With gnu coreutils:

tail -n+2 f*

Otherwise:

for f in f*; do tail -n+2 "$f"; done

or, if no filename contains a newline character

ls f* | xargs -I{} tail -n+2 {}

All of the above assume that there are no directories whose names start with f. Otherwise, again assuming no filename contains a newline character, you could use:

ls -p f* | grep -v / | xargs -I{} tail -n+2 {}

Or you could just redirect stderr to /dev/null to ignore the error messages from tail.

Upvotes: 3

Arnauld
Arnauld

Reputation: 6130

You can do it this way:

awk FNR!=1 f* > result

Upvotes: 20

Related Questions