How to match on phrase at beginning of line and specific columns? | 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 !!

How to match on phrase at beginning of line and specific columns?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-19-2012
Scottie1954 Scottie1954 is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 May 2014, 6:09 PM EDT
Posts: 43
Thanks: 10
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
Last Activity: 26 July 2014, 6:48 AM EDT
Location: Newtown, PA
Posts: 121
Thanks: 7
Thanked 28 Times in 27 Posts

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

Sponsored Links
    #3  
Old 12-19-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 25 July 2014, 5:04 PM EDT
Location: Galactic Empire
Posts: 3,356
Thanks: 230
Thanked 1,190 Times in 1,123 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
Scottie1954 Scottie1954 is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 May 2014, 6:09 PM EDT
Posts: 43
Thanks: 10
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
Last Activity: 26 July 2014, 8:16 AM EDT
Location: Aachen, Germany
Posts: 3,886
Thanks: 62
Thanked 922 Times in 875 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
Scottie1954 Scottie1954 is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 May 2014, 6:09 PM EDT
Posts: 43
Thanks: 10
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
Last Activity: 26 July 2014, 8:16 AM EDT
Location: Aachen, Germany
Posts: 3,886
Thanks: 62
Thanked 922 Times in 875 Posts
OK, try this one:
Code:
awk '/^700/ && $11$12=="79"' FS="" file

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
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 09:09 AM.