foreach variable used in awk (please help!!)

I have a list of markers, say marker.txt:

rs913257
rs1018390
rs764180

and I need to know which ROW that each marker appears in another map file, say map.txt:

a rs12354060
b rs913257
c rs6650104
d rs2185539
e rs6681105
f rs1018390
g rs764180
h rs12564807
i rs3094315

The result shall return:

2
6
7

My marker_row.csh is like this:

#! /bin/csh -f

foreach marker (`more marker.txt`)
    awk -F, '{FS=" "; if($2==$marker) print NR}' map.txt
end

but returns me nothing!

Please help! Appreciate a lot!!

Hi.

This is in awk (nothing CSH about it):

$ awk 'NR == FNR { A[$2] = NR; next } A[$1] { print A[$1]}' map.txt markers
2
6
7

For both, you could use:

grep -f markers -n file1
2:rs913257
6:rs1018390
7:rs764180

Great. Thanks a lot! Scott. So seems there's no way allowing me to use a 'foreach' variable in 'awk', as the way I did it, right?

awk 'NR==FNR {a[$1]++; next} ($2 in a) {print FNR} ' marker.txt map.txt

The foreach construct you mentioned is a shell construct. In awk, you'd use something like

for( ELEMENT in ARRAY )

Doing this wholly inside awk is quicker than doing it in the shell.

And I'd recommend not using the C-Shell - period - which is why I did it in awk :slight_smile: