Operating System - HP-UX
1836598 Members
1907 Online
110102 Solutions
New Discussion

Re: Common lines between two files

 

Common lines between two files

Hi,
I have two files with some common lines between them.
How Can I extract these common lines and put them into another file?

Are There some shell commands or scripts?

Thank You!!!!
Christian Aguilar
7 REPLIES 7
Zafar A. Mohammed_1
Trusted Contributor

Re: Common lines between two files

use grep or egrep command.

Thanks
Zafar
Wilfred Chau_1
Respected Contributor

Re: Common lines between two files

# sdiff -o output -l teset1 teset2

when you are prompted with %,
enter l.

Then your matching lines will be in the output file.
James R. Ferguson
Acclaimed Contributor

Re: Common lines between two files

Hi:

See the man pages for 'comm' and 'diff'.

Regards!

...JRF...
Dario_1
Trusted Contributor

Re: Common lines between two files

Hi!

See the man pages for diff

Regards,

DR
Systeemingenieurs Infoc
Valued Contributor

Re: Common lines between two files

You can and should user the underestimated join command, it's not only very performant, but also very powerful.


file1="file1"
file2="file2"

sort1="${file1}.sort"
sort2="${file2}.sort"

sort ${file1} > ${sort1}
sort ${file2} > ${sort2}

join ${sort1} ${sort2}

rm -f ${sort1} ${sort2}

hecou
A Life ? Cool ! Where can I download one of those from ?
H.Merijn Brand (procura
Honored Contributor

Re: Common lines between two files

It all very much depends on how these `common' lines appear in the files.

If both files are sorted, 'comm' is *the* way to go:

# comm -12 file1 file2 >common
# comm -13 file1 file2 >onlyin2
# comm -23 file1 file2 >onlyin1

Then of course you have the question "are all lines unique?", and do you want unique lines squeezed? If lines are distributed unsorted and non-uniquely, and you want to see all lines:

# fgrep -f file1 file2

which has the disadvantage that if file1 contains short lines that might be occuring as part of files2's lines, you get too much. With GNU grep you can do better:

# fgrep -x -f file1 file2

And to squeeze the output, filter the dups:

# fgrep -x -f file1 file2 | uniq

Or if the sequence is not important:

# fgrep -x -f file1 file2 | sort -u

Or alternatively

# perl -e'$f2=pop;while(<>){$x{$_}++};@ARGV=($f2);while(<>){exists$x{$_}and print' file1 file2

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
vasundhara
Frequent Advisor

Re: Common lines between two files

Hi,

you can use sdiff for this.

-o output
Use the next argument, output, as the name of a third file that is created as a user-controlled merging of file1 and file2. Identical lines of file1 and file2 are copied to output. Sets of differences, as produced by diff(1), are printed;

where a set of differences share a common gutter character. After printing each set of differences, sdiff prompts the user with a % and waits for user-typed commands.

If you give q, it will quit and the common lines will be present in file "output".

Regards
VJ