-2

I want to write a script which replaces 'gene' feature from the 3rd column of the $1 file into 'quant'.

#!/bin/bash
awk -F "\t" '{gsub("gene","quant",$3);print}' $1

The code works well, however I would like to read "gene" as an argument, so how can I specify argument $2 instead of 'gene' in the above code?

Thanks!

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
BoMikGo
  • 57
  • 3
  • 1
    This is an awk question, not a bash question. bash and awk are two different languages. (Just like if you have a bash script that runs `python -c '...some code...'`, any question about what goes in `...some code...` is a question about Python, not bash). – Charles Duffy Mar 18 '20 at 22:11
  • It's not clear if you want to always change $3 to "quant" or only change it if it is exactly `"gene"` or change part of it that's `gene` or something else. [edit] your question to show concise, testable sample input and expected output so we can help you. See [ask] if that's not clear. – Ed Morton Mar 18 '20 at 22:51

1 Answers1

2

Use -v awkvar="$value" to create an awk variable with a given value. Thus:

#!/bin/bash
awk -v orig="$2" -F '\t' '{gsub(orig,"quant",$3);print}' "$1"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441