0

Using a bash script, I'm trying to insert a variable in an awk argument.

!/bin/bash
while read col; do
    awk -F'\t' '$22~/$col/' 75437_kbart.txt >> titles.txt
done <col.txt

But I don't get result. The variable "col" is a line in col.txt.

In the command line, I get results :

awk -F'\t' '$22~/EBSCOhost.a9h/' 75437_kbart.txt >> titles.txt

Here, "EBSCOhost.a9h" is a line in col.txt file.

I've read the duplicates. All this does not work :

    awk -F'\t' '$22~/$col/' 75437_kbart.txt >> titles.txt
    awk -F'\t' '$22~/var/' var="$col" 75437_kbart.txt >> titles.txt
    awk -v var="$col" -F'\t' '$22~/var/' 75437_kbart.txt >> titles.txt
    awk -F'\t' '$22~/var/' 75437_kbart.txt var="${col}" >> titles.txt

Using the script, I do get a result if I hardcode the $col variable :

    awk -F'\t' '$22~/EBSCOhost.a9h/' 75437_kbart.txt >> titles.txt

The "-v" only seems to work with "BEGIN/END".

How can I change the awk argument to include the line of the text file ?
For ex. '$22~/$col/' => '$22~/EBSCOhost.a9h/'

trogne
  • 3,402
  • 3
  • 33
  • 50
  • 1
    Does the answer here help? https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script – EdmCoff Apr 25 '23 at 16:39
  • This does not work : `awk -v var="$col" -F'\t' '$22~/var/' 75437_kbart.txt >> titles.txt` – trogne Apr 25 '23 at 16:44
  • 1
    Can you show actual output of your error message if one exists? Or the results of `echo`ing the `awk` statement? If `$col` contains spaces or tabs, you're going to need to use `var="$col"` – EdmCoff Apr 25 '23 at 16:48
  • I have no error. I notice that I have no result even if I hardcode the variable : `awk -v var="EBSCOhost.a9h" -F'\t' '$22~/var/' 75437_kbart.txt >> titles.txt` . But this DO work in the script `awk -F'\t' '$22~/EBSCOhost.a9h/' 75437_kbart.txt >> titles.txt`. I want to replace EBSCOhost.a9h with $col... – trogne Apr 25 '23 at 16:53
  • `awk -v var="$col" 'BEGIN {print var}'` , I do see the correct var printed. – trogne Apr 25 '23 at 16:57
  • Use `'$22~var'` instead of `'$22~/var/'`. The slashes imply a literal pattern rather than a variable reference. – Gordon Davisson Apr 25 '23 at 17:36
  • It does not work without the slash : `awk -v var="$col" -F'\t' '$22~var' 75437_kbart.txt >> titles.txt` – trogne Apr 25 '23 at 17:55
  • So it's not possible ? – trogne Apr 25 '23 at 18:24
  • I ran a sample test case (using `-v var="$col"` and `'$2~var'`) with your script and it worked for me on my system. Can you provide sample data where this fails? – EdmCoff Apr 25 '23 at 20:09
  • 1
    Are you sure that `$col` doesn't have anything weird in it, like invisible characters? Try printing it with `printf "%s\n" "$col" | cat -vet` -- there should be a `$` immediately at the end (i.e. `EBSCOhost.a9h$`), but nothing else unusual. If you see anything other than that (especially, anything between the string you expect and the `$`), that indicates a problem in the data. – Gordon Davisson Apr 26 '23 at 02:06
  • Are you sure your input is tab-separated? Create a [mcve] with, say 3 lines and 4 columns of concise, testable sample input and expected output. Now test your script with that and I expect you'll figure out whatever problem you have by yourself. Btw, calling awk in a shell read loop is almost certainly the wrong approach vs just calling awk once. – Ed Morton Apr 26 '23 at 11:16
  • @GordonDavisson I get correct strings, like EBSCOhost.a9h$. – trogne Apr 26 '23 at 18:51
  • It works! : `awk -v var="$col" -F'\t' '$22~var' 75437_kbart.txt >> titles.txt` . Why `/var/` is not working? What do you mean by literal pattern? It does not find the literal "var" either. – trogne Apr 26 '23 at 19:17
  • 1
    By "literal", I mean it's not treating `var` as a variable name, but as a fixed text string. So `awk -v var="EBSCOhost.a9h" ... '$22~var' ...` will search for "EBSCOhost.a9h", but if you add slashes, like `awk -v var="EBSCOhost.a9h" ... '$22~/var/' ...`, it'll search for "var" instead. – Gordon Davisson Apr 30 '23 at 08:15

0 Answers0