1835536 Members
2863 Online
110078 Solutions
New Discussion

Re: ksh help

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

ksh help

Hi,

I have two files, file1 and file,

file1: file2:
line1 line1
line2 line3
line3 line4
line4 line7
line5 line8
line6 ...
line7
line8
...

I wanted to find out all lines, which is in file1 but not in file2, ex line2, line5, and line6...

thanks,
Roger
none
5 REPLIES 5
Randy Tarrier
Advisor
Solution

Re: ksh help

Hi Roger,
The easiest way is: diff file1 file2
The output will tell you wihch lines do not match exactly. "<" means the line is in file1 only,
">" means it is in file2 only.
Do it as long as you love it!
John Poff
Honored Contributor

Re: ksh help

Hi,

It sounds like you need the 'comm' command. It will show you lines that are common between the files. Try this:

comm -23 file1 file2

to see the lines that are in file1 but not in file2.

JP
Rodney Hills
Honored Contributor

Re: ksh help

If the files are sortted then-

join -v 1 file1 file2

will display only the lines that aren't in file1. -v 2 will display unmatched in file2.

HTH

-- Rod Hills
There be dragons...
Chris Vail
Honored Contributor

Re: ksh help

Like some of the others, I like the diff command, but here's a little more complex version that you can do from the command prompt:
for LINE in `cat file1`
do
LINE2=`grep $LINE file2`
if test "$LINE2"
then
:
else
echo "$LINE not found in file2"
fi
done

Good luck, and happy shell scripting!
Chris
monasingh_1
Trusted Contributor

Re: ksh help

cat file1 |grep -v -f file2



should work fine...