Operating System - Linux
1751742 Members
5600 Online
108781 Solutions
New Discussion юеВ

How i can merge two text files?

 
SOLVED
Go to solution
Francesco_13
Regular Advisor

How i can merge two text files?

Hi,
i have two text files with table names,for example:

file1 file2
--------|-------------
tab1 tab6
tab2 tab2
tab3 tab5
tab4 tab7
tab5 | tab4

How i can merge file1 and file2 and produce file3 with all table names , with NO duplicate table names?
thanks
Best regards.
Francesco
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: How i can merge two text files?

Hi:

# cat file1 file2 | sort -u > file3

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: How i can merge two text files?

sort -u file1 file2 > mymergedfile
If it ain't broke, I can fix that.
Francesco_13
Regular Advisor

Re: How i can merge two text files?

i'm sorry , i forget the last action:
for the same table name in file1 and file2, i'm do it to write the table name from file2 only.(the duplicate table_name to eliminate it's the table_name from file1).

more thanks and regards

Francesco
James R. Ferguson
Acclaimed Contributor

Re: How i can merge two text files?

Hi (again):

You wrote, "for the same table name in file1 and file2, i'm do it to write the table name from file2 only.(the duplicate table_name to eliminate it's the table_name from file1).".

I'm missing something. What does it matter if they are the same?

Regards!

...JRF...
Francesco_13
Regular Advisor

Re: How i can merge two text files?

Hi,
the file1 and file2 have the same table names, but file1 it's from one old version and file2 from new version of the same table. I need to produce the file3 with all table_names and know if the occurence it's from old or new version of same table.
(the same table have different data depending of version and i need to resume if the table have old or new data
example:
file3
tab1 > file1
tab2 > file1
tab3 > file1
tab4 > file2
tab5 > file2
tab6 > file2

i hope it's clear explained for my poor english, and sorry if i have changed the original question!
+ thanks and regards

Francesco
Hein van den Heuvel
Honored Contributor

Re: How i can merge two text files?

You'll need a little script.
Here is a perl suggestion and sample usage

$ cat x.pl
while ($file=shift @ARGV) {
open FILE, "<$file";
while () {
chomp;
$tables{$_} = $file;
}
}
foreach (sort keys %tables) {
print "$_ > $tables{$_}\n";
}$ cat x
aap
noot
mies
$ cat xx
noot
mies
teun
piet
$ cat xxx
teun
vuur$ perl x.pl x xx xxx
aap > x
mies > xx
noot > xx
piet > xx
teun > xxx
vuur > xxx
$ perl x.pl xxx x xx
aap > x
mies > xx
noot > xx
piet > xx
teun > xx
vuur > xxx


Cheers,
Hein.


spex
Honored Contributor

Re: How i can merge two text files?

Hi Francesco,

I tested this under FreeBSD, as I don't have access to HP-UX at the moment. HP-UX solution should be similar.

$ cat f1 && echo '====' && cat f2
tab1
tab1
tab2
tab3
tab4
====
tab2
tab4
tab6
tab7
$ grep -H '' * | sort -t: +1 | uniq
f1:tab1
f1:tab2
f2:tab2
f1:tab3
f1:tab4
f2:tab4
f2:tab6
f2:tab7

PCS
Francesco_13
Regular Advisor

Re: How i can merge two text files?

Hi,
thanks for script, but it's too hard for me.(i don├В┬┤t know use of perl) i will try to use it.
A traditionally unix script would be more easy for me.

thanks.
Best regards.
Francesco
Dennis Handly
Acclaimed Contributor

Re: How i can merge two text files?

From Hein's example you can use awk:
#!/usr/bin/ksh
TMP=/var/tmp/merge.$$
for i in $* ; do
awk -v NAME=$i '
{ print $0, ">", NAME
} ' $i >> $TMP
done
sort -u -k1,1 $TMP
rm -f $TMP

The output for the 3 files:
$ merge x*
aap > x
mies > x
noot > x
piet > xx
teun > xx
vuur > xxx