Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-19-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
How to match on phrase at beginning of line and specific columns?

Here is my file:


Code:
 
700       7912345678910
61234567891234567891
700       8012345678910
61234567891234567891

I want to pull all lines that begin with '700' only if columns 11-12 are '79'.
My code so far only pulls the '79', not the whole line:


Code:
 
grep ^700 file1 | cut -c 11,12 | grep 79

Thanks a bunch!
Sponsored Links
    #2  
Old 12-19-2012
mjf mjf is offline
Registered User
 
Join Date: Nov 2011
Location: Newtown, PA
Posts: 70
Thanks: 4
Thanked 14 Times in 14 Posts

Code:
grep '^700.......79' file1

Sponsored Links
    #3  
Old 12-19-2012
Yoda's Avatar
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,255
Thanks: 149
Thanked 711 Times in 684 Posts

Code:
awk ' { if(($0 ~ /^700/)&&(substr($0,11,2)==79)) print; } ' filename

The Following User Says Thank You to Yoda For This Useful Post:
Scottie1954 (12-20-2012)
    #4  
Old 12-20-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
That did it! I hadn't seen the 'if' statement used with 'substr' in awk before. Thanks for the lesson.
Sponsored Links
    #5  
Old 12-20-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 1,881
Thanks: 25
Thanked 433 Times in 419 Posts
Try this short version (still longer than mjf's):
Code:
$ awk '/^700/ && $2~/^79/' file
700       7912345678910

Sponsored Links
    #6  
Old 12-20-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
Thank you, RudiC, for the alternative; that'll go into my library of examples. However, in this case, my source file sometimes has no white space before column 11, so I need to search the specific column range (for the sake of brevity, I simplified my example file). bipinajith's solution was the most accurate and worked on more than one unix OS.
Sponsored Links
    #7  
Old 12-20-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 1,881
Thanks: 25
Thanked 433 Times in 419 Posts
OK, try this one:
Code:
awk '/^700/ && $11$12=="79"' FS="" file

Sponsored Links
Reply

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
Match columns and write specific word Priyanka Chopra UNIX for Dummies Questions & Answers 1 11-04-2012 11:33 PM
Awk match on columns and delete line jacobs.smith UNIX for Dummies Questions & Answers 5 07-13-2012 05:01 PM
Help with replace line based on specific pattern match perl_beginner Shell Programming and Scripting 3 11-25-2011 02:52 AM
Compare 2 lists using a full and/or partial match at beginning of line? Garrred UNIX for Dummies Questions & Answers 7 10-12-2010 02:22 PM
match a phrase vanitham Shell Programming and Scripting 4 01-16-2008 01:18 AM



All times are GMT -4. The time now is 12:25 PM.