Grep special pattern in 3rd column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep special pattern in 3rd column
# 1  
Old 11-01-2012
Grep special pattern in 3rd column

I have a file (test.dat) that has this pattern:

Code:
1000 000001 (92.431343802235503, 90.0)
1000 000002 (87.568656197764497, 80.0)
1000 000003 (150.75815307316083, 150.0)
1000 000004 (29.241846926839159, 20.0)
1000 000005 (110.02128542766, 110.0)
1000 000006 (69.978714572339996, 60.0)
1000 000007 (86.093858223079451, 80.0)
1000 000008 (93.906141776920549, 90.0)
1000 000009 (72.905069709350556, 70.0)
1000 000010 (107.09493029064944, 100.0)

I would like to grep all lines that has 20.0 in the 3rd column. I have tried sorting with the 3rd column this way:

Code:
sort -k3n,3 test.dat > test1.dat

but this does not work. Can someone help me out here
# 2  
Old 11-01-2012
If you want to grep 20 then why you need to sort...?

Quote:
Originally Posted by kayak
Code:
1000 000001 (92.431343802235503, 90.0)
1000 000002 (87.568656197764497, 80.0)
1000 000003 (150.75815307316083, 150.0)
1000 000004 (29.241846926839159, 20.0)
1000 000005 (110.02128542766, 110.0)
1000 000006 (69.978714572339996, 60.0)
1000 000007 (86.093858223079451, 80.0)
1000 000008 (93.906141776920549, 90.0)
1000 000009 (72.905069709350556, 70.0)
1000 000010 (107.09493029064944, 100.0)

Which 20..?
assuming below from 8th column.

Code:
$ awk '$3 ~ /20/' file
1000 000008 (93.906141776920549, 90.0)

and

last column

Code:
$ awk '$NF ~ /20/' file
1000 000004 (29.241846926839159, 20.0)


Last edited by pamu; 11-01-2012 at 10:38 AM..
This User Gave Thanks to pamu For This Post:
# 3  
Old 11-01-2012
Code:
awk -F, ' { if(match($2,/20.0/)>0) print; } ' test.dat

This User Gave Thanks to Yoda For This Post:
# 4  
Old 11-01-2012
using awk:
Code:
cat test.dat|awk '$NF~/20.0)/{print}'

This User Gave Thanks to sheemam For This Post:
# 5  
Old 11-01-2012
Thanks guys, but what I get using:

Code:
awk '$NF ~ /20/' test.dat > test2.dat

or

Code:
cat test2.dat|awk '$NF~/20.0)/{print}' > test3.dat

is a sorting that looks like this:

Code:
1000 000004 (29.241846926839159, 20.0)
1000 000011 (129.32191982771337, 120.0)
1000 000029 (25.261155313843393, 20.0)
1000 000044 (29.490853589302692, 20.0)
1000 000054 (124.62368336572428, 120.0)
1000 000063 (120.87431703483179, 120.0)
1000 000065 (124.55954901314844, 120.0)
1000 000074 (126.54363387282677, 120.0)

I do not want 120.0 but only 20.0 in the sorting. Also I want the brackets and comma removed so that I have four columns at the end. Can someone still help?
# 6  
Old 11-01-2012
Code:
perl -lne '/\s20.0\)$/g && do{s/(\(|\)|,)//g;print;}' inputfile

# 7  
Old 11-01-2012
this should fix ur prob:

Code:
cat test.dat|awk '$NF~/^20.0)/{print}'

This User Gave Thanks to sheemam For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

3. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. Shell Programming and Scripting

Grep correct pattern with special character and variables

cat file time="north_south_east_west_08:00" location="A" start="left" status="ok" end="north" time="north_south_east_west_12:00" location="C" start="right" status="ok" end="south" time="north_south_east_west_23:00" location="G" start="left" status="ok" end="east"... (7 Replies)
Discussion started by: ctphua
7 Replies

6. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

7. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

8. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

9. Shell Programming and Scripting

SED equivalent for grep -w -f with pattern having special characters

I'm looking for SED equivalent for grep -w -f. All I want is to search a list of patterns from a file. Also If the pattern doesn't match I do not want "null returned", rather I would prefer some text as place holder say "BLANK LINE" as I intend to process the output file based on line number. ... (1 Reply)
Discussion started by: novice_man
1 Replies

10. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies
Login or Register to Ask a Question