Search a file for patterns from another file to another with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search a file for patterns from another file to another with awk
# 8  
Old 12-11-2012
Quote:
Originally Posted by Fundix
I was wondering about
Code:
NR==FNR

and have found that it's a trick to be sure we are reading file1.
To be sure you are reading file2 don't you need to use
Code:
NR!=FNR

?

How can we manage it in case of we need to read more than 2 files ?

Thank You
Good Doubt.. Smilie

See

NR - Number of line read. It is total number of lines read.

FNR - Number of line for current file. Total number of lines read for a current file.

So after finishing first file we don't really need to have condition as NR!=FNR

check..

Code:
$ cat file1
1|AAA|123456
2|BBB|098765432

$ cat file2
-|klk|AAA|$123.00|Qty.2|US
-|opi|EEE|$23.00|Qty.4|US

$ awk 'NR==FNR{print "NR",NR,"FNR",FNR;next}{print "NR",NR,"FNR",FNR}' file1 file2
NR 1 FNR 1
NR 2 FNR 2
NR 3 FNR 1
NR 4 FNR 2

# check this also..

$ awk 'NR==FNR{print "NR",NR,"FNR",FNR,FILENAME;next}{print "NR",NR,"FNR",FNR,FILENAME}' file1 file2
NR 1 FNR 1 file1
NR 2 FNR 2 file1
NR 3 FNR 1 file2
NR 4 FNR 2 file2

Quote:
Originally Posted by Fundix
How can we manage it in case of we need to read more than 2 files ?
To perform awk operation for more than two files please check this..Smilie

https://www.unix.com/shell-programmin...#post302716845

Last edited by pamu; 12-11-2012 at 05:58 AM.. Reason: added info...
This User Gave Thanks to pamu For This Post:
# 9  
Old 12-11-2012
Things are clearer now Smilie

For more than 2 files, your thread remind me that i've read something about it.

A great THANK YOU Smilie
# 10  
Old 12-12-2012
This is rather simple

Code:
awk 'BEGIN{FS=OFS="|"} NR==FNR {a[$3]=$0; next} ($2 in a) {print $1, $2, a[$2]}' file2 file1

# 11  
Old 12-12-2012
Quote:
Originally Posted by royalibrahim
This is rather simple
I don't think there is any other difference than just reading file2 first and then file1..Smilie

so what makes its simple..?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 search patterns from file in another

I would like to grep for aaa and bbb and ccc from one line in file1.txt in any order on a line on file2.txt file1.txt aaa bbb ccc ddd fff ggg hhh ddd jjj jjj cccfile2.txt aaa bbb ccc ddd fff ggg --> output whole line since it matches with aaa bbb ccc of file1.txt aaa ddd jjj hhh --> no... (1 Reply)
Discussion started by: sdf
1 Replies

2. Shell Programming and Scripting

How to search 2 separate patterns from a 2nd file.?

Hi Guys, Need your urgent support please. I have a file with 3 separate strings separated by a comma and 2nd file which has a sentence where I can find these 3 strings. I need to find sentences which do not have these strings and maybe redirect it to a 3rd file. All the 3 strings will occur... (3 Replies)
Discussion started by: danish0909
3 Replies

3. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

4. Shell Programming and Scripting

reading lines from a file between two search patterns

Hi, I am new to shell scripting and is working on a script to extract lines from a log file between two time stamps using awk command. After some research I used following command: awk '/01 Oct 2011/{p=1} /10 Oct 2011/{p=0} p' test.log >> tmp.log This works fine. But now i want to... (3 Replies)
Discussion started by: davidtd
3 Replies

5. Shell Programming and Scripting

to read two files, search for patterns and store the output in third file

hello i have two files temp.txt and temp_unique.text the second file consists the unique fields from the temp.txt file the strings stored are in the following form 4,4 17,12 15,65 4,4 14,41 15,65 65,89 1254,1298i'm able to run the following script to get the total count of a... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

6. Shell Programming and Scripting

Need help in retrieving log from a UNIX file using the search patterns

Hi everyone, I am trying to retrieve certain log from a big file. The log size can be from 200 - 600 lines. I have 3 search patterns, out of which 2 (first and last lines) search patterns are common for all the transactions but 3rd search pattern (occurs in the middle of transaction) is... (5 Replies)
Discussion started by: msrayudu
5 Replies

7. Shell Programming and Scripting

connecting search patterns in awk with AND

Hello friends, I couldnt connect two search patterns in awk, what i want is to search for two words in a log file; "+MB)" and "Done" i use this code /usr/xpg4/bin/awk '/+MB)/ {gsub("\(","",$5);print int($5)}' mylog.txt and i get integer part of (123,45MB) in a log file "mylog" with ... (1 Reply)
Discussion started by: EAGL€
1 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

10. UNIX for Dummies Questions & Answers

How to parameterize multiple search patterns and generate a new file

I have one file: 123*100*abcd*10 123*101*abcd*-29*def 123*100*abcd*-10 123*102*abcd*-105*asd I would like to parameterize the search patterns in the following way so that the user could dynamically change the search pattern. *100* and *- (ie *minus) *102* and *- The output that is... (6 Replies)
Discussion started by: augustinep
6 Replies
Login or Register to Ask a Question