Operating System - HP-UX
1834328 Members
2730 Online
110066 Solutions
New Discussion

estract content form two file

 
SOLVED
Go to solution
Marco_67
Advisor

estract content form two file

Hello,

I have two files, file1 and file2.
I need to extract only those line from file2 that contains line text of file1.

Do I need some scripting or could it be done by concatenating commands ?
Please detail me about.

Thank you
0
9 REPLIES 9
Suraj Singh_1
Trusted Contributor

Re: estract content form two file

I did not get your question..

Try "comm -12 file1 file 2" and see if it helps.

Rgds
What we cannot speak about we must pass over in silence.
MarkSyder
Honored Contributor
Solution

Re: estract content form two file

I'm making two possibly incorrect assumptions:

1. you want to match the whole line

2. you want the output to go to a new file (file3)

> file3
for i in `cat file2`
do
grep $i file1 >> file3
done

This empties file3 before creating the new file3.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Etienne Roseau
Frequent Advisor

Re: estract content form two file

Mark is writing faster than i do....
but this is also what i understood from your question.
MarkSyder
Honored Contributor

Re: estract content form two file

It's touch typing Ettienne ;-)

Mark
The triumph of evil requires only that good men do nothing
Peter Godron
Honored Contributor

Re: estract content form two file

Marco,
the comm -12 solution only works for sorted files and matches on complete lines!!
So please sort the files beforehand.

Would it be possible to provide a couple of example lines?

Regards
Etienne Roseau
Frequent Advisor

Re: estract content form two file

hi Mark,
maybe 'cause of my nails (like my boss's charming blond secretary !)
;-)
Muthukumar_5
Honored Contributor

Re: estract content form two file

You can try as,

grep -f file1 file2

Else, get every line in file1 and do grep as like as,

while read line; do

grep $line file2

done < file1

hth.
Easy to suggest when don't know about the problem!
H.Merijn Brand (procura
Honored Contributor

Re: estract content form two file

Muthkumar, that grep is both fast and dangerous!

What if the lines in file 1 have

.*

?

Ouch!

# perl -ne'BEGIN{local@ARGV=("file1");while(<>){$f1{$_}++}}exists$f1{$_}and print' file2 ...

Safe and simple

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Godron
Honored Contributor

Re: estract content form two file

Marco,
can you please clarify:
1. Are the files sorted
2. Are you interested in matching the whole line of file1 against whole line of file2.
Regards