Command to show unique strings in two files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Command to show unique strings in two files

Shell Programming and Scripting


Tags
compare tools

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-14-2013
Arun_Linux Arun_Linux is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 30 July 2014, 3:50 AM EDT
Location: India
Posts: 93
Thanks: 18
Thanked 8 Times in 8 Posts
Wrench Command to show unique strings in two files

how to display the unique strings in two files using shell script or commands.
I tried diff and cmp but it shows the entire line, i need only the mismatched strings.

File1:

Code:
sat,sun,mon,tue
rose,lilly,lotus
white,red,blue,green,pink

File2:

Code:
sat,sun,mon,tue
rose,sunflower,lotus
white,red,blue,green,pink

output:

Code:
something like this.
lilly (line 2, 2nd column) in file1 doesnt match with file2.

Sponsored Links
    #2  
Old 02-14-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 20 August 2014, 7:37 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Try sth like this..


Code:
awk -F, 'NR==FNR{for(i=1;i<=NF;i++){A[i,FNR]=$i};next}{for(i=1;i<=NF;i++){if(A[i,FNR]!=$i){print A[i,FNR],"(line "FNR","i" Column) in file1 does not match with file2";}}}' file1 file2

Sponsored Links
    #3  
Old 02-14-2013
Arun_Linux Arun_Linux is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 30 July 2014, 3:50 AM EDT
Location: India
Posts: 93
Thanks: 18
Thanked 8 Times in 8 Posts
Pamu: Its throwing error..


Code:
bash-3.00$ awk -F, 'NR==FNR{for(i=1;i<=NF;i++){A[i,FNR]=$i};next}{for(i=1;i<=NF;i++){if(A[i,FNR]!=$i){print A[i,FNR],"(line "FNR","i" Column) in file1 does not match with file2";}}}' file1.txt file2.txt

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

    #4  
Old 02-14-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 20 August 2014, 7:37 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Use /usr/xpg4/bin/awk or nawk on Solaris.
The Following User Says Thank You to pamu For This Useful Post:
Arun_Linux (02-14-2013)
Sponsored Links
    #5  
Old 02-14-2013
Arun_Linux Arun_Linux is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 30 July 2014, 3:50 AM EDT
Location: India
Posts: 93
Thanks: 18
Thanked 8 Times in 8 Posts
Many thanks... It works perfectly... tested on both solaris and linux :-).
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding a text in files & replacing it with unique strings gordom Shell Programming and Scripting 5 01-28-2013 08:35 AM
calculating unique strings values Paulwintech Shell Programming and Scripting 9 02-15-2012 02:54 PM
Script that sorts and show only unique IP linuxgeek Shell Programming and Scripting 6 06-17-2010 03:00 AM
How to count unique strings my_Perl Shell Programming and Scripting 6 07-31-2009 04:28 AM
Finding Unique strings which match pattern tektips UNIX for Dummies Questions & Answers 5 02-16-2009 04:13 PM



All times are GMT -4. The time now is 03:59 PM.