Print ALL lines except if field is 999

Hi All!!! :slight_smile:

I need a command that will print each line of a text file UNLESS the 3rd field of that line is equal to the number 999. (space seperated fields)

Solaris10/BASH SHELL:

INPUT.TXT

aaa bbb 111 222
ccc ddd 333 444
eee fff 999 555
ggg hhh 666 777
aaa bbb 999 222
ccc ddd 333 444

OUTPUT.TXT

aaa bbb 111 222
ccc ddd 333 444
ggg hhh 666 777
ccc ddd 333 444

The INPUT.TXT file might have 5 or 10 fields, but it is field 3=999 that I dont want. I'm hoping there is a bash shell command (awk?) that could do this.

Thank you so very much!
Take cares!
-aj

Please use code tags as required by forum rules!

Any attempts from your side?

awk '{if $3=999 print $0}' INPUT.TXT > OUTPUT.TXT

I know that probably wont work...but it helps get the idea out there of what we need (hope)

awk '$3 !~ "999"' input.file > output.file

Fixing yours:

awk '{if ($3 != "999") print $0}' INPUT.TXT > OUTPUT.TXT
1 Like

That has many an syntax error plus a logical one. Correcting the syntaxes awk '{if ($3==999) print $0}' file yields

eee fff 999 555
aaa bbb 999 222

and shows the logical one. Try

 awk '$3!=999' file
aaa bbb 111 222
ccc ddd 333 444
ggg hhh 666 777
ccc ddd 333 444
1 Like

NICE!! thanks so much!

@Aia: That would suppress lines with 1999 or 99921234 as well... well, the first, unedited version ...

Hi.

For the augmented test file, data1, named in variable FILE:

aaa bbb 111 222
aaa 999 111 222
999 bbb 111 222
ccc ddd 333 444
eee fff 999 555
ggg hhh 666 777
aaa bbb 999 222
ccc ddd 333 444
ddd eee 9999 555
ddd eee 1999 555
ddd eee 1991 555
jjj kkk 444 999

the GNU grep command:

grepp -P -v '^\w+\s+\w+\s+999\s' $FILE

produces:

aaa bbb 111 222
aaa 999 111 222
999 bbb 111 222
ccc ddd 333 444
ggg hhh 666 777
ccc ddd 333 444
ddd eee 9999 555
ddd eee 1999 555
ddd eee 1991 555
jjj kkk 444 999

on this system:

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
grepp GNU grep 2.5.3

You will need the perl regular expression option compiled into your grep ( I keep 2 around: grep (without), and grepp (with) ), however, it could be done without, the RE needing to be somewhat different.

Best wishes ... cheers, drl