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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print values of a string froma file ayhanne UNIX for Dummies Questions & Answers 2 01-28-2009 09:10 PM
how to print the values of pcpu of any process in unix ronit_ok2000 UNIX for Dummies Questions & Answers 2 11-13-2008 07:00 AM
Awk to print distinct col values anduzzi Shell Programming and Scripting 22 08-18-2008 06:09 PM
Print column names along with values from SQL thana Shell Programming and Scripting 5 07-24-2008 03:33 AM
awk to print mon and max values of ranges Mudshark Shell Programming and Scripting 3 02-02-2006 03:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-28-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158
How to print Dissimilar keys and their values?

Hi guyz I have been using this script to find similar keys in 2 files and merge the keys along with their values. Therefore it prints similar keys by leaving dissimilar. Any one knows how to print only Dissimilar leaving Similar.

Help would be appreciated.

The script I'm using for similar ones is

awk 'FNR==NR{ a[$1]=$0;next}a[$1]!=""{print $1,a[$1]}' file2 file1

the files for the script for similarkeys is
Mac1-/home/>file1
1234
133
1345
134
23
4555
Mac1-/home/>file2
1234 tab kshgjkghj
100 tab dfghjk
23 tab drjghfg
134 tab drkhgjgj
1345 tab djghf
4555 tab khdjhgjfg
13tabjghhdgf
Mac1-/home/l>output

1234 1234 tab kshgjkghj
1345 1345 tab djghf
134 134 tab drkhgjgj
23 23 tab drjghfg
4555 4555 tab khdjhgjfg

The script I' trying to design has to do some thing like thisvfor dissimilar ones

Mac1-/home/l>output

100 tab dfghjk
13 tab jghhdgf
  #2 (permalink)  
Old 05-28-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Quote:
Originally Posted by repinementer View Post
The script I' trying to design has to do some thing like thisvfor dissimilar ones
so where is it, your designed code?
  #3 (permalink)  
Old 05-28-2009
repinementer repinementer is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 158

Code:
awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt

-----Post Update-----

X:thu gireeshkumarbogu$ cat q1.txt
a9 abc
a1 def
a2 ghi
a4 poiu
X:thu gireeshkumarbogu$ cat q2.txt
a1 def
a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt
a1 a1 def
a2 a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt

-----Post Update-----

X:thu gireeshkumarbogu$ cat q1.txt
a9 abc
a1 def
a2 ghi
a4 poiu
X:thu gireeshkumarbogu$ cat q2.txt
a1 def
a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt
a1 a1 def
a2 a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt

Last edited by repinementer; 05-28-2009 at 05:32 AM.. Reason: added code tags
  #4 (permalink)  
Old 05-28-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Quote:
Originally Posted by repinementer View Post
Code:
awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt

-----Post Update-----

X:thu gireeshkumarbogu$ cat q1.txt
a9 abc
a1 def
a2 ghi
a4 poiu
X:thu gireeshkumarbogu$ cat q2.txt
a1 def
a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt
a1 a1 def
a2 a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt

-----Post Update-----

X:thu gireeshkumarbogu$ cat q1.txt
a9 abc
a1 def
a2 ghi
a4 poiu
X:thu gireeshkumarbogu$ cat q2.txt
a1 def
a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt
a1 a1 def
a2 a2 ghi
a6 ghij
X:thu gireeshkumarbogu$ awk 'BEGIN { while (getline < "q1.txt") data[$1]=1 } { if (data[$1]) print $1 "\t" $0; else print "\t" $0 }' q2.txt
you have a previous thread where i showed you how you can read in 2 files with awk ( see post 10).
Experiment with the second awk statement. You just need to invert ($1 in a) part. Give it a shot.
  #5 (permalink)  
Old 05-28-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514

Code:
awk 'FNR==NR{a[$1]++;next}!a[$1] {print $0}' a.txt b.txt


-Devaraj Takhellambam
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 06:47 PM.


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