3

I have the following AWK command which works well.

awk -v RS="\n+?[$]{4}\n+?" '/HMDB0000008/' test.xt

However what I want is to be able to call it from the command line and pass an argument so that it can find what I want.

For example, something like this:

awk -f var=HMDB0000008 RS="\n+?[$]{4}\n+?" '/$var/' test.txt

However this does not seem to work. Can you tell me what would be the best way?

Thank you

3
5

awk doesn't use $ to expand variables, additionally they are passed with the -v option not -f. Also strings within /.../ will be treated as a regex pattern so you need to use the ~ pattern match operator against the entire line ($0):

awk -v var=HMDB0000008 -v RS="\n+?[$]{4}\n+?" '$0 ~ var' test.txt
2
  • 2
    Also awk regexps don't have perl's +? operator. Even if it was supported, it wouldn't make much sense here, the first one would make no difference compared the + and the second is superflous as a non-greedy \n+? followed by nothing is the same as just \n. (using a regexp as RS is also not standard/portable) Nov 25 '20 at 19:19
  • 1
    See also index() to find substrings. Nov 25 '20 at 19:20

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