0

I have a file (temp_4n.in) with the following lines:

 5  C   172.0696  
 5  C   135.6942  
 5  H   30.9062  
 5  H   31.8124  
 12 H   31.0377  
 12 O   293.0612  
 12 H   32.4678  

I have a script (testsc.txt) that will extract the last H for the lines starting with a certain number which I will change regularly:

 grep $'^${mold}\tH' temp_4n.in | tail -1 > temp_5n.in

However, when I run the script (wanting, for example, the line starting with 12) using the following command, it fails:

 qsub -v mold=12 ./testsc.txt

I think this has something to do with the first '$' in the grep command being regarded as a parameter and erroneously being substituted. Can anyone help me figure out what the problem is?

1

qsub -v varname=value sets varname to the value provided in the environment of the job submitted.

In the script that you submit, you use this value in a grep command, but the string that contains the variable is quoted with single quotes, preventing the shell from expanding it.

Your script should instead be using

grep "^$mold[[:blank:]]H" temp_4n.in | tail -1 > temp_5n.in

The initial $ in your code ($'...') is not needed here and instead we use [[:blank:]] to match a space or tab.


An alternative would be to use awk in your script:

awk -v mold="$mold" '$1 == mold && $2 == "H" { this = $0 } END { print this }' temp_4n.in >temp_5n.in

This uses string comparisons (not regular expressions) to test the first and second column, and when a matching line is found the whole line is saved in the variable this. At the end, the last value of this is printed. This replaces both of grep and tail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.