The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
While finding compare the result with given string mr_bold UNIX for Dummies Questions & Answers 4 04-24-2008 09:31 PM
compare string in two files MiLKTea Shell Programming and Scripting 2 03-10-2008 05:35 AM
Compare string to a pattern jerryte Shell Programming and Scripting 2 01-25-2008 08:37 PM
Compare Char to String Phobos High Level Programming 3 04-09-2005 11:01 AM
string compare gundu Shell Programming and Scripting 3 03-23-2005 04:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-26-2007
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
String compare

Hi Friends,

Can anyone help me with comparing the records in twofiles,

I have two files (csv)

FILE1:

1023,SMITH JAMES , (203) 789-1249
1023,HARRY POTTER , (213) 789-1249
1023,JONES D, (903) 789-1249

FILE1:

1023,SMITH ,2037891249
1023,HARRY , 2137891249
1023,JONES, 7037891249

it should return only one row i.e 1023,JONES, 7037891249 as they are different,It has to supress the "(" chareacters and blank ones.




Thanks in advance for your help.

S


(203) 789-1249 and 2037891249,
I have to compare these

Last edited by sbasetty; 01-29-2007 at 03:48 PM..
  #2 (permalink)  
Old 01-26-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,508
here's an idea to start with:
Code:
[root@localhost test]# echo "1023,SMITH , (203) 789-1249" |sed 's/[-() ]//g'
1023,SMITH,2037891249
All ( , ) , - and spaces are stripped. you can do this for both files and then compare them.
Code:
[root@localhost test]# sed -i 's/[-() ]//g' file
[root@localhost test]# sed -i 's/[-() ]//g' file2
[root@localhost test]# diff file file2
3c3
< 1023,JONES,9037891249
---
> 1023,JONES,7037891249
  #3 (permalink)  
Old 01-27-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
If I understand correctly:

Code:
awk 'NR==FNR{gsub(/[ \(\)-]/,"");x[$0];next}
{gsub(/ /,"")}!($0 in x)' file1 file2
Use nawk on Solaris.
  #4 (permalink)  
Old 01-27-2007
nervous nervous is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 55
Quote:
Originally Posted by radoulov
If I understand correctly:

Code:
awk 'NR==FNR{gsub(/[ \(\)-]/,"");x[$0];next}
{gsub(/ /,"")}!($0 in x)' file1 file2
Use nawk on Solaris.
Can you please explain your code.

Thanks in advance.
An awk student.
  #5 (permalink)  
Old 01-27-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by nervous
Can you please explain your code.
[...]
An awk student.
Code:
awk '
# If NR==FNR this is the first file, so get rid of 
#+ the "(",")","-"," " characters ("gsub" is global substitution),
#+ and populate the x array: x[$0].
NR==FNR{gsub(/[ \(\)-]/,"");x[$0];next}
# Otherwise, it's the second file, so 
#+ remove the spaces. Now we have
#+ the right formating. 
{gsub(/ /,"")}
# If the current record is not
#+ previously stored in the x array,
#+ print it (default action).
!($0 in x)' file1 file2
AnOTHER awk student

Last edited by radoulov; 01-27-2007 at 03:03 PM..
  #6 (permalink)  
Old 01-29-2007
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
Thank you all,

Small clarification on this:
How can we use sed on a perticular column (third column in this example),
sed 's/[-() ]//g' is processing all the columns.
I have two files to compare.



[root@localhost test]# echo "1023,SMITH , (203) 789-1249" |sed 's/[-() ]//g'
1023,SMITH,2037891249
  #7 (permalink)  
Old 01-29-2007
sbasetty sbasetty is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 75
Hi Randuolov,

will this command displays only the changed data can you please explain.
when I run this command it is displaying same file with the data.
Thanks


Quote:
Originally Posted by radoulov
Code:
awk '
# If NR==FNR this is the first file, so get rid of 
#+ the "(",")","-"," " characters ("gsub" is global substitution),
#+ and populate the x array: x[$0].
NR==FNR{gsub(/[ \(\)-]/,"");x[$0];next}
# Otherwise, it's the second file, so 
#+ remove the spaces. Now we have
#+ the right formating. 
{gsub(/ /,"")}
# If the current record is not
#+ previously stored in the x array,
#+ print it (default action).
!($0 in x)' file1 file2



AnOTHER awk student
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0