Grep by column number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep by column number
# 1  
Old 02-02-2009
Grep by column number

I have a data file that is arranged like this:

Code:
Marketing  Ranjit Singh   Eagles       Dean Johnson   FULL
Marketing  Ken Whillans   Eagles       Karen Thompson FULL
Sales      Peter RobertsonGolden TigersRich Gardener  PART
President  Sandeep Jain   Wimps        Ken Whillans   CONT
Operations John Thompson  Hawks        Cher           PART
Operations Cher           Vegans       Karen Patel    CONT
Sales      John Jacobs    Hawks        Davinder Singh FULL
Finance    Dean Johnson   Vegans       Sandeep Jain   FULL
EngineeringKaren Thompson Vegans       John Thompson  PART
IT         Rich Gardener  Golden TigersPeter RobertsonFULL
IT         Karen Patel    Wimps        Ranjit Singh   FULL

I need to be able to search within characters 12-26 only. But I also want it to output the entire line, like the basic grep function does.

I've been working on it for a week, and I haven't been able to get grep to recognize that I want the first 11 characters, and everything after the 26th character.

Please help Smilie
# 2  
Old 02-02-2009
Something like this?

Code:
awk '{s=substr($0,12,15);sub(/ *$/,"",s)}s==var' var="Peter Robertson" file

Regards
# 3  
Old 02-02-2009
It doesn't seem to be working. I typed it just as you had here, except I changed file to the name of the data file.

Sorry if I'm missing something, I'm new to UNIX, and I'm trying my best to learn Smilie
# 4  
Old 02-02-2009
It works fine for me, this is what I get:

Code:
$ cat file
Marketing  Ranjit Singh   Eagles       Dean Johnson   FULL
Marketing  Ken Whillans   Eagles       Karen Thompson FULL
Sales      Peter RobertsonGolden TigersRich Gardener  PART
President  Sandeep Jain   Wimps        Ken Whillans   CONT
Operations John Thompson  Hawks        Cher           PART
Operations Cher           Vegans       Karen Patel    CONT
Sales      John Jacobs    Hawks        Davinder Singh FULL
Finance    Dean Johnson   Vegans       Sandeep Jain   FULL
EngineeringKaren Thompson Vegans       John Thompson  PART
IT         Rich Gardener  Golden TigersPeter RobertsonFULL
IT         Karen Patel    Wimps        Ranjit Singh   FULL
$
$
$ awk '{s=substr($0,12,15);sub(/ *$/,"",s)}s==var' var="Peter Robertson" file
Sales      Peter RobertsonGolden TigersRich Gardener  PART
$
$ awk '{s=substr($0,12,15);sub(/ *$/,"",s)}s==var' var="Cher" file
Operations Cher           Vegans       Karen Patel    CONT
$
$ awk '{s=substr($0,12,15);sub(/ *$/,"",s)}s==var' var="Peter Robertson" file
Sales      Peter RobertsonGolden TigersRich Gardener  PART
$

Regards
# 5  
Old 02-02-2009
Quote:
Originally Posted by hitman247m
I have a data file that is arranged like this:

Code:
Marketing  Ranjit Singh   Eagles       Dean Johnson   FULL
Marketing  Ken Whillans   Eagles       Karen Thompson FULL
Sales      Peter RobertsonGolden TigersRich Gardener  PART
President  Sandeep Jain   Wimps        Ken Whillans   CONT
Operations John Thompson  Hawks        Cher           PART
Operations Cher           Vegans       Karen Patel    CONT
Sales      John Jacobs    Hawks        Davinder Singh FULL
Finance    Dean Johnson   Vegans       Sandeep Jain   FULL
EngineeringKaren Thompson Vegans       John Thompson  PART
IT         Rich Gardener  Golden TigersPeter RobertsonFULL
IT         Karen Patel    Wimps        Ranjit Singh   FULL

I need to be able to search within characters 12-26 only. But I also want it to output the entire line, like the basic grep function does.

I've been working on it for a week, and I haven't been able to get grep to recognize that I want the first 11 characters, and everything after the 26th character.

Please help Smilie
Code:
grep -E "^.{11}Peter Robertson" file
Sales      Peter RobertsonGolden TigersRich Gardener  PART

Make sure those are spaces and not tabs between the records. The grep is based on it being spaces, basically saying from the start of the line, take anything in the first 11 characters, and then start the comparison.
# 6  
Old 02-02-2009
Ok well, i got the awk command working, but it's not doing exactly what I wanted. Sorry, I should have been more clear in my first post. I needed it to match even partial names within characters 12-26. So if I type "ohn" for example, it would return everyone with that string inside it.

Last edited by hitman247m; 02-02-2009 at 04:04 PM..
# 7  
Old 02-02-2009
Quote:
Originally Posted by hitman247m
Ok well, i got the awk command working, but it's not doing exactly what I wanted. Sorry, I should have been more clear in my first post. I needed it to match even partial names within characters 12-26. So if I type "ohn" for example, it would return everyone with that string inside it.
Code:
awk '{s=substr($0,12,15);sub(/ *$/,"",s)}s ~ var' var="ohn" file

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep values from column 2 in reference of column 1

Gents Is it possible to update the code to get the desired output files from the input list. I called variable to the first column. I need to consider the first column as key to grep the values in the second column according to the desired request. input list (attached ) output1 ... (12 Replies)
Discussion started by: jiam912
12 Replies

2. Shell Programming and Scripting

Extract number from column

I have a lot of file with a lot of lines following the same pattern the lines go like this: alpha_9/output- -413.74928476 2.6116 and I want it to be: 9 -413.74928476 2.6116 thanks for the help (5 Replies)
Discussion started by: galboski
5 Replies

3. UNIX for Beginners Questions & Answers

Grep A Column Based On Column Name

I have a file with two columns separated by white space. Dog Cat fido sneaky dopey poptart ears whisker barky herd Trying to list the words under the column named Dog. Tried a few variations of awk but can't... (4 Replies)
Discussion started by: jimmyf
4 Replies

4. Shell Programming and Scripting

Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records Table --------- Col1 col2 col3 col4 ....................col20 1 2 3 4 .................... 20 a b c d .................... v ... (11 Replies)
Discussion started by: Priti2277
11 Replies

5. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

6. Shell Programming and Scripting

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

8. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

9. Shell Programming and Scripting

Identifying the column number

I'd like to be able to identify in which column a string occurs. So far I know that I can tell how many columns there are and how to return a specific column: $ sar -r | grep 'kbswpcad' | awk 'NF = 9 { print $NF }' %swpused I've even managed to get the columns to output to an array but I... (2 Replies)
Discussion started by: pondlife
2 Replies

10. Shell Programming and Scripting

returning a column number

Hi all, i was doing a small program where if i was to be given the first 3 letters of any month i.e. in the form of Jan or Apr then it would return the column number where it finds a match. To do this i created a 12 element array of months with first 3 letters and if i echo'ed the contents of... (2 Replies)
Discussion started by: scriptingmani
2 Replies
Login or Register to Ask a Question