Ueffes
Ueffes

Reputation: 164

Using variables and arrays in awk

I've got quite a long script so far and can't really manage to get this one awk command to work. I've got this for-loop:

echo "$numberoflipids"                  # 1
for (( i=0 ; i<$numberoflipids ; i++ ))
do
    echo "${nol[$i]}"                   # POPC
    echo "$pr"                          # 5
    awk -v lipid="${nol[$i]}" threshold="$pr" '$4~/lipid/&&$NF>threshold{print >"filec";next}{print > "tmp"}' filea && mv tmp fileb
    ## Also tried this:
    # awk '$4~/"'${nol[$i]}'"/&&$NF>"'$pr'"{print >"patch_rmlipids.pdb";next}{print > "tmp"}' bilayer_CG_ordered.pdb && mv tmp patch.pdb
    ## And this works... (giving the exact values)
    # awk '$4~/"POPC"/&&$NF>5{print >"patch_rmlipids.pdb";next}{print > "tmp"}' bilayer_CG_ordered.pdb && mv tmp patch.pdb
done

the third awk command works perfectly fine for me... it searches column 4 in my filea for POPC, copies the lines that exceed 5 in the last column to filec and copies the remaining lines to fileb.

I hope one of you finds the time to read the code and maybe give me some advice in which way I failed to give awk these variables.

PS: My files look like this:

ATOM    624  SC1 SER   288      54.730  23.870  56.950  1.00  0.00
ATOM   3199  NC3 POP   487      50.780  27.750  27.500  1.00  3.18        
ATOM   3910  C2B POP   541      96.340  99.070  39.500  1.00  7.00         
ATOM   4125  W    PW   559      55.550  64.300  16.880  1.00  0.00

(Old post regarding the awk command: bash - check for word in specific column, check value in other column of this line, cut and paste the line to new text file )

Upvotes: 1

Views: 231

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74595

You need to remove the / from around lipid - at the moment you are matching against the literal pattern /lipid/ rather than the contents of the variable.

Change:

$4~/lipid/

to:

$4~lipid

Upvotes: 2

chepner
chepner

Reputation: 531035

Instead of the ~ operator (/.../ requires a literal regex; you can't use variables inside them), use the match function.

awk -v lipid="${nol[$i]}" threshold="$pr" \
  'match($4, lipid) && $NF > threshold {print >"file";next}{print > "tmp"}' filea &&
 mv tmp fileb

Upvotes: 0

Related Questions