Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk array in regular expression

Tags:

awk

I am trying to search a pattern stored in awk array "a" read from file1 and then search in 4rth column of file2 , the following command works perfectly

awk -F" " 'NR==FNR{a[NR]=$1;next}{for (i in a){ if($4 ~ a[i])print $0}}'  file1 file2 

but If I change the command at if to search at the start of 4rth column of file2 as shown below , it does not work , any suggestions please ?

awk -F" " 'NR==FNR{a[NR]=$1;next}{for (i in a){ if($4 ~ "^a[i]" )print $0}}'  file1 file2 

can the second command be modified to search the array element at the start of 4rth column in file2 ?

like image 465
Vicky Avatar asked Dec 07 '25 08:12

Vicky


1 Answers

Change "^a[i]" to "^"a[i]......

like image 139
Ed Morton Avatar answered Dec 11 '25 20:12

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!