Vijay
Vijay

Reputation: 67291

Need an awk script or any other way to do this on unix

i have small file with around 50 lines and 2 fields like below

file1
-----
12345   8373
65236   7376
82738   2872
..
..
..

i have some around 100 files which are comma"," separated as below:

file2
-----
1,3,4,4,12345,,,23,3,,,2,8373,1,1

each file has many lines similar to the above line. i want to extract from all these 100 files whose 5th field is eqaul to 1st field in the first file and 13th field is equal to 2nd field in the first file

I want to search all the 100 files using that single file?

i came up with the below in case of a single comma separated file.i am not even sure whether this is correct! but i have multiple comma separated files.

awk -F"\t|," 'FNR==NR{a[$1$2]++;next}($5$13 in a)' file1 file2

can anyone help me pls?

EDIT: the above command is working fine in case of a single file.

Upvotes: 1

Views: 224

Answers (2)

cdarke
cdarke

Reputation: 44394

Here is another using an array, avoiding multiple work files:

#!/bin/awk -f

FILENAME == "file1" {
    keys[$1] = ""
    keys[$2] = ""
    next
}

{
    split($0, fields, "," )
    if (fields[5] in keys && fields[13] in keys) print "*:",$0
}

I am using split because the field seperator in the two files are different. You could swap it around if necessary. You should call the script thus:

runit.awk file1 file2

An alternative is to open the first file explicitly (using "open") and reading it (readline) in a BEGIN block.

Upvotes: 1

dogbane
dogbane

Reputation: 274748

Here is a simple approach. Extract each line from the small file, split it into fields and then use awk to print lines from the other files which match those fields:

while read line
do
   f1=$(echo $line | awk '{print $1}')
   f2=$(echo $line | awk '{print $2}')
   awk -v f1="$f1" -v f2="$f2" -F, '$5==f1 && $13==f2' file*
done < small_file

Upvotes: 0

Related Questions