Grep/Awk on 1st 2 Letters in 2nd Column of File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep/Awk on 1st 2 Letters in 2nd Column of File
# 1  
Old 11-06-2012
Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one:

Code:
192.168.1.1   CAXY0_123   10ABFL000001   #   Comment
192.168.1.2   CAYZ0_123   10ABTX000002  #   Comment
192.168.2.1   FLXY0_123   11ABCA000001   #   Comment
192.168.2.2   FLYZ0_123   11ABTX000002   #   Comment
192.168.3.1   TXXY0_123   12ABCA000001  #   Comment
192.168.3.2   TXYZ0_123   12ABFL000002   #   Comment

...and print the entire line in which the match was found.

For example, I need to grep/awk for 'CA' in the CAXY0_123 and CAYZ0_123 entries in the first 2 lines of the file so that the resulting output will be:
Code:
192.168.1.1   CAXY0_123   10ABFL000001   #   Comment
192.168.1.2   CAYZ0_123   10ABTX000002  #   Comment

Can someone let me know how this can be done? I've been googling myself crazy and checking previous forum posts here but have found nothing that applies to this exact situation. I also haven't had any luck playing around with the grep/awk/nawk commands with which I'm already familiar.

Thanks!

Last edited by radoulov; 11-06-2012 at 11:07 AM..
# 2  
Old 11-06-2012
Hi

Code:
$ awk '$2 ~ /^CA/' file
192.168.1.1 CAXY0_123 10ABFL000001 # Comment
192.168.1.2 CAYZ0_123 10ABTX000002 # Comment

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 11-06-2012
With grep:

Code:
grep ' CA' file

192.168.1.1 CAXY0_123 10ABFL000001 # Comment
192.168.1.2 CAYZ0_123 10ABTX000002 # Comment

# 4  
Old 11-06-2012
or (with some assumptions):
Code:
grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3} CA' file

# 5  
Old 11-06-2012
Hey guys. Thanks for the quick replies. I tried the suggestions below on the acutal file (~40000 entries) and, after cross checking the output with another source, I appeared to get the most accurate results with Guru's code (awk '$2 ~ /^CA/' file).

I'll be making note of all of the suggestions below for future reference, though.

Thanks again! You guys just saved me a bunch of time.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

3. UNIX for Dummies Questions & Answers

Grep -v value in 2nd column

Trying to do a grep -v on a value in the 2nd column of text. So if the word apple appears in a line in the 2nd column, it would not show up when the file was cat. Seems like a simple enough operation but I just can't figure it out. Any help would be appreciated. Thanks in advance. Are apples... (4 Replies)
Discussion started by: jimmyf
4 Replies

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

5. Shell Programming and Scripting

Calculate 2nd Column Based on 1st Column

Dear All, I have input file like this. input.txt CE2_12-15 3950.00 589221.0 9849709.0 768.0 CE2_12_2012 CE2_12-15 3949.00 589199.0 9849721.0 768.0 CE2_12_2012 CE2_12-15 3948.00 589178.0 9849734.0 768.0 CE2_12_2012 CE2_12-52 1157.00 ... (3 Replies)
Discussion started by: attila
3 Replies

6. UNIX for Dummies Questions & Answers

Grep.Need help with finding the words which start at [A-K] letters in thesecond column of the table

Hi buddies ! I need some help with one grep command :) I have this table: 1 Petras Pavardenis 1980 5 08 Linas Bajoriunas 1970 10 3 Saulius Matikaitis 1982 2 5 Mindaugas Stulgis 1990... (1 Reply)
Discussion started by: vaidastf
1 Replies

7. UNIX for Dummies Questions & Answers

Grep /Awk letters X - X in every line and print it as a mac address

hey i m kinda new to this so i will appreciate any help , i have this list of values: pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200 pwwn = 0x5001248201bcd7a7 nwwn=0x5001248201bcd7a6 port_id = 0x280300 pwwn = 0x50012482009c51ad nwwn=0x50012482009c51ac port_id =... (4 Replies)
Discussion started by: boaz733
4 Replies

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

9. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question