|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
grep '^700.......79' file1 |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
That did it! I hadn't seen the 'if' statement used with 'substr' in awk before. Thanks for the lesson.
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Try this short version (still longer than mjf's): Code:
$ awk '/^700/ && $2~/^79/' file 700 7912345678910 |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
OK, try this one: Code:
awk '/^700/ && $11$12=="79"' FS="" file |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|