mercredi 18 avril 2018

Pass variable to awk pattern

I am writing a shell script which calls an awk script and then I take some user input in the BEGIN using getline, and I save the input to some variables.

BEGIN {printf "What's the word?"
getline word < "-"
}

Now, one of these variables is called "word" and I want to use it in another pattern in the script to print all lines containing the word given. I tried something like this:

/(^| )word( |$)/ 

which will print all lines containing the word "word", and I know that it's not gonna work because it's not recognized as being a variable. I'd searched a lot and found patterns starting with

$0~

but it's not working either in my case. Is there a way I could pass a variable to this pattern and print all lines containing the word stored in the variable?

Aucun commentaire:

Enregistrer un commentaire