Computing.Net > Forums > Unix > comparing files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

comparing files

Reply to Message Icon

Name: chew
Date: March 20, 2008 at 00:02:46 Pacific
OS: solaris 5.9
CPU/Ram: -
Product: -
Comment:

hi,

file1:
hello
hi
test
god

file2:
hi
hello

let's say file one is a masterfile, and i wanted to print out string that are in file1 and NOT in file2.. how do i do that? i'm not able to use the diff command as DIFF COMMAND compares line one in file1 with line one in file2.

kindly assist



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 20, 2008 at 11:18:01 Pacific
Reply:

Take a look at the Unix comm command, select or reject lines common to two files.


0

Response Number 2
Name: James Boothe
Date: March 20, 2008 at 12:06:49 Pacific
Reply:


comm -23 file1s file2s


That comm solution (like diff) does require sorting both files first.


And also like diff, this is a line-per-line matchup.  If file2 has
a single line "hello", and file1 has two or more of those lines, the
comm command will match up and ignore one pair, but the additional
hello lines in file1, being unmatched, would print.


And that may be what you want.


Following is a solution that does not require sorting the files, and
if a line exists in file2, that will cause all occurrences of that line
in file1 to be ignored.


awk 'BEGIN {
while ((getline < "file2") > 0)
list[$1] = 1}
!list[$1]' file1


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: comparing files

Compare file names then move www.computing.net/answers/unix/compare-file-names-then-move-/7257.html

Comparing all files in current dir www.computing.net/answers/unix/comparing-all-files-in-current-dir/4102.html

Comparing file times. www.computing.net/answers/unix/comparing-file-times/4256.html