Extract lines whose third field is 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract lines whose third field is 0
# 1  
Old 09-04-2012
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
Code:
$ cat list.txt
galaxy:b:5:world
stapler:a:0:hello
abc:a:4:stomper
kepler:uic:5:jam
tempo:havana:0:john

# 2  
Old 09-04-2012
u could have done it in many ways....

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

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 09-04-2012
Thank you vidyadhar. Can this be done without awk ?
# 4  
Old 09-04-2012
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?
# 5  
Old 09-05-2012
Using grep:

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

# 6  
Old 09-05-2012
Quote:
Originally Posted by mjf
Using grep:

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

Greedy matching will cause this to give unexpected results. Check this:
Code:
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

# 7  
Old 09-05-2012
perl

Hi,

Check this out,

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

Cheers,
Ranga Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenation lines based on first field of the lines

Hello All, This is to request some assistance on the issue that I encountered until recently. Problem is: I have a pipe delimited file in which some lines/records are broken. Now, I have to join/concatenate broken lines in the file to form actual record to make sure that the count of records... (8 Replies)
Discussion started by: svks1985
8 Replies

2. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Extract lines with min value, using two field separators.

I have a file with two ID columns followed by five columns of counts in fraction form. I'd like to print lines that have a count of at least 4 (so at least 4 in the numerator, e.g. 4/17) in at least one of the five columns. Input file: comp51820_c1_seq1 693 0/29 0/50 0/69 0/36 0/31... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

5. UNIX for Dummies Questions & Answers

Ps output field extract

This is the output from ps -ef cmd . I have to extract the fourth (C) and the seventh (TIME) field root 3932344 3801216 Apr 08 - 0:00 /usr/sbin/rsct/bin/ERrmd root 3997836 1 0 Apr 08 - 0:00 /usr/sbin/uprintfd root 4128894 3801216 0 Apr 08 - 0:09... (3 Replies)
Discussion started by: Anu_1
3 Replies

6. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

7. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

8. Shell Programming and Scripting

Extract Field from a file

I have an input file with content like : 18:51:18 | 217863|Acct 0110855565|RC 17608| 16 Subs| 1596 UsgRecs| 2 Secs| 430 CPUms| prmis2:26213 <MoveUsage d aemon needs to run on this account before it can be billed.> 23:03:30 | 896529|Acct 2063947620|RC 17608| 8 Subs| 148 UsgRecs| ... (9 Replies)
Discussion started by: Rajesh Putnala
9 Replies

9. UNIX Desktop Questions & Answers

Extract third field of third line

Hi, how do I extract the third field of the first line? I know how to get the first line or the third field of a file, but I can't get the single entry. awk 'NR==1' file.txt awk '{print $3}' file.txt Please tell me how to combine these. And how do I set this value into a variable? ... (1 Reply)
Discussion started by: etownbetty
1 Replies

10. UNIX for Dummies Questions & Answers

extract a field by logic

below are the contents of file 'tmp.out': 2|34|534| 1|355|54| 1|443|34| 3|43|768| 3|7|887| 1|9|990| How do I extract the 2nd and 3rd columns of file 'tmp.out' only when the first column equals '1'. Note that this file will be huge; atleast 5million+ rows, so I want to avoid looping... (4 Replies)
Discussion started by: ChicagoBlues
4 Replies
Login or Register to Ask a Question