1

If I have 2 files where the first lines contain identical headings:

A.txt:

A
1
aa

B.txt

A
ee
7

I want to combine them like so:

C.txt

A
1
aa
ee
7

Is there a one liner to do this?

2
  • Do you want this to happen only if the header is the same? – mattdm Dec 3 '10 at 13:05
  • Not necessary.. I'm thinking of a simple case when I know the headers are the same. Handling the case where the headers can be slightly different might be the subject of another question :-) – StamfordBingo Dec 3 '10 at 13:27
3
tail -n +2 B.txt | cat A.txt - > C.txt
0
0

If order is not a problem:

sort A.txt B.txt | uniq

2
  • 1
    How will this result in the desired output? – Ignacio Vazquez-Abrams Dec 3 '10 at 13:06
  • This also deletes duplicate lines. – Jonas Stein Sep 20 '14 at 3:27
0

Here's another way that's easily scalable to any number of input files (just add them as additional arguments:

awk 'FNR!=NR&&FNR==1{next}{print}' A.txt B.txt > C.txt

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.