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
munish
  • 7,987

1 Answers1

2

This happens because shell variables are not expanded in single quotes. Use awk with -v to pass the pattern into awk like so:

fname=testfile.txt
lsof | awk -v pattern="$fname" '/deleted/ && $0 ~ pattern { print $4 }'
Kusalananda
  • 333,661
Chris Down
  • 125,559
  • 25
  • 270
  • 266