Grep by column-Please help! | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Grep by column-Please help!

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-19-2006
bobo bobo is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 5 March 2014, 10:47 AM EST
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Data Grep by column-Please help!

This is the sample of a data file that I have. It has 10 columns:

0,285,1,2,5,285,1,2,5,0
0,168,1,1,61,168,1,1,61,1512
0,102,3,1,60,102,3,1,60,1525
0,282,3,3,2,282,3,3,2,1503
0,167,3,4,10,167,3,1,24,1472
0,101,1,0,0,101,1,0,0,0
0,263,3,2,0,263,3,2,0,1470
0,245,2,1,50,245,2,1,50,1425
0,303,1,4,32,303,1,4,32,1470
0,165,2,4,49,165,2,4,49,1490
0,103,2,2,24,102,1,1,15,1512
0,371,2,1,28,371,2,1,28,1512
0,103,2,0,0,103,2,0,0,0
0,48,2,4,5,48,2,4,5,1527
0,101,3,3,55,101,3,3,55,0
0,101,1,2,55,101,1,2,55,0
0,285,1,1,63,285,1,1,63,0
0,289,2,3,38,289,2,3,38,1474
0,203,2,1,39,203,2,1,39,1470
0,102,3,6,51,102,3,6,51,1492
0,306,3,3,47,306,3,3,47,1512
0,189,3,3,38,189,3,3,38,1490
0,305,3,2,6,305,3,2,6,1702
0,316,1,1,16,316,1,1,16,1470
0,53,1,1,23,53,1,1,23,0
0,189,3,2,61,189,3,2,61,0
0,142,3,7,16,142,2,7,16,1603
0,189,3,1,24,189,3,1,24,1471
0,348,3,2,13,348,3,2,13,1512
0,102,2,1,42,102,2,1,42,1473
0,304,2,0,0,304,2,0,0,0
0,18,1,2,55,18,1,2,55,1501
0,102,3,3,57,101,1,3,29,1525
0,348,1,2,18,348,1,2,18,1503
0,205,3,2,35,205,3,2,35,1401
0,17,3,7,58,17,3,9,25,1404
0,165,1,2,32,165,1,2,32,1512
0,103,3,2,48,103,3,2,48,1525
0,169,2,1,42,169,2,1,42,1604
0,55,2,4,12,55,2,4,12,1503
0,198,3,2,40,198,3,2,40,1501
0,236,3,2,45,236,3,2,45,1495
0,362,1,4,49,362,2,4,49,1491
0,205,2,2,52,205,2,2,52,1503
0,75,2,3,49,75,2,3,49,1472
0,292,1,4,42,292,1,4,42,1492
0,12,2,2,27,12,2,2,27,1472
0,103,2,0,0,103,2,0,0,0
0,109,2,4,48,53,1,2,58,1471
0,306,1,4,31,68,3,2,51,1501
0,97,1,1,12,97,1,1,12,0
0,102,1,3,8,102,1,3,8,1512
0,12,2,1,10,12,2,1,10,0
0,371,3,2,37,371,3,2,37,1512
0,44,3,2,32,15,2,1,25,0
0,53,1,0,0,53,1,0,0,0
0,18,1,2,21,292,1,3,49,1472
0,101,1,0,0,101,1,0,0,0
0,190,2,1,55,190,2,3,18,1425
0,101,3,2,32,101,3,2,32,1511
0,302,2,0,0,302,2,0,0,0
0,52,3,2,28,42,1,4,4,1492
0,101,3,3,5,101,3,3,5,1512
0,186,2,2,37,186,2,2,37,0
0,71,3,2,55,262,1,2,44,1471
0,17,2,2,39,17,2,2,39,1471
0,98,3,5,21,98,3,5,21,1470
0,68,3,8,23,68,3,8,23,1501
0,235,3,3,0,235,3,2,52,1474
0,212,3,3,24,212,3,3,24,1405
0,48,2,4,50,48,2,4,50,0
0,235,3,4,47,235,3,4,47,1470
0,166,1,2,22,166,1,2,22,0
0,305,3,2,22,305,3,2,22,1512
0,208,1,3,23,208,1,3,23,1503
0,15,2,8,34,15,2,8,34,1471
0,265,2,0,0,265,2,0,0,0
0,190,3,5,46,190,3,5,46,1473
0,188,1,2,56,188,1,2,56,0
0,265,2,2,31,265,2,2,31,1503
0,43,3,1,42,80,2,1,47,1473
0,188,1,2,3,188,1,2,3,0
0,101,3,3,9,101,3,3,9,1503
0,11,3,1,49,11,3,1,49,0
0,316,3,1,63,316,3,1,63,0
0,52,3,3,62,52,3,3,62,1411
0,190,3,2,63,190,3,2,63,1490
0,190,2,2,54,190,2,2,54,1471
0,102,1,6,31,102,1,6,31,1495
0,103,3,2,29,102,2,2,48,1512
0,292,2,4,57,210,2,2,51,1490
0,306,2,2,62,306,2,2,62,1406
0,208,1,4,25,208,1,4,25,1470
0,261,3,4,12,261,3,4,12,1502
0,269,3,4,22,269,3,4,22,1471
0,289,3,2,60,289,3,2,60,1471
0,142,2,6,40,142,2,6,40,1474
0,306,2,4,34,306,2,4,34,1490
0,262,2,2,58,71,2,2,0,1501
0,286,1,1,61,286,1,1,61,1474
0,101,3,2,23,101,3,2,23,1608
0,167,3,2,15,167,3,2,15,1470
0,335,2,1,12,335,3,1,12,1512
0,97,2,2,38,97,2,2,38,1405
0,109,2,4,7,109,2,4,7,0
0,52,2,2,14,52,2,2,14,1501
0,285,2,2,5,285,2,2,5,1473
0,102,2,1,14,102,2,1,14,1607
0,62,2,0,0,62,2,0,0,0
0,83,3,1,21,83,3,1,21,1490
0,52,3,4,14,52,3,4,14,1473
0,181,2,0,0,181,2,0,0,0
0,103,2,2,16,103,2,2,16,1491
0,303,1,3,21,303,1,3,21,1491
0,283,3,2,21,283,3,2,13,1472
0,12,3,2,7,12,3,2,7,0
0,254,2,2,26,254,2,2,26,1474
0,302,3,1,46,302,3,1,46,1472
0,181,2,3,40,53,3,1,18,1413
0,52,1,3,48,52,1,3,48,1431
0,128,3,2,23,128,3,2,23,1605
0,363,3,1,22,363,3,1,22,1472
0,305,3,4,54,305,3,4,54,1501
0,68,3,1,61,68,3,1,61,1490
0,102,1,2,46,101,1,2,31,1702
0,203,1,2,8,203,1,2,8,1474
0,303,1,0,0,303,1,0,0,0
0,236,3,4,50,236,3,4,50,1470
0,97,2,3,10,97,2,3,10,1490
0,205,2,4,8,205,2,4,8,1512
0,142,2,8,29,142,2,8,29,1512
0,203,3,1,59,221,3,1,26,1471
0,8,3,2,29,8,3,2,29,1503
0,205,2,4,57,205,2,4,57,1471
0,170,3,1,27,170,3,1,27,1490
0,304,2,2,40,304,2,2,40,1473
0,191,1,0,0,191,1,0,0,0

how can I GREP a particular data in a certain column. For example, I like to grep the data for column 6 with the vule of 52. I should have :

0 52 3 3 62 52 3 3 62 1411
0 52 2 2 14 52 2 2 14 1501
0 52 3 4 14 52 3 4 14 1473
0 52 1 3 48 52 1 3 48 1431
Sponsored Links
    #2  
Old 04-19-2006
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts

Code:
 awk -F, '{ if ($6 == 52) print $0 }' input.txt

The grep solution looks ugly.


Code:
grep "^[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,52," input.txt

Sponsored Links
    #3  
Old 04-19-2006
amro1 amro1 is offline
Banned
 
Join Date: Mar 2006
Last Activity: 9 September 2010, 5:31 PM EDT
Posts: 119
Thanks: 0
Thanked 6 Times in 5 Posts
Here...

cat file | cut -d "," -f 1,2 | grep 11
The command cat streams file to the standard output and we pipe it to cut command that extracts specific field from the file. "-d" sets the delimiter, in our case it is comma, "-f" species fields (columns) you want to extract (you can change/add your own list of cause) and eventually "grep" selects only these records that feature number 11 (it may be anything again).
Get "UNIX Essentials and UNIX Core" DVD it teaches things like that.

Hope it helps.
    #4  
Old 04-19-2006
x96riley3 x96riley3 is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 20 May 2013, 3:39 PM EDT
Location: Nashville
Posts: 80
Thanks: 0
Thanked 0 Times in 0 Posts
Here is a PERL way. It's not simple but it's fast.

#!/usr/bin/perl

open(F,"/tmp/input") || die "Can't open /tmp/input $!\n";
while(<F>){
($COL1,$COL2,$COL3,$COL4,$COL5,$COL6,$COL7,$COL8,$COL9,$COL10) = split(/,/);
if ( $COL6 == "52" ) {
print "$COL6\n";
} else {
print "52 not found\n";
}
}

-X
Sponsored Links
    #5  
Old 04-19-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 19 April 2014, 7:11 PM EDT
Location: Ashburn, Virginia
Posts: 9,833
Thanks: 42
Thanked 378 Times in 225 Posts
The awk solution will print out records with 52 in field 6. amro1's solution will either print out a column of 11's or it will print out records with 11 anywhere depending on what columns are selected. The perl solution results in stuff like:
52 not found
52 not found
52
52 not found
52 not found
52 not found
52 not found

Looking at the original post we can see the desired out. The awk solution is really close, we just need to turn those commas into spaces...
$ awk -F, '$6==52 {gsub(","," ");print $0}' < data
0 52 3 3 62 52 3 3 62 1411
0 52 2 2 14 52 2 2 14 1501
0 52 3 4 14 52 3 4 14 1473
0 52 1 3 48 52 1 3 48 1431
$
Sponsored Links
    #6  
Old 04-19-2006
bobo bobo is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 5 March 2014, 10:47 AM EST
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Bug Dear friends

Thank you very much for the replies! I will try them all.
Sponsored Links
    #7  
Old 04-19-2006
bobo bobo is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 5 March 2014, 10:47 AM EST
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Data Problems!

This syntax works great:

awk -F, '{ if ($6 == 52) print $0 }' input.txt

But I do have problems:

The data i have is SEPERATE by a ; not ,....Please have with this minor issue.

Thanks!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep but ignore first column danhodges99 Shell Programming and Scripting 5 02-15-2011 07:59 PM
Column grep css136 Shell Programming and Scripting 6 09-30-2010 11:41 PM
Get 5th column using grep command jyothi_wipro Shell Programming and Scripting 6 05-10-2010 06:47 AM
Grep by column number hitman247m UNIX for Dummies Questions & Answers 7 02-02-2009 04:34 PM
Row to column converter using Awk or grep? Needhelp2 Shell Programming and Scripting 3 11-06-2008 12:27 PM



All times are GMT -4. The time now is 08:40 PM.