extract a field by logic


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract a field by logic
# 1  
Old 01-27-2009
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 over this file.

Thanks,

- CB
# 2  
Old 01-27-2009
Code:
 grep '^1|' test.log | awk -F '|' '{ print $2 "|" $3 }'

# 3  
Old 01-27-2009
Hammer & Screwdriver Perhaps this approach

Code:
> cat file150
2|34|534|
1|355|54|
1|443|34|
3|43|768|
3|7|887|
1|9|990|
> awk -F"|" '$1=="1"{print $2"|"$3}' file150
355|54
443|34
9|990

# 4  
Old 01-27-2009
Okay, so what if I want to search for that '1' in the sixth column, and extract 8 and 9th columns.

- CB
# 5  
Old 01-27-2009
got it...

awk -F"|" '$6=="1"{print $8"|"$9}' tmp.out

where the tmp.out file has 9 columns.

Thx.

- CB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: John K
8 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Extract a certain field from a CSV?

EDIT: This problem has been solved thanks to the help of scottn. Okay, so I have a CSV. Let's say it has the following entries in it: Jackie Chan,1954,M Chuck Norris,1930,M Bruce Lee,1940,M How would I, for example, extract the gender out of a certain person, maybe based on the year of... (12 Replies)
Discussion started by: chickeneaterguy
12 Replies

5. 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

6. Shell Programming and Scripting

how to extract last occurence of the field

path = /first/second/third/fourth. i want to extract /first/second/third from path.my program something like this .... path=/first/second/third/fourth noc=`echo $path|tr '/' '\n'|wc -w` var='echo $noc|cut -d'/' -f 1-$noc --> is giving error. why $noc is not working here.any other... (3 Replies)
Discussion started by: kcp_pavan
3 Replies

7. UNIX for Dummies Questions & Answers

logic needed to extract lines

Hi All, Are there any logic to extract the only lines between first two *TM* (which is marked in blue)? VOL1HST99 0 HDR1A999999S 999999HST99 HDR2F001200012001 UHL1 0729609000001 000000 *TM*^^^^^^^^^^^^^^^^^^^^^^ 01012610252171017301805000... (2 Replies)
Discussion started by: ganapati
2 Replies
Login or Register to Ask a Question