The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - comparing files dbrundrett Shell Programming and Scripting 4 08-22-2008 07:23 AM
Comparing two files guptan Shell Programming and Scripting 5 08-04-2008 05:02 AM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 12:34 AM
Comparing two txt files - pls help jisha Shell Programming and Scripting 5 01-23-2008 01:14 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-08-2007 11:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-19-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Comparing items in 2 files

Hi,

I have 2 files with contents in them and I need to compare each item in each file.

File1:
item4
item5

File2:
item2
item3
item5
item6

The items names can be of different lengths.
If the items in the File1 are not in File2,
delete the missing item in File1.

The resulting File1 in the above example should be:
File1:
item5

How can I do this using shell script? Thanks in advance!
Reply With Quote
Forum Sponsor
  #2  
Old 01-19-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
no need of shell script, a command can do...

egrep "$(< file2)" file1
Reply With Quote
  #3  
Old 01-19-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
Hi, thanks for your help but when I tried to use this command, it say syntax error.

I tried using this command instead:

egrep "(<file2)" file1

but both the contents in file1 is not changed.


Quote:
Originally Posted by mahendramahendr
no need of shell script, a command can do...

egrep "$(< file2)" file1
Reply With Quote
  #4  
Old 01-19-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
$ more file1
item4
item5 hello
item2

$ more file2
item2
item3
item5 hello
item6

$ egrep "$(<file2)" file1
item5 hello
item2

Can you compare the command I have provided and what you typed ??

you are missing "$" symbol... try again.
Reply With Quote
  #5  
Old 01-19-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
This is what I get:

% cat file2
item2
item3
item5
item6
% cat file1
item4
item5

% egrep "$(<file2)" file1
Variable syntax


I am using unix to do this command
Reply With Quote
  #6  
Old 01-19-2006
Registered User
 

Join Date: Dec 2005
Location: London
Posts: 222
ah got it... you are using csh shell or something similar....

either change your current shell to ksh or

write a script as :

#!/usr/bin/ksh

egrep "$(<file2)" file1

I use ksh shell as my default
Reply With Quote
  #7  
Old 01-19-2006
ReV ReV is offline
Registered User
 

Join Date: Jun 2005
Posts: 34
hmm.. but I can't test this command in Unix?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0