LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2007, 12:09 AM   #1
anhtt
LQ Newbie
 
Registered: Jul 2007
Posts: 27

Rep: Reputation: 15
shell script: compare 2 files


I have 2 files. Now I want write 1 shell script which check one bye one row in file1. If the row of file1 is found in the file2, we won't do anything. Otherwise, we will save the row of file1 in a new file ( file3).
I also wrote a shell script. But it is so slow because there are a lot of rows in file1 and file2.

cat file1 | while read line;
do
echo $line > /tmp/check
x=`cut -f1 -d" " /tmp/check`
y=`grep -c "$x" file2
if [ "$y" -eq 1 ];then
continue
else
echo $x >> file3
fi
done


Help me !
 
Old 08-29-2007, 12:12 AM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
It's probably easier to use one of the sdiff, diff or cmp commands. Have you tried them before?
 
Old 08-29-2007, 12:41 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
Code:
for line in `cat t1.t`
do
        rslt=`grep $line t2.t`
        if [[ $? -ne 0 ]]
        then
                echo $line >>t3.t
        fi
done
 
Old 08-29-2007, 02:49 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'FNR==NR{ arr[$0];next}
     {if ($0 in arr) print "found"
      else print "not found"   
     }' "file1" "file2"
 
Old 08-29-2007, 03:04 AM   #5
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Rep: Reputation: 15
Quote:
Originally Posted by gilead View Post
It's probably easier to use one of the sdiff, diff or cmp commands. Have you tried them before?
I agree with this, sdiff will mark all changes with a "<", ">", or a "|". use the equivalent of a grep -v for those three things and then cut out one of the columns and you have exactly what you want. unfortunately right now I am too tired for code thats any better than stupid slow and inefficient at the moment so you'll need to figure out the details on your own.
 
Old 08-29-2007, 03:33 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
If the order does not matter, and all rows in each file are unique (no two identical rows in the same file), then the fastest is probably:
Code:
cat file1 file2 | sort | uniq -d | cat file1 - | sort | uniq -u
which would only display lines from file1 which are not in common between file1 and file2. If you want the reverse (lines in common), then it is simpler:
Code:
cat file1 file2 | sort | uniq -d
If the above conditions are false (identical rows or important order), then to some extent (some thousands of lines), this should be a good solution:
Code:
grep -vxf <(sed 's/[]\.*^$[]/\\\0/g' file2) file1
which gives lines in file1 that are not in file2.

To output the result to a "file3" file, just append
Code:
 >file3
to any of the above commands.

Yves.

[edit:]I had forgotten the 'f' option in grep. Now it is ok.[/edit]

Last edited by theYinYeti; 08-29-2007 at 03:47 AM.
 
Old 08-29-2007, 03:39 AM   #7
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613Reputation: 613
comm is the program you want -does exactly what you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 09:09 AM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 05:24 AM
shell script to compare filese b/w local and remote and delete files from local serve dsids Linux - Networking 9 08-23-2006 08:20 AM
compare files in C shell shashwat.gupta Programming 8 05-24-2006 03:13 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration