|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Search a file for patterns from another file to another with awk
Hi all ! I have 2 files: file1: Code:
1|AAA|123456 2|BBB|098765432 ... file2: Code:
-|klk|AAA|$123.00|Qty.2|US -|opi|EEE|$23.00|Qty.4|US ... Output: Code:
1|AAA|-|klk|AAA|$123.00|Qty.2|US I would need to search the 3rd field of file2 for the patterns contained in the 2nd field of file1. And then, for the matching lines, return fields 1 and 2 from file1 followed by the entire line of file2. I tried several variants of the code below but don't know how to define the different fields from different files: Code:
awk 'BEGIN{FS=OFS="|"}{NR==FNR}{a[$2]=++i;next} { if ( $3 in a) {print $0}}' file1 file2Any help would be great, thanks ! |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Code:
for V_STR in `cat file.1`; do
V_F1=`echo $V_STR | awk -F\| '{print $1}'`
V_F2=`echo $V_STR | awk -F\| '{print $2}'`
V_File=`grep $V_F2 file.2` && echo $V_F1"|"$V_F2"|"$V_File
doneCheers, |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
small correction to your code... Code:
$ awk 'BEGIN{FS=OFS="|"}
NR==FNR{a[$2]=$1 FS $2;next} {if ($3 in a) {print a[$3],$0}}' file1 file2
1|AAA|-|klk|AAA|$123.00|Qty.2|USLast edited by pamu; 12-11-2012 at 01:53 AM.. |
| The Following User Says Thank You to pamu For This Useful Post: | ||
beca123456 (12-11-2012) | ||
|
#4
|
|||
|
|||
|
Thanks a lot pamu, it works very well !
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Pamu,
can you please explain your code ? Thank You |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Code:
awk 'BEGIN{FS=OFS="|"} # Set FS=OFS="|"
NR==FNR{a[$2]=$1 FS $2;next} # Reading file1. Create array a and assigns value to a[$2]=$1 FS $2, means a[$2]=$1"|"$2, And next for slipping next line of code.
{if ($3 in a) {print a[$3],$0}}' # Reading file2. Check is $3 present in array a(is $3 present in index of array a) then print a[$3](means $1"|"$2 from file1) and $0(from file2)I hope this helps ![]() pamu |
| The Following User Says Thank You to pamu For This Useful Post: | ||
Fundix (12-11-2012) | ||
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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 |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| reading lines from a file between two search patterns | davidtd | Shell Programming and Scripting | 3 | 10-17-2011 01:48 PM |
| to read two files, search for patterns and store the output in third file | vaibhavkorde | Shell Programming and Scripting | 3 | 04-14-2011 03:48 AM |
| Need help in retrieving log from a UNIX file using the search patterns | msrayudu | Shell Programming and Scripting | 5 | 01-19-2011 11:10 PM |
| Perl - How to search a text file with multiple patterns? | Sp3ck | Shell Programming and Scripting | 2 | 03-25-2009 11:30 PM |
| How to parameterize multiple search patterns and generate a new file | augustinep | UNIX for Dummies Questions & Answers | 6 | 07-30-2003 08:50 AM |
|
|