1

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 )

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

-.- sry enter already sends the comment... i"ve been going through a lot lot lot of more tries and i guess it is impossible to give awk 2 variables...
Not at all, you can do -v a=1 -v b=2 no problem. Perhaps you're trying to pass the whole array to awk? If so, see this question: stackoverflow.com/q/5400228/2088135
lolz I already tried that and now it works... but now it doesn't create filec -.- i'm losing sanity now my code looks like this: awk -v lipid="POPC" -v threshold="5" '$4~lipid&&$NF>threshold{print >"awkrm.pdb";next}{print > "tmp"}' bilayer_CG_ordered.pdb && mv tmp awk2patch.pdb
If you have a new problem, it is better to ask a new question rather than using the comments beneath an answer. Perhaps you will be able to work out the solution yourself. Either way, the original problem has been solved, so you should accept an answer.
0

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

3 Comments

Are there any advantages to using match over simply removing the /?
Probably not. I don't use awk very often, and forget the minutiae of how it works.
hey, same comment here... I rly think it doesn't work to give 2 variables to awk... tried it several times and in very different constellations... also looked at the stackoverflow questions about awk and variables but i"ve never seen 2 variables... :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.