Grep but ignore first column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep but ignore first column
# 1  
Old 02-15-2011
Grep but ignore first column

Hi,

I need to perform a grep from a file, but ignore any results from the first column.
For simplicity I have changed the actual data, but for arguments sake, I have a file that reads:

Code:
MONACO   Monaco  ASMonaco
MANUTD   ManUtd  ManchesterUnited
NEWCAS   NewcastleUnited
NAC000    NAC  NACBreda

The first column in this file will always contain a 6-character ID. This file is basically a lookup table where a grep is performed with the idea of returning the ID in the first column. There can be many aliases for each ID, as you can see above.
The problem I have is that sometimes I get 2 IDs returned. For example, when I grep for 'NAC', with the aim being to return the ID 'NAC000', but this grep also matches the ID for the MONACO line.

Is there any way to grep, but ignore any matches from the first column? Or perhaps grep only from column 2, or 3 etc etc?

Thanks.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-15-2011 at 05:08 PM.. Reason: code tags, please!
# 2  
Old 02-15-2011
Just grep for space followed by zero or more chars then your string:

Code:
$ grep " .*NAC" infile
NAC000 NAC NACBreda

# 3  
Old 02-15-2011
grep for 'NAC' in the SECOND column
Code:
nawk 'pat ~ $col' pat='NAC' col=2 myFile

# 4  
Old 02-15-2011
See if this works for you:
Code:
egrep '^.{6}.*NAC' input_file

# 5  
Old 02-15-2011
Thanks guys, found egrep was best for what I wanted, appreciated shell_life.
# 6  
Old 02-15-2011
Code:
awk '$2~/NAC/' infile

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

Grep and ignore list from file

cat /tmp/i.txt '(ORA-28001|ORA-00100|ORA-28001|ORA-20026|ORA-20025|ORA-02291|ORA-01458|ORA-01017|ORA-1017|ORA-28000|ORA-06512|ORA-06512|Domestic Phone|ENCRYPTION)' grep -ia 'ORA-\{5\}:' Rep* |grep -iavE `cat /tmp/i.txt` grep: Unmatched ( or \( Please tell me why am i getting that (6 Replies)
Discussion started by: jhonnyrip
6 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. SCO

Grep to ignore suffix & find end of line

In COBOL, a hyphen can be used in a field name and in a specific program some field names would be identical to others except a suffix was added--sometimes a suffix to a suffix was used. For example, assume I am looking for AAA, AAA-BBB, and AAA-BBB-CCC and don't want to look at AAA-BBB-CCC... (7 Replies)
Discussion started by: wbport
7 Replies

5. Shell Programming and Scripting

Grep command to ignore line starting with hyphen

Hi, I want to read a file line by line and exclude the lines that are beginning with special characters. The below code is working fine except when the line starts with hyphen (-) in the file. for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'` do . . done How... (4 Replies)
Discussion started by: Srinraj Rao
4 Replies

6. Shell Programming and Scripting

Ignore delimiter within a column

Hi, So I was trying this awk snippet awk -F, '$1 ~ /Match/' File This is my sample file output Name,Age,Nationality,Description Jack,20,American,Tall, caucasian, lean Mary,30,British,Short,white,slim I would expect expected Output to be, when a certain match is met say $1 is... (2 Replies)
Discussion started by: sidnow
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 regex to ignore sequence only if surrounded by fwd-slashes

Hi, I've got a regex match to perform in a Bash script and can't quite get it right. Basically I want to match all IP address like sequences in a file which may or may not contain an IP address but with the extra qualification of ignoring any IP-like sequence which begins and ends with a... (27 Replies)
Discussion started by: gencon
27 Replies

9. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

10. Shell Programming and Scripting

ignore fields to check in grep

Hi, I have a pipe delimited file. I am checking for junk characters ( non printable characters and unicode values). I am using the following code grep '' file.txt But i want to ignore the name fields. For example field2 is firstname so i want to ignore if the junk characters occur... (4 Replies)
Discussion started by: ashwin3086
4 Replies
Login or Register to Ask a Question