Extract lines whose third field is 0

Hi,
I have a file with colon separated values like below. How can i get those lines whose third field is 0 (zero). In the below example, lines starting with stapler and tempo has its third field as 0

$ cat list.txt
galaxy:b:5:world
stapler:a:0:hello
abc:a:4:stomper
kepler:uic:5:jam
tempo:havana:0:john

u could have done it in many ways....

 
awk -F":" '$3=="0"{print}' filename
1 Like

Thank you vidyadhar. Can this be done without awk ?

Lots and lots and lots of ways.

If awk will not do, presumably there's other ways that won't do too. So instead of playing 20 questions, can you just tell us what you want?

Using grep:

grep '^.*:.*:0' list.txt

Greedy matching will cause this to give unexpected results. Check this:

echo 'HI:THERE:YOU:THERE:0'|grep '^.*:.*:0'
HI:THERE:YOU:THERE:0

echo 'HI:THERE:0:YOU:THERE:0'|grep '^.*:.*:0'
HI:THERE:0:YOU:THERE:0

Hi,

Check this out,

perl -F':' -ane 'if($F[2]==0){print;}' file

Cheers,
Ranga :slight_smile:

Yes elixir_sinari, got a bit greedy there.
This should tighten it up (assuming no special chars in file other than the ':' delimiter):

grep '^[a-zA-Z0-9]*:[a-zA-Z0-9]*:0' list.txt

elixir_sinari
Registered User

Greedy match grep:

grep '^[^:]*:[^:]*:0:' infile

Why can awk not be used?

awk -F: '$3==0'