On 4/8/2008 10:00 AM, Kenny McCormack wrote:
> In article <ftg14s$392$1@[EMAIL PROTECTED]
>,
> RC <raymond.chui@[EMAIL PROTECTED]
> wrote:
>
>>RC wrote:
>>
>>>myvariable="hello"
>>>
>>>cat my file, if first field match "hello", then print whole line
>>>
>>>I tried
>>>
>>>cat myfile.txt | awk -F: '$1 ~ /$myvariable/ {print $0}'
>>>
>>>but doesn't work, I also tried double $ like $1 ~ /$$myvariable/
>>>still no luck.
>>
>>I found the answer from
>>http://www.faqs.org/faqs/unix-faq/faq/part3/section-12.html
>>
>>cat myfile.txt | awk -F: '$1 ~ /'"$myvariable"'/ {print $0}'
>>
>>Where '" are single quote and double quotes
>> "' are double quotes and single quote
>
>
> Good for you.
>
> Now, of course, the nitpickers will come along and point out:
> 1) UUOC
> 2) UUO$0
> 3) In the nanny-state, you aren't supposed to do it this way.
> They have this wacky idea that it is safer/better to use -v.
> Hint: It isn't.
>
Yes, it is:
$ var="#"
$ awk 'BEGIN{ print '"$var"' }'
awk: cmd. line:1: BEGIN{ print # }
awk: cmd. line:1: ^ syntax error
$ awk -v var="$var" 'BEGIN{ print var }'
#
Regards,
Ed.


|