1834154 Members
2445 Online
110064 Solutions
New Discussion

Awk output redirection?

 
Boudewijn
Occasional Contributor

Awk output redirection?

Hi,

I have a simple awk statement in my script which checks some stuff. If i run the statemtn from the command line i get exactly the output i want.

The problem is when i put it in the script and try to redirect the output to a file. It doesnt work.
awk '{if ( $2 == "Z" ) print $0}

How do i get the output to append to a logfile which i need to declare in a variable in the script or within the awk statement?



7 REPLIES 7
Steven Sim Kok Leong
Honored Contributor

Re: Awk output redirection?

Hi,

Hope I get you right.

Script:
====================================
LOGFILE=/var/adm/syslog/mylog

awk '{if ( $2 == "Z" ) print $0} >> $LOGFILE
====================================

Hope this helps. Regards.

Steven Sim Kok Leong
Mark Greene_1
Honored Contributor

Re: Awk output redirection?

try this:

eval " awk '{if ( $2 == "Z" ) print $0} " >> logfile

--
mark
the future will be a lot like now, only later
Paula J Frazer-Campbell
Honored Contributor

Re: Awk output redirection?

Hi

awk '{if ( $2 == "Z" ) print $0} >> /tmp/logfile


Paula
If you can spell SysAdmin then you is one - anon
Steven Sim Kok Leong
Honored Contributor

Re: Awk output redirection?

Hi,

Btw, I notice that you missed out the ending single-quote in your awk command. Perhaps a typo?

awk '{if ( $2 == "Z" ) print $0}'

Hope this helps. Regards.

Steven Sim Kok Leong
Boudewijn
Occasional Contributor

Re: Awk output redirection?

The problem at the moment is that if i specify a relative PATH it works, but when I declare a variable like LOGFILE="/export/...." and then redirect the output to ${LOGFILE} then i get nothing.
Carlos Fernandez Riera
Honored Contributor

Re: Awk output redirection?


LOGFILE=/export/.....

export LOGFILE

## redirection outside awk
awk ' $2 == "ZZ" { print $0 }' file >> $LOGFILE

## redirection inside awk

awk ' $2 == "Z" { print $0 >> log}' log=$LOGFILE file

unsupported
Boudewijn
Occasional Contributor

Re: Awk output redirection?

Thx for all the help but it was osmthing very VERY stupid. I forgot one directory in my path.

Explains why the relative worked and the absolute path didnt. the files were one dir. up :)

But thx alot anyway!!