String compare


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String compare
# 1  
Old 01-26-2007
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 Smilie


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

Last edited by sbasetty; 01-29-2007 at 03:48 PM..
# 2  
Old 01-26-2007
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  
Old 01-27-2007
If I understand correctly:

Code:
awk 'NR==FNR{gsub(/[ \(\)-]/,"");x[$0];next}
{gsub(/ /,"")}!($0 in x)' file1 file2

Use nawk on Solaris.
# 4  
Old 01-27-2007
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  
Old 01-27-2007
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 Smilie

Last edited by radoulov; 01-27-2007 at 03:03 PM..
# 6  
Old 01-29-2007
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  
Old 01-29-2007
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

String comparsion compare *

Dear all Would anyone tell me how to prevent user from input non asterisk(i.e. *) character via keyboard? #!/bin/ksh targetHour=-1 while ] do echo "Please input target hour": read targetHour done When I execute the above coding, and then input a "j", it return the following... (3 Replies)
Discussion started by: cstsang
3 Replies

3. Shell Programming and Scripting

String compare

Hi, I have file like below, Srinivas Jala Srinivas Jala AA Srikanth ML Srikanth ML KK Vijay Kumar Dha Vijay Kumar Dha JJ i want to compare like "Srinivas Jala" word in same line, if i found i shoud get some like found, or not found. Pls help to get the code. (3 Replies)
Discussion started by: Srinivas.Jala
3 Replies

4. Shell Programming and Scripting

String Compare

Hi i have a bash script which calls a PHP script (comparison_cron.php). The php script prints the string "no_data_to_retrieve" without quotes. Why does the following bash script not leave the while loop? #!/bin/bash cronOutput=$(php /srv/www/vhosts/xyz/httpdocs/comparison_cron.php)... (2 Replies)
Discussion started by: kaphiphi
2 Replies

5. UNIX for Dummies Questions & Answers

How to compare a string with IP

Hi, I have a variable with value tmp2=123.45.175.243, I am taking this value from a network file. In the script I need to check whether the variable has only numerals and .(dot). if ..." ] then printf "SUCCESS\n" else printf "FAILED\n" fi doesnt work, is there a alternate... (1 Reply)
Discussion started by: happyrain
1 Replies

6. Shell Programming and Scripting

Compare String

Hi everybody. I have this comand "/usr/local/check_procs myprocess" which prints "OK: myprocess running" on my console if the process is running and "myprocess not running" if it is not. I am writing a bash script to check if the process is running but am getting an error. Here is my... (3 Replies)
Discussion started by: kanexxx
3 Replies

7. Shell Programming and Scripting

how can i compare te length of the string

how can i campare te length of the string $var = unix if ( $var -eq 4 ) then.. is it right (6 Replies)
Discussion started by: mail2sant
6 Replies

8. Shell Programming and Scripting

compare string in two files

i have two files: file1.txt AA BB DD EE file2.txt AA,AAA BB,BBB CC,CCC DD,DDD EE,EEE FF,FFF i want an output file: file3.txt AA,AAA BB,BBB (2 Replies)
Discussion started by: MiLKTea
2 Replies

9. Shell Programming and Scripting

Compare string to a pattern

I am new to unix and need to learn how to compare a variable $subject to a string pattern. If the variable has the word "Item" in it then it should be true. How do I do this? Currently I am using the Bourne shell but I can also use Korn or Bash. I come from a Rexx background where strings are... (2 Replies)
Discussion started by: jerryte
2 Replies

10. Shell Programming and Scripting

string compare

Hi, I am trying to get an ouput of certain fields from a file: awk '{ print $NF }' portfile 8087 8047 localhost:1117 Now i need to take this output and see if it exists in netstat -a command. How do i check that. Thanks, Gundu (3 Replies)
Discussion started by: gundu
3 Replies
Login or Register to Ask a Question