Dataman
Dataman

Reputation: 3567

Passing bash variable as a pattern to awk

I would like to know how to pass a bash variable as a pattern to awk. I have read several Q/As which tend to answer the same question, however, none of them solved my problem (probably, I am missing something!).

I have several files like this:

1   9909    23121  
1   23121   561510  
3   75879819    75902940  
3   75902940    75916152  
4   75916152    75982212  
3   75982212    75998727  
22  82964754    83232297 
X   59962662    60745473

and I want to create different files each containing same starting pattern and I only want to extract column 2 e.g. a file containing column 2 of all the rows starting with one, the other file containing column 2 of the rows starting with X etc.

My code looks like this:

for x in *.CNVs; do  
   for y in `seq 22` X Y; do  
        awk '/^$y/ {print $2}' $x > freec_${x}_${y}_st.txt;  
   done;  
done;  

I have also tried using -v option like this:

for x in *.CNVs; do  
    for y in `seq 22` X Y; do  
        awk -v pattern="{$y}" '/^pattern/ {print $2}' $x > freec_${x}_${y}_st.txt;  
    done;  
done;

I don't get any error, all the files are created; however, they are all empty! Any suggestion is much appreciated!

[Edit] I found a neater way using grep (before, I was thinking that it cannot be done by grep!):

for x in *.CNVs; do
    for y in `seq 22` X Y; do
        grep "^${y}\s" $x | cut -f2 > ${x}_${y}_st.txt;
    done;
done;

Using grep is helpful since by using \s, I can discriminate between line starting with 2, 21 and 22 etc.

Upvotes: 2

Views: 1677

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80941

Variables aren't expanded in regexp literals (/.../). You need to use the ~ operator and string literals.

awk -v pattern="$y" '$0 ~ "^"pattern {print $2}' "$x" > "freec_${x}_${y}_st.txt"

You also need to be careful with any pattern metacharacters/etc. in the pattern.

Upvotes: 3

Related Questions