1

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
steeldriver
  • 81,074
CJF
  • 13
  • 4
    Isn't it just a matter of quoting the shell expansion? i.e. awk -v v="$var" – steeldriver Nov 10 '17 at 01:12
  • I second steeldriver's comment. Also, can you not replace (index($1,v)!=0) with $1~v? – user001 Nov 10 '17 at 01:15
  • After you lose all the noise words, leaving you with awk '$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
  • Sorry if duplicated. I tried to check. Also, thanks for help consolidating the code. I'm a novice at scripting, so this is very useful. – CJF Nov 10 '17 at 01:55
  • 1
    @G-Man It is not a security question, it is a scripting question. I find the initiative to close it as a dupe of a security question surreal. The relevant security implications could be mentioned as related matter, but an answer to this question would be essentially different as the dupe candidate. – peterh Nov 10 '17 at 06:08
  • @peterh: Have you even looked at the other question? – G-Man Says 'Reinstate Monica' Nov 10 '17 at 06:10
  • 1
    @G-Man Yes. I've seen a 5 page long security-tuned scripting documentation, which includes the required roughly 3-line answer to this question. – peterh Nov 10 '17 at 06:14
  • 1
    [SE] believes in having “canonical” questions. That is the Unix & Linux Stack Exchange definitive canonical reference question on the pitfalls of failing to quote a variable in bash/POSIX shells. Why the author chose to title it that way, I don’t understand; but you have judged a book by its cover — *it* (Stéphane Chazelas’s question) is not a security question; it is a scripting question.  … (Cont’d) – G-Man Says 'Reinstate Monica' Nov 10 '17 at 06:30
  • 1
    (Cont’d) …  Yes, the answer is “You should always quote your shell variable references (e.g., "$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
  • I just saw this question in the Reopen queue and while I agree with closing it as a duplicate, a more relevant duplicate target would be Gilles’ canonical Q&A, https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters – Anthony Geoghegan Nov 10 '17 at 14:23
  • 1
    @AnthonyGeoghegan: Thanks for adding that link.  Perhaps some moderator will see this and add that to the list of duplicate targets. – G-Man Says 'Reinstate Monica' Nov 10 '17 at 23:00
  • In this case, @jthill comment to modify the field separator from whitespace to something else solved the issue. Quoting the variable, though good practice that I accidentally omitted in the question, was not the ultimate issue. – CJF Nov 18 '17 at 14:49

1 Answers1

-1

You have to put $var into double apostrophes also in the awk command, so:

varName="a string with spaces"
awk -v var="$var" '{...your awk code...}'

It is because your awk command will be called by a shell, and most shells, including bash, forget the tokens after variable substitution.

peterh
  • 9,731