Hi everyone,
Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output:
$c = "Tue Dec";
$log = ` awk '/ \$c /' in.txt`;
print $log;
Hi everyone,
Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output:
$c = "Tue Dec";
$log = ` awk '/ \$c /' in.txt`;
print $log;
Try
log=`awk -vpat1="$c" '$0 ~ pat1' in.txt`;
From where you want to take your contents? From the shell?
C="Tue Dec"
LOG=`awk '/'"$C"'/' in.txt`
echo $LOG
or from awk itself?
Thanks pfylnn for your reply.
Its a perl script. Actually my code is as below:
system("lastlog -u user > last.txt");
`sed -n '2p' last.txt > last1.txt`;
$c = `cut -c44-62 last1.txt`;
print $c;
$log = ` awk '/\$c/' in.txt`;
print $log;
Oh, I see. Just remove the backslash before the $c variable:
$log = ` awk '/$c/' in.txt`;
and it will do the work.
hey Pfylnn,
if i reeive backslash it gives me this error:
awk: /Tue
awk: ^ unterminated regexp
sh: line 1: /: is a directory
---------- Post updated at 07:12 AM ---------- Previous update was at 07:08 AM ----------
Hey carloM,
Thanks for ur reply. But the code u suggest gives me this error:
awk: ./OSS20.pl ~ pat1
awk: ^ syntax error
I guess this is because there is a line break in the $c variable (or maybe somewhere else). Have you checked it? If you just asign directly the value "Tue Dec" to $c it will work. So probably there is something different in $c�s contents.
[EDIT] You can 'chomp' $c before using it to remove the line break:
$c = `cut -c44-62 last1.txt`;
chomp $c;
$log = ` awk '/$c/' in.txt`;
print $log;
this way awk will not complain any more.
Thanks pfylnn, I got it. It was because of newline in $c.