1837131 Members
2350 Online
110112 Solutions
New Discussion

Concatenation of 2 Files

 
SOLVED
Go to solution
Pando
Regular Advisor

Concatenation of 2 Files

I have 2 files which contains the same line item 1. During concatenation, i need to dispose of the line 1 from file2 and append the succeeding line to file1. Is there a way to do it in one command line?

Maximum points to all correct reply!

Thanks!
8 REPLIES 8
Elmar P. Kolkman
Honored Contributor

Re: Concatenation of 2 Files

I'm not sure I understand the question correctly, but you could do something like this:
sed -n '2,$p' file2 >> file1

Of course there are lots of ways to do the cat without line 1...
Every problem has at least one solution. Only some solutions are harder to find.
H.Merijn Brand (procura
Honored Contributor

Re: Concatenation of 2 Files

# man join

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Muthukumar_5
Honored Contributor

Re: Concatenation of 2 Files

To remove first line of file2 then,

awk '{ if (NF!=1) print $0 }' file2 >> file1

If you want to remove duplicate lines in file2 compared to file1 then,

grep -vf file1 file2 >> file1

HTH.
Easy to suggest when don't know about the problem!
Elmar P. Kolkman
Honored Contributor

Re: Concatenation of 2 Files

I think Muthumumar meant NR in his awk syntax.

The grep is also dangorous. If file1 contains a line consisting of a part of a line of file2, those are skipped when adding file2.

Example: file1 contains 2 lines:
aaa
bba

file2 contains following lines:
aaaa
bbbb
bbba

Now only line 2 would be added with the grep...

If the resulting order is not important, a simpler solution would be in that case:
sort -u file1 file2 > file3
Every problem has at least one solution. Only some solutions are harder to find.
Thierry Poels_1
Honored Contributor
Solution

Re: Concatenation of 2 Files

hi,

tail +2 file2 >> file1

good luck,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Kent Ostby
Honored Contributor

Re: Concatenation of 2 Files

If you want all the lines after line 1 then it would be:

awk 'NR>1{print $0}' file2 >> file1

for just the second line, it would be:

awk 'NR==2{print $0}' file2 >> file1

Oz
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
James A. Donovan
Honored Contributor

Re: Concatenation of 2 Files

Maybe something like this is what you're looking for?

Given two files:

$ cat file1
aaa
bbb
ccc
ddd

$ cat file2
aaa
eee
fff

$ comm -13 file1 file2 >>file1

...this would append all lines that appear ONLY in file2 to the end of file1...

$ cat file1
aaa
bbb
ccc
ddd
eee
fff

...continuing the example...

$ cat file3
aaa
bbb
ccc
ggg

$ comm -13 file1 file3 >>file1

$ cat file1
aaa
bbb
ccc
ddd
eee
fff
ggg

...shows that since file3 and file1 both contain lines aaa, bbb and ccc, only line ggg from file3 is appended.
Remember, wherever you go, there you are...
Pando
Regular Advisor

Re: Concatenation of 2 Files

Thanks for all the replies. It was a big help!