awk compare


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk compare
# 1  
Old 12-12-2013
awk compare

Need some help with this .... pls

File 1
Code:
 
50000972082d9508,vmx_2917_fa3ea
50000972082d9509,vmx_2917_fa3eb
50000972082d9548,vmx_2917_fa3fa
50000972082d9549,vmx_2917_fa3fb
50000972082d9588,vmx_2917_fa3ga
50000972082d9589,vmx_2917_fa3gb
50000972082d95c8,vmx_2917_fa3ha

File 2
Code:
50000972082d9508,50000972082d9508(*)
50000972082d9509,50000972082d9509(*)
50000972082d9549,50000972082d9549(*)
50000972082d9588,50000972082d9588(*)

Output
Code:
 
50000972082d9508,vmx_2917_fa3ea(*)
50000972082d9509,vmx_2917_fa3eb(*)
50000972082d9548,vmx_2917_fa3fa
50000972082d9549,vmx_2917_fa3fb(*)
50000972082d9588,vmx_2917_fa3ga(*)
50000972082d9589,vmx_2917_fa3gb
50000972082d95c8,vmx_2917_fa3ha

If column 1 of file2 matches column 1 of file1 , append/suffix column 2 of file1 with a (*)

Thanks !
# 2  
Old 12-12-2013
Code:
awk -F, 'NR==FNR{A[$1]=$0;next}A[$1]{$NF=$NF"(*)"}1' OFS=, file2 file1

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-12-2013
OR
Code:
$ awk -F, 'FNR==NR{A[$1];next}($1 in A){$NF=$NF"(*)"}1' OFS=\, file2 file1

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 12-12-2013
Both options work ... Thanks Guys !!

Is there any way to do a "case in-senstive comparison "
# 5  
Old 12-12-2013
Quote:
Originally Posted by greycells
Both options work ... Thanks Guys !!

Is there any way to do a "case in-senstive comparison "
just converting into either upper or lower case then compare

Code:
$ awk -F, 'FNR==NR{A[tolower($1)];next}(tolower($1) in A){$NF=$NF"(*)"}1' OFS=\, file2 file1

Code:
$ awk -F, 'FNR==NR{A[toupper($1)];next}(toupper($1) in A){$NF=$NF"(*)"}1' OFS=\, file2 file1

This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare two column using awk?

Hi team, I have below sample file. It has 4 columns. I want awk script to compare column2 and column4 row by row and print result in new column5 If value matches then result should be MATCHED if not then result should be NOT MATCHED Input file as below UDC_MSISDN,UDC_NPREFIX... (9 Replies)
Discussion started by: shanul karim
9 Replies

2. Shell Programming and Scripting

[awk] Compare two files

HI!! I am trying to compare two files using AWK but I have some problems. I need to count how many times letters are used in two texts. This is my script { long=length($0) for (i=1;i<=long;i++) { aux=substr($0,i,1) if ( aux != " " && aux != "" ) ... (7 Replies)
Discussion started by: ettore8888
7 Replies

3. Shell Programming and Scripting

awk compare and keep uniq

Hi all I was wondering if you may help me in resolving an issue. In particular I have a file like this: the ... represent different string and what I wrote Cur or Ent are the constant. Well, what I would like to obtain is a file in which are reported only the ID in which the second column... (6 Replies)
Discussion started by: giuliangiuseppe
6 Replies

4. HP-UX

Awk compare two files

Hi guys, I have 2 files: File1 ABC|2203|115.50 ABC|2288|328.12 ABC|2289|611.09 ABC|2290|698 DEF|1513|721.3 DEF|1514|40 DEF|1515|5 File2 ABC|2288|328.12 ABC|2289|666.08 ABC|2290|698.00 DEF|1513|721.30 (3 Replies)
Discussion started by: Eduardo Aceves
3 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

7. UNIX for Dummies Questions & Answers

Using AWK to compare 2 files

Hi How can I use awk to compare specific columns in 2 files and print the difference. I currently have this: BEGIN { OFS = FS = "," } NR == FNR { b = $3 next } { e = "" for (x in b) { if (match ($1, x)) { if (RSTART == 1 && RLENGTH > length(e)) { e=x (2 Replies)
Discussion started by: ladyAnne
2 Replies

8. Shell Programming and Scripting

Compare two files using awk

Hi. I'm new to awk and have searched for a solution to my problem, but haven't found the right answer yet. I have two files that look like this: file1 Delete,3105551234 Delete,3105551236 Delete,5625559876 Delete,5625556789 Delete,5625553456 Delete,5625551234 Delete,5625556956... (8 Replies)
Discussion started by: paul.o
8 Replies

9. Shell Programming and Scripting

awk compare 2 files

Hi i hope some awk gurus here can help me.. here is what i need i have 2 files: File1 152445 516532 405088.pdf 152445 516533 405089.pdf 152491 516668 405153.jpg 152491 520977 408779.jpg 152491 0 409265.pdf File2 516532 /tmp/MainStreet_Sum09_Front_FNL.pdf 516533... (9 Replies)
Discussion started by: kenray
9 Replies

10. Shell Programming and Scripting

awk column compare

I've been banging my head against the wall to accomplish the following. Given two files: File a.txt 12345 hello 32324 there File b.txt 12345 stuff 45454 howdy 32324 joe If column 1 matches between the two files, then print only the entire line of the 2nd file (b.txt in this... (3 Replies)
Discussion started by: tiggyboo
3 Replies
Login or Register to Ask a Question