Operating System - Linux
1748268 Members
3698 Online
108760 Solutions
New Discussion юеВ

Re: How i can merge two text files?

 
SOLVED
Go to solution
Hein van den Heuvel
Honored Contributor

Re: How i can merge two text files?

Actually, with AWK there is a really 'short' solution:

$ awk '{a[$1]=FILENAME} END {for (k in a) { print k " > " a[k] }}' file1 file2

With the same sample files

$ grep "." x*
x:aap
x:noot
x:mies
xx:noot
xx:mies
xx:teun
xx:piet
xxx:teun
xxx:vuur

$ awk '{a[$1]=FILENAME} END {for (k in a) { print k " > " a[k] }}' x xx xxx
teun > xxx
vuur > xxx
mies > xx
aap > x
noot > xx
piet > xx

$ awk '{a[$1]=FILENAME} END {for (k in a) { print k " > " a[k] }}' xxx xx x
teun > xx
vuur > xxx
mies > x
aap > x
noot > x
piet > xx
Hein



Sandman!
Honored Contributor

Re: How i can merge two text files?

Try the awk construct below:

awk '{
if (x[$1]) {
if (FILENAME=="file2")
x[$1]=FILENAME
} else
x[$1]=FILENAME
} END{for (i in x) print i" > "x[i]}' file1 file2
Francesco_13
Regular Advisor

Re: How i can merge two text files?

Hi,
thanks to all! with the last solution i have produced the correct file.
Best regards.
Francesco