0

Possible Duplicate:
external variable in awk

How do I pass this variable below?

This doesn't work:

fname=testfile.txt
lsof | awk '/deleted/&&/$fname/ {print $4}'  *----no output*

While this works:

lsof | awk '/deleted/&&/testfile.txt/ {print $4}'
3r

marked as duplicate by Mat, manatwork, munish, Renan, glenn jackman Dec 20 '12 at 14:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • use double quotes instead of single quotes – Rahul Patil Dec 20 '12 at 10:50
1

This happens because shell variables are not expanded in single quotes. Use double quotes, like so:

fname=testfile.txt
lsof | awk "/deleted/&&/$fname/ {print \$4}"

Alternatively, you can pass it in as a variable to awk upon invocation using the -v switch.

  • double does not give the expected result – munish Dec 20 '12 at 11:15
  • @munish You're probably missing the backslash escape. – Chris Down Dec 20 '12 at 11:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.