print contents of file2 for matching pattern in file1 - AWK


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users print contents of file2 for matching pattern in file1 - AWK
# 1  
Old 09-01-2009
print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2.
Also file 2 will either start with A, B or C.
And 3rd column in file 2 is always F2.

When column 2 of file 2 matches file1 column, print all those rows into a separate file.

Here is an example.


file 1:
Code:
100
103
104
108

file 2:

Code:
A|100|F2|hello
B|100|F2|djhbsdhjf
B|100|F2|dksadbkdfd
C|100|F2|djsbdjinldf
A|101|F2|hellodfd
B|101|F2|djhbsdhjdff
B|101|F2|dksadbkdfgd
C|101|F2|djsbdjinlgfg
A|102|F2|hellodfgfd
B|102|F2|djhbsdhjfgf
C|102|F2|djsbdjinlhgf
A|103|F2|hellohggg
B|103|F2|djhbsdhjhjhj
B|103|F2|dksadbkdfdr
C|103|F2|djsbdjinlfgf
A|104|F2|hellofg
B|104|F2|djhbsdhjfgf
B|104|F2|dksadbkhfgg
C|104|F2|djsbdjinlhgh
A|105|F2|hellohgh
B|105|F2|djhbsdhjdsgh
B|105|F2|dksadbkds
C|105|F2|djsbdjinlds
A|108|F2|hello
B|108|F2|djhbsdhj
B|108|F2|dksadbk
C|108|F2|djsbdjinl

OUTPUT:

Code:
A|100|F2|hello
B|100|F2|djhbsdhjf
B|100|F2|dksadbkdfd
C|100|F2|djsbdjinldf
A|103|F2|hellohggg
B|103|F2|djhbsdhjhjhj
B|103|F2|dksadbkdfdr
C|103|F2|djsbdjinlfgf
A|104|F2|hellofg
B|104|F2|djhbsdhjfgf
B|104|F2|dksadbkhfgg
C|104|F2|djsbdjinlhgh
A|108|F2|hello
B|108|F2|djhbsdhj
B|108|F2|dksadbk
C|108|F2|djsbdjinl


I am trying awk...but no luck...here is what i am trying
Code:
awk -v i="1" 'BEGIN { FS="|" }
FR==NR
{
a[i]=$2
if (a[i]==a[i-1]) {  h[$2,i]=$0; i++ }
else { if (i==1) { h[$2,i]=$0; i++;   } 
       if (i!=1) { h[$2,i]=$0; i=1; ; }
       }
       next
}
{
         for (j=1;j<1000;j++) 
         {
         if (h[$0,j]!="") { print h[$0,j]
                          }
          }
          next
                  }' file2 file1   >  ouputfile




************************************************************
i do not want to use for/while unix loops as it is nt efiicient ..........

Last edited by vgersh99; 09-07-2009 at 08:11 PM.. Reason: code tags, PLEASE!
# 2  
Old 09-01-2009
Not awk but this should work:
Code:
perl -nle '{if (/^(\d+)$/) {$x .= "|$1";} else { $y = substr($x,1);print $_ if /[|]$y[|]/ }}' file1 file2

# 3  
Old 09-01-2009
you can get the results this way; it creates a field array and compares value1 in file1 with value2 in file2 and prints the matching values

Code:
nawk '{FS="|"} NF==1 {acc[$1]=1} NF>1 {if( ( $2 in acc ) ) {print $1"|"$2"|"$3"|"$4} }' file1.txt file2.txt

# 4  
Old 09-07-2009
Quote:
Originally Posted by Vi-Curious
Not awk but this should work:
Code:
perl -nle '{if (/^(\d+)$/) {$x .= "|$1";} else { $y = substr($x,1);print $_ if /[|]$y[|]/ }}' file1 file2


this worked but ...can u please explain ?
sorry for the late response

---------- Post updated at 06:00 PM ---------- Previous update was at 05:59 PM ----------

Quote:
Originally Posted by tpietschmann
you can get the results this way; it creates a field array and compares value1 in file1 with value2 in file2 and prints the matching values

Code:
nawk '{FS="|"} NF==1 {acc[$1]=1} NF>1 {if( ( $2 in acc ) ) {print $1"|"$2"|"$3"|"$4} }' file1.txt file2.txt


this worked well. even faster than perl command given above. thank you very much.


by the way wats difference between nawk and awk ?
# 5  
Old 09-07-2009
Code:
nawk -F'|' -v OFS='|' '
  FNR==NR {f1[$1];next}
  $2 in f1' file1 file2



---------- Post updated at 07:12 PM ---------- Previous update was at 07:10 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 6  
Old 09-07-2009
Quote:
Originally Posted by i.scientist
this worked but ...can u please explain ?
Quick/dirty and slightly inefficient but ok....

Code:
perl -nle '<perl expression>' file1 file2

Read each line from file1 and file2 and perform <perl expression> on each line.

Code:
if (/^(\d+)$/) {$x .= "|$1";}

If the line consists of a single integer number, append it to variable x using | as a separator. The ^ is beginning of line, the $ is end of line and (\d+) represents one or more decimal digits. After your file1 example is processed, $x will equal |100|103|104|108.

Code:
else { $y = substr($x,1);print $_ if /[|]$y[|]/ }

If the line is not a single integer (then it will be your entries from file2), strip the first character off of variable x and save what remains in y. This is inefficient because it has to be done for each line that is processed. This places 100|103|104|108 in y. Print the line if it contains any of the strings in variable y located between two | characters.
# 7  
Old 09-08-2009
thanks "Vi curious" for your "perl" explanation.
Appreciated.

bye
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

5. Shell Programming and Scripting

awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with... (6 Replies)
Discussion started by: msmehaffey
6 Replies

6. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

7. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

8. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

9. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

10. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies
Login or Register to Ask a Question