1830234 Members
2353 Online
109999 Solutions
New Discussion

Re: Scripting help

 
SOLVED
Go to solution
George Chechakunnil
Frequent Advisor

Scripting help

hello Gurus,

I have a file which has some 10 coloumns. If the value of the 6th field is non zero value then i need to print out first field

STWBK01 2979 WNT FTA 40 0
USTWNA01 2979 WNT FTA 40 0
USTWA448 2979 WNT FTA 40 HI

Now i want the output as

USTWA448

As the 6th field here is not 0 but HI.

what awk combination shall i use to get this?

thanks in advance
George
I am like a small boy picking up pebbles in god's vast shore of knowledge --- Sir Issac Newton
4 REPLIES 4
Sandman!
Honored Contributor
Solution

Re: Scripting help

# awk '$6!=0 {print $1}' file
rmueller58
Valued Contributor

Re: Scripting help

grep HI FILENAME |awk '{print $1}'
Hein van den Heuvel
Honored Contributor

Re: Scripting help

Rex>> grep HI FILENAME |awk '{print $1}'

Hmmm, thanks for playing, but that's just so wrong on so many levels!

1) Why bring our attention back to a problem with a perfect solution?

2) Why pipe through grep when awk is perfectly happy to read the file directly and pretty much designed to do the filtering. ( awk '/HI/{print $1}' FILENAME )

but most importantly...

3) this 'solution' will have false positives for records where other columns happen to have the character sequence HI in them

4) this 'solution' will have false negatives for cases where column 6 is not 0, but not HI either.

The question/requirement was

"If the value of the 6th field is non zero"

Cheers!
Hein.




Peter Nikitka
Honored Contributor

Re: Scripting help

Hi,

Hein, there is a shorter solution:
awk '$6+0 {print $1}' file
and yes, no forced conversion to integer is needed, even this will work:
awk '$6 {print $1}' file

The evaluation of $6 to TRUE will exactly succeed when the condition of Geoerge is met.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"