Print only matches


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print only matches
# 1  
Old 03-03-2013
Print only matches

Im not sure how to tell this awk command to only print when there is a match between the two files. Right now its printing the words in quotation even if there is not a match. I tried removing the additional info and it prints blank spaces??
Code:
awk ' {
    IP = $1
    $1 = ""
}
FNR == NR {
    browser[IP] = $0
}
FNR != NR && IP in browser {
         printf "%s %s%s %s%s\n", "Match:" IP, "Request:", browser[IP], "Location:", $0
         
}
' file1 file2 >match.log


Last edited by sectech; 03-03-2013 at 11:09 PM.. Reason: fixed spelling
# 2  
Old 03-03-2013
If you don't show us the contents of file1 and file2, we can only guess at what might be going wrong. Are there any empty lines in file1 and file2?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-04-2013
You got it, in file2 the first line is blank. That was causing it. Thank you for the direction!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print whole line if variables matches

Der colleagues, 4 days I am trying to solve my issue and no success.. Maybe you can give me a clue how to achieve what I need.. So I have two files. file1 example: 1_column1.1 1_column2.1 aaa 1_column4.1 1_column1.2 1_column2.2 ttt 1_column4.2 1_column1.3 1_column2.3 ... (10 Replies)
Discussion started by: nypreH
10 Replies

2. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

3. UNIX for Dummies Questions & Answers

Print Matches to New Columns

Hi all, I have a problem that I'm struggling to resolve. I have two files that look like this: File 1 654654654 3 987987987 2 321321321 1 File 2 14NS0064 654654654 14NS0054 654654654 14NS0032 654654654 14NS0090 987987987 14NS0093 987987987 14NS0056 321321321 As you may notice,... (2 Replies)
Discussion started by: winkleman
2 Replies

4. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

5. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. UNIX for Advanced & Expert Users

Print line if subsrt matches array

Hi Folks! im printing all lines where the characters in position 270-271 match 33|H1|HA|KA|26 so i came up with this #!/bin/bash array=(33 H1 HA KA 26 ) for i in "${array}" do #echo $i awk '{ if (substr($0,270,2)~'/$i/') print; }' $1 >> $1.temp done It works fine . but... (2 Replies)
Discussion started by: phpsnook
2 Replies

7. Shell Programming and Scripting

How to print line if field matches?

Hi all, I got several lines line this a b c d e 1 e a 1 c d e 3 f a b c 1 e 8 h a b c d e 1 w a 1 c d e 2 w a b c d e 1 t a b c d e 7 4 How can I print the line if 1 is the field one before the last field? Basicly this 2 field ? a b c d e 1 e a b c d e 1 t The file I got is... (7 Replies)
Discussion started by: stinkefisch
7 Replies

8. Shell Programming and Scripting

Print xml if value matches

Hello Gurus.... Can any one please let me know how to print entire xml if the vlaue in the xml matches to a variable given. In PERL. example: <rpad:systemId>abc</hcdd:systemId> <rpad:hcdd>1172</hcdd:hcdid> </get:Request></soapenv:Body></soapenv:Envelope> if value macthes 1172 I... (3 Replies)
Discussion started by: thankful123
3 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

print next line if matches a particular word..need help

Hi i need a help for making a script whch can print next line if it matches a particular word like file1 have ename Mohan eid 2008 ename Shyam eid 345 if scipt got Mohan it will print next line (eid 2008) pls help me .......:) (2 Replies)
Discussion started by: anish19
2 Replies
Login or Register to Ask a Question