You're viewing a comment by CKP and its responses.

CKP Permalink
April 28, 2014, 14:30

I have an input file "myfile" and a shell variable "VAR" whose
contents are shown below:

# cat myfile
Hello World
Hello Universe
# echo $VAR

I need to process all lines in "myfile" which contains pattern "World"
using awk. It works if i do as shown below:

# awk '/World/ { print $0 }' myfile

But i could not use VAR to do the same operation. I tried the
following even knowing that these will not work:

# awk '/$VAR/ { print $0 }' myfile


# awk -v lvar=$VAR '/lvar/ { print $0 }' myfile


# awk -v lvar=$VAR 'lvar { print $0 }' myfile

Please let me know how to match the contents of VAR in awk.

Thanks in advance

Comment Responses

Curtis Wilbar Permalink
May 07, 2014, 16:21

This will work:

awk -v lvar="${VAR}" 'match($0, lvar) { print $0 }' myfile

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "cloud_100": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.