For Programmers: Free Programming Magazines  


Home > Archive > AWK > July 2004 > Removing Lines/Not Printing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Removing Lines/Not Printing
groblela

2004-07-08, 3:56 pm

My question concerns not printing (removing) particular lines that match
specified search criteria.

I've tried the following to no avail:
First setting the variable Tgt equal to data provided within a batch file.
Secondly, specifying that if the data in column 7 matches the string
target AND column 6 equals the number provided as ${Tgt}, then do not
print that line.

set Tgt = ${2}
nawk '{if ($7 == /target/ && $6 != ${Tgt}) !print $0}'

What am I doing wrong??
groblela

Bob Harris

2004-07-08, 3:56 pm

In article
< 6097924caade44403f183465df912775@localho
st.talkaboutprogramming.com>,
"groblela" <lee.grobleski@nospam.wpafb.af.mil> wrote:

> My question concerns not printing (removing) particular lines that match
> specified search criteria.
>
> I've tried the following to no avail:
> First setting the variable Tgt equal to data provided within a batch file.
> Secondly, specifying that if the data in column 7 matches the string
> target AND column 6 equals the number provided as ${Tgt}, then do not
> print that line.
>
> set Tgt = ${2}
> nawk '{if ($7 == /target/ && $6 != ${Tgt}) !print $0}'
>
> What am I doing wrong??
> groblela


set Tgt = ${2}
nawk -v Tgt=${Tgt} '
$7 == "target" && $6 != Tgt { next }
{ print }
'
Ian Stirling

2004-07-08, 8:56 pm

groblela <lee.grobleski@nospam.wpafb.af.mil> wrote:
> My question concerns not printing (removing) particular lines that match
> specified search criteria.
>
> I've tried the following to no avail:
> First setting the variable Tgt equal to data provided within a batch file.
> Secondly, specifying that if the data in column 7 matches the string
> target AND column 6 equals the number provided as ${Tgt}, then do not
> print that line.
>
> set Tgt = ${2}
> nawk '{if ($7 == /target/ && $6 != ${Tgt}) !print $0}'
>
> What am I doing wrong??


Shell does not expand ${Tgt} as it's in single quotes.
Either
nawk '{if ($7 == /target/ && $6 != '${Tgt}') !print $0}', or
If nawk accepts the same as gawk, then
nawk -v target=$Tgt '...
I'd write it as
-v number=$Tgt '$7=="target"&&$6==number{next}'

Stepan Kasal

2004-07-09, 3:55 am

Hello,

Ian Stirling wrote:
> nawk -v target=$Tgt '...
> I'd write it as
> -v number=$Tgt '$7=="target"&&$6==number{next}'


or simply

nawk '$7=="target"&&$6==number{next}' number="$Tgt"

In this case, the assignment is performed before the first line is read.

Stepan Kasal

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com