Awk comparision


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk comparision
# 1  
Old 10-17-2012
Awk comparision

Hi Everyone

I am new to Unix shell scripting

Can anyone please explain me in detail how this command works
Code:
 
awk -F@ 'NR==FNR{A[$1]=$2;next}$3 in A{$3=A[$3]}1'  file2 file1

The above command I got it from this forum, but unable to implement it as I am not getting how this worksSmilie

I have two files abc.txt and xyz.txt

abc.txt
Code:
 
1,2,3,4
5,6,7,8
9,0,1,2

xyz.txt
2,1
6,2


2nd column of first file should be compared with 1st column of second file, it match is found, 4th colmn of first file should be updated with the second column of the second file

Output should be
Code:
1,2,3,1
5,6,7,2
9,0,1,2

Please provide me command and explaination.

Thank you for your help in advance.


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 10-17-2012 at 01:56 PM..
# 2  
Old 10-17-2012
Code:
awk -F',' 'NR==FNR{A[$1]=$2;next}$2 in A{$4=A[$2]}1' OFS=, file2 file1

These 2 Users Gave Thanks to balajesuri For This Post:
# 3  
Old 10-17-2012
Please use code tags. Otherwise, the admins will chase you down. I am a pure biologist, I tried to work on your task using the following commands. But, in awk there is one single liner, to do your task.

Check these commands. Hope they help

Code:
$ cat 1
1,2,3,4
5,6,7,8
9,0,1,2

Code:
$ cat 2
2,1
6,2

Code:
awk -F, '{print $1"\t"$2"\t"$3"\t"$4}' 1 > temp && mv temp 1

Code:
awk -F, '{print $1"\t"$2}' 2 > temp && mv temp 2

The above commands will replace comma with tab.

The changed files will look like this.

Code:
$ cat 1
1	2	3	4
5	6	7	8
9	0	1	2

Code:
$ cat 2
2	1
6	2

Join can be used for your task.

Code:
$ join -12 -21 1 2 | awk '{print $2"\t"$1"\t"$3"\t"$5}' > output

Main output file is this

Code:
$ cat output
1	2	3	1
5	6	7	2

---------- Post updated at 12:28 PM ---------- Previous update was at 12:27 PM ----------

Quote:
Originally Posted by balajesuri
Code:
awk -F',' 'NR==FNR{A[$1]=$2;next}$2 in A{$4=A[$2]}1' OFS=, file2 file1

You were very fast before my lengthy post.

Thanks for the useful command though.
# 4  
Old 10-18-2012
Awk comparision

Thank you balaji ..it works Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk comparision between 2 files and substitution in third

Hi All, I have two files in the following format. File 1 : 1044|1|20121031|2910039.4|MR|201210|G1044|E 1082|2|20121031|1664662.84|MR|201210|G1082|E 1696|3|20121031|190801.5|MR|201210|G1696|E 1824|4|20121031|196350|MR|201210|G1824|E 1900|5|20121031|221447.8|MR|201210|G1900|E File 2 :... (7 Replies)
Discussion started by: nua7
7 Replies

2. Shell Programming and Scripting

awk: string followed by tab comparision

Hi all, Currently i am using if( $0~/ NOT / && $0~/ NULL /) { ................. } to check if the input record contains "NOT" and "NULL". But in some cases "NOT" and "NULL" are preceded and followed by tab. How do i find compare for these fields as well? (3 Replies)
Discussion started by: ysvsr1
3 Replies

3. Shell Programming and Scripting

File comparision with AWK / SED

Hi all I need to compare two separate product lists that are changed weekly. New products are added, old products are removed and prices change. I have found various Windows programs that do this function but it's not as clean as I like and just wondered if there was a simpler way with... (1 Reply)
Discussion started by: mrpugster
1 Replies

4. Shell Programming and Scripting

File Comparision

Hi All, I want to write a script which will compare two files and tell me if the files are different. Actually my files will be same but order of lines will be different,so diff is not working. I have written a script to do this:- while read line; do cnt=`grep -i $line... (6 Replies)
Discussion started by: prasson_ibm
6 Replies

5. UNIX for Advanced & Expert Users

Comparision of two files.

File Structure file1.txt.arch 029429288,1,,,02087400376,N,02087400376,N,0,02087400376,N,0,0,8010,08000151736,U,N,,08000151736,U,20100726111237,20100726111237,0,20100726111651,00004140,16,16,10,N;... (1 Reply)
Discussion started by: ravigupta2u
1 Replies

6. Shell Programming and Scripting

Range of records using comparision \awk

Hi Gurus, I have to fetch the records from a logs as per the time stamp . I am comfortable to use awk and sed in the script . But the logic to fetch the records as per comparison is the problem. $cat my_log.log <Jul 30, 2010 7:01:12 AM EEST> <Error> <WebLogicServer> <Jul 30, 2010 8:04:12 AM... (3 Replies)
Discussion started by: posix
3 Replies

7. Shell Programming and Scripting

File comparision

HI, I would like to know how to compare two files and replace non-matching lines with "_" . I can get non-mathing lines with grep -v -f file1 file2 i just want to knw how to display 'file2' with non-matching lines from 'file1' replaced by "_" for exmaple file1: a b c d ... (2 Replies)
Discussion started by: maddy81
2 Replies

8. Shell Programming and Scripting

while - comparision

Hi, Please find the attached scriplet and suggest me to fix the bug in this. ----------------------------------- noofdirs=`ls *.tar | wc -l` if ; then let i=1 while ( $i <= $noofdirs ) ; do echo $i mkdir $i file1=`ls *.tar | head -1` mv $file1 $i i =... (2 Replies)
Discussion started by: sharif
2 Replies

9. Solaris

date comparision

I am wriitng a shell script to compare a date in `date +%Y%m%d%H%M` to the output of `cat /var/adm/messages.0 | grep Temp |grep "CPU Fans" | cut -f1-4,19 -d' '` which looks like Dec 2 11:30:57 79 Dec 2 11:37:58 79 Dec 2 11:44:58 79 Dec 2 11:50:59 79 Dec 2 11:58:00 79 Dec 2... (1 Reply)
Discussion started by: Hitesh Shah
1 Replies

10. UNIX for Dummies Questions & Answers

Number comparision in AWK

Hi, I have a file like this. "2006","10",25,"U","1129","32","C",0,0,0,0,0,0,0,0,0,0,0,0,352,16,4,0,0,0,0,0,"80",,1 "2006","11",25,"U","1148","32","C",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"80",,2 "2006","14",25,"U","1149","10","C",0,0,0,0,0,0,0,0,0,0,0,0,560,12,0,0,0,0,0,0,"80",,3... (1 Reply)
Discussion started by: vskr72
1 Replies
Login or Register to Ask a Question