I am trying to pass a variable that contains spaces from bash to awk. I have simple a simple script for doing this with single string variables (in which I am searching the first column of the FILE for the variable), for example:
var=string
awk -v v=$var '(index($1, v) !=0) {print}' FILE
This does not work if var="string1 string2"
. I have new searches where the variables are groups of words separated by spaces as in second var. E.g.,
var="string1 string2 string_n"
awk -v v=$var '(index($1, v) !=0) {print}' FILE
awk -v v="$var"
– steeldriver Nov 10 '17 at 01:12(index($1,v)!=0)
with$1~v
? – user001 Nov 10 '17 at 01:15awk '$1~v' v="$var" FILE
, you'll need to use something besides whitespace for a field separator in awk.awk '$1~v' v="$var" FS=. FILE
– jthill Nov 10 '17 at 01:30"$var"
) unless you have a good reason not to, and you’re sure you know what you’re doing.” — but that’s the answer to a thousand questions, and there’s very little benefit to having the same answer duplicated on the site a thousand times — especially if it leads 1000 OPs to ask “Why?” – G-Man Says 'Reinstate Monica' Nov 10 '17 at 06:30