John_K
September 4, 2012, 11:44am
1
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
John_K
September 4, 2012, 11:48am
3
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?
mjf:
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
mjf
September 5, 2012, 8:24am
8
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'