How to print Dissimilar keys and their values?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print Dissimilar keys and their values?
# 1  
Old 05-28-2009
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  
Old 05-28-2009
Quote:
Originally Posted by repinementer
The script I' trying to design has to do some thing like thisvfor dissimilar ones
so where is it, your designed code?
# 3  
Old 05-28-2009
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  
Old 05-28-2009
Quote:
Originally Posted by repinementer
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  
Old 05-28-2009
Code:
awk 'FNR==NR{a[$1]++;next}!a[$1] {print $0}' a.txt b.txt


-Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculate average from a given set of keys and values

Hello, I am writing a script which expects as its input a hash with student names as the keys and marks as the values. The script then returns array of average marks for student scored 60-70, 70-80, and over 90. Output expected 50-70 1 70-90 3 over 90 0 The test script so far... (4 Replies)
Discussion started by: nans
4 Replies

2. Shell Programming and Scripting

Generate separate files with similar and dissimilar contents

Hello experts, I have 2 files 1.txt (10,000 lines of text) and 2.txt (7500 lines of text). Both files have similar as well as dissimilar entries. Is there a way(s) where i can perform the following operations : 1. Generate a file which will have all similar lines. 2. Generate a file which... (7 Replies)
Discussion started by: H squared
7 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Extract values of duplicate keys

I have two questions that are related, so it would be great if you can help me with both! Question1: I have a file A that looks like this: a x b y b z c w I want to get something like: a x b y; z c w Given that a,b,c has no spaces. But the other letters might contain spaces. ... (2 Replies)
Discussion started by: Viernes
2 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

Deleting keys and values-Awk

key pair is 1st and 6th column ex:a20 : p10 or a20 : p11 For every key pair if the vlaue(4th column) is the same then delete all the lines who has keypair and the value ex: a20 : p10 has value 1 only then delete those but a20 : p11 has different values 1,2 and 3 and keep those. input a20 ... (8 Replies)
Discussion started by: ruby_sgp
8 Replies

7. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

8. Shell Programming and Scripting

comparing the values of repeated keys in multiple columns

Hi Guyz The 1st column of the input file has repeated keys like x,y and z. The ist task is if the 1st column has unique key (say x) and then need to consider 4th column, if it is + symbol then subtract 2nd column value with 3rd column value (we will get 2(10-8)) or if it is - symbol subtract 3rd... (3 Replies)
Discussion started by: repinementer
3 Replies

9. Shell Programming and Scripting

select values based on keys

HI The input 1st column has specific keys like 1 with value a,b and c. 2 with b,b,d and 3 with a,a a. when ever c appears as one of the values the result will be key ........ c (You can see in the out put as 1 w...... 6.... c) and same follows for d. Thanx:) I'm learning awk scripting. If... (3 Replies)
Discussion started by: repinementer
3 Replies

10. UNIX for Dummies Questions & Answers

arrow keys / special keys

how to use the arrow keys in shell scripting. is there any special synatax / command for this. i just want to use the arrow keys for navigation. replies appreciated raguram R (3 Replies)
Discussion started by: raguramtgr
3 Replies
Login or Register to Ask a Question