Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2011
Registered User
 
Join Date: Feb 2011
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
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

Moderator's Comments:
Please use [code] and [/code] tags when posting code, data, logs, examples etc. to enhance readability and to preserve indention etc., thanks.

Last edited by zaxxon; 02-04-2011 at 11:30 AM.. Reason: code tags
Sponsored Links
    #2  
Old 02-04-2011
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Location: St. Gallen, Switzerland
Posts: 5,866
Thanks: 80
Thanked 371 Times in 351 Posts

Code:
$ echo "ELT190  +0019879753004367 BDGF..."| sed 's/.*+00\(....\).*/\1/g'
1987

Sponsored Links
    #3  
Old 02-04-2011
Registered User
 
Join Date: Feb 2011
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
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
    #4  
Old 02-04-2011
Registered User
 
Join Date: Feb 2011
Posts: 436
Thanks: 9
Thanked 105 Times in 105 Posts

Code:
 echo "ELT190  +0019879753004367 BDGF..." |awk '{print substr($2,1,4)}' FS=00
1987

The Following User Says Thank You to yinyuemi For This Useful Post:
jhunter87 (02-04-2011)
Sponsored Links
    #5  
Old 02-04-2011
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Location: St. Gallen, Switzerland
Posts: 5,866
Thanks: 80
Thanked 371 Times in 351 Posts

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  
Old 02-04-2011
Registered User
 
Join Date: Feb 2011
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
That's working now. Thanks a lot!
Sponsored Links
Closed Thread

Tags
awk, first occurrence, print, sed

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



All times are GMT -4. The time now is 10:43 AM.