|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Sed/Awk: Finding first instance of a string and printing the following characters
Hi all, I'm new to scripting, so sorry if this is a simple question! I want to find the first occurrence of the string "00" in a file, and then print the first 4 characters that follow that string. So for example, if the file begins: Code:
ELT190 +0019879753004367 BDGF... I want it to print "1987" only I can use sed/awk and I think grep as well. Thanks a lot
Last edited by zaxxon; 02-04-2011 at 11:30 AM.. Reason: code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Code:
$ echo "ELT190 +0019879753004367 BDGF..."| sed 's/.*+00\(....\).*/\1/g' 1987 |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Thanks for that,
It's almost right, but not quite. That line prints the first 4 characters after the LAST occurrence of +00, I want the first 4 characters after the FIRST occurrence of "00" Thanks |
|
#5
|
||||
|
||||
|
Code:
$ echo "ELT190 +0019879753004367 00notthissss BDGF..."| awk -F "00" '{print substr($2,0,4)}'
1987 |
| The Following User Says Thank You to zaxxon For This Useful Post: | ||
jhunter87 (02-04-2011) | ||
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
That's working now. Thanks a lot!
|
| Sponsored Links | ||
|
![]() |
| Tags |
| awk, first occurrence, print, sed |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with finding a string and printing value in the next column | drewpark | Shell Programming and Scripting | 7 | 12-14-2009 02:13 PM |
| find the first instance after a string | josslate | Shell Programming and Scripting | 2 | 05-19-2009 08:11 AM |
| Printing the Status of a Process in an Instance | yoursdavinder | Shell Programming and Scripting | 8 | 02-11-2009 08:25 AM |
| replace nth instance of string | uttamhoode | Shell Programming and Scripting | 4 | 04-03-2008 02:25 AM |
| finding first instance | peter.herlihy | UNIX for Dummies Questions & Answers | 20 | 08-14-2002 03:52 AM |
|
|