1

How to concatenate two files one to another using awk so that the first line of both is ignored?

Each file is in format:

#filename
4 5
5 6

Second for instance

#filename
7 6 
1 4 

Expected output:

4 5 
5 6 
7 6 
1 4 
  • 1
    welcome to U&L, what's wrong with cat ? e.g. cat f1 f2 – Archemar Feb 6 at 6:35
  • What is the logic here? Is it just file 1 followed by file 2? – Sparhawk Feb 6 at 6:36
  • @Archemar Just concatenating with cat would leave the header in the output. – Kusalananda Feb 6 at 7:43
  • @Kusalananda my bad, was early morning, I didn't see the catch. – Archemar Feb 6 at 8:03
2

It looks as if you want to concatenate the contents of the two files while skipping the first row of each file.

Getting everything but the first row of a file can be done in many ways, for example using awk:

awk 'FNR > 1' file

With awk, it's actually quite easy to do what you'd want to do with that exact awk program:

$ awk 'FNR > 1' file1 file2
4 5
5 6
7 6
1 4

FNR is a variable that awk maintains that holds the current record number (line number by default) in the current file. If FNR in this program is strictly greater than 1, then the current line in the current file is not the first line of that file. A program with just a condition like this in it will cause the lines for which the condition is true to be printed.

2

For completeness, here's a GNU tail-based solution

tail -q -n +2 file1 file2
0

Using sed i have done by below method

sed -n -e '1!p' f1 ;sed -n -e '1!p' f2

output

4 5
5 6
7 6
1 4
-1

Why not

awk '{print $0}' file1 file2
  • ... so that the first line of both is ignored... – Jeff Schaller Feb 7 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.