Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file comparison problem 1

Status
Not open for further replies.

pavNell

Technical User
Sep 27, 2002
178
US
I need to filter some lines from a file.

FileA looks similiar to:

4
34
1228
1776
2125
7054
7645
8845

FileB looks similiar to:

4
7054
8845

I need output to look like:

34
1228
1776
2125
7645

Basically, lines from fileA that are not in fileB

I've tried every conceivable uniq, sort, comm with no avail.
I'm thinking diff may be the tool but I just don't understand the output of diff.

I also tried:
for i in `cat fileA`
do
test `grep -v -w $i fileB` && echo "$i" >> FileC
done

but getting "test: too many arguments" error...

Thanks for any help.
 
tried?

comm -23 file1 file2

it works for the sample provided! vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Yes, I guess it does...however, given the next two sets of numbers, comm -23 fails to filter for numbers 1667 and 6329.

fileA

7
8
55
405
494
688
689
690
693
698
736
738
744
761
926
1332
1478
1667
1791
1932
2001
2077
2082
2132
2461
2527
2953
3352
3370
3408
3500
3823
3855
3868
3918
3969
3975
4114
4225
4262
4328
4963
5090
5176
5228
5398
5416
5427
5511
5531
5677
5733
6109
6274
6327
6329
6510
6693
7742
7749
7796
8210
8564
8693
8730
8758
8759
8833
8917
8984
9138
9194
9245
9402
9409
9629
9631
9655
9680
9693
9713
9744
9746
9769
9955
10000
10045
10056

fileB

1667
4044
4370
6329
6715
8618
9423
9684

I've found a solution, the following does the trick...

cat fileA | while read line
do
test `grep -w $line fileB 2> /dev/null` && : || echo &quot;$line&quot;
done >> fileC

I'll be sure to verify any output of comm in the future.
 
It appears the reason is comm expects the files to be lexically, not numerically sorted. Try the following:
Code:
comm -23 <(sort fileA) <(sort fileB) | sort -n
Cheers, Neil :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top