awk pattern match not printing desired columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pattern match not printing desired columns
# 1  
Old 06-21-2014
awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below:

Code:
awk -F, 'NR==FNR {a[$1,$2]=$0; next} ($12,$4) in a {print $12,$1,a[$1,$2]}' OFS="," file4.csv file3.csv

but the code does not print the entire row from file4 in addition to column 12 and 1 of file3.

file4:
Code:
o,c,q,co,ov,b
1190530,9115,1,108500080,5,93904
1194044,9909,1,107127979,1,6732
1197502,3203,1,106414464,0.75,13474
1198271,5558,1,107120272,1.5,5072
1198272,5558,1,107120272,1.5,5072
1198273,5558,1,107120272,1.5,5072
1198274,5558,1,107120272,1.5,5072
1198275,5558,1,107120272,1.5,5072
1199256,4401,1,105100050,2,13791

file3:
Code:
id,ch,de,c,co,br,da,ps,pm,pq,pa,o,ma,rt,rep,od
86246,205,7,707,1078778070,12564,2012-03-02,12,OZ,1,7.59,1208251,34,5,t,2013-04-24
86246,205,63,6319,107654575,17876,2012-03-02,64,OZ,1,1.59,1208251,34,5,t,2013-04-24
86246,205,97,9753,1022027929,0,2012-03-02,1,CT,1,5.99,1208251,34,5,t,2013-04-24
86246,205,25,2509,107996777,31373,2012-03-02,16,OZ,1,1.99,1208251,34,5,t,2013-04-24
86246,205,55,5558,107684070,32094,2012-03-02,16,OZ,2,10.38,1208251,34,5,t,2013-04-24
86246,205,97,9753,1021015020,0,2012-03-02,1,CT,1,7.8,1208251,34,5,t,2013-04-24
86246,205,99,9909,107127979,6732,2012-03-02,16,OZ,1,2.49,1194044,34,5,t,2013-04-24
86246,205,59,5907,102900020,2012,2012-03-02,16,OZ,1,1.39,1208251,34,5,t,2013-04-24
86246,205,9,9909,107127979,9209,2012-03-02,4,OZ,2,1.5,1194044,34,5,t,2013-04-24

However, the output does not print the entire line of the first file
Code:
o,id,
1194044,86246,
1194044,86246,

What am I doing wrong?

Thanks

Last edited by bkane3; 06-21-2014 at 01:28 AM..
# 2  
Old 06-21-2014
Change:
Code:
awk -F, 'NR==FNR {a[$1,$2]=$0; next} ($12,$4) in a {print $12,$1,a[$1,$2]}' OFS="," file4.csv file3.csv

to:
Code:
awk -F, 'NR==FNR {a[$1,$2]=$0; next} ($12,$4) in a {print $12,$1,a[$12,$4]}' OFS="," file4.csv file3.csv

This User Gave Thanks to Don Cragun 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

Comparing two columns in two files and printing a third based on a match

Hello all, First post here. I did not notice a previous post to help me down the right path. I am looking to compare a column in a CSV file against another file (which is not a column match one for one) but more or less when a match is made, I would like to append a third column that contains a... (17 Replies)
Discussion started by: dis0wned
17 Replies

2. Shell Programming and Scripting

Help! Printing out CSV output from awk Pattern Match

Hi, I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file. So basically the input file format is like this:... (1 Reply)
Discussion started by: An0mander
1 Replies

3. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

4. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

5. Shell Programming and Scripting

AWK command to cut the desired header columns

Hi Friends, I have a file1 i want to retrieve only the fields which have DEP,CITY,TRANS as headers in other file. Output: I want to give the input as DEP,CITY,TRANS column names to get the output. i used cut command .. but if i have 300 fileds it is more difficult to... (4 Replies)
Discussion started by: i150371485
4 Replies

6. UNIX for Dummies Questions & Answers

Printing nth and n+1th line after a pattern match

Hi , I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed :) (6 Replies)
Discussion started by: saj
6 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

9. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

10. UNIX for Dummies Questions & Answers

awk printing all columns after (but including) $n

I am piping an "ls -l" to awk so that all it returns is the file size, date, and file name. The problem is that some files may have spaces in the name so awk is only printing the first word in the file name. I won't know how many space-delimited words are in the filename, so what I want to do is... (2 Replies)
Discussion started by: cassj
2 Replies
Login or Register to Ask a Question