Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Sed/Awk: Finding first instance of a string and printing the following characters

Shell Programming and Scripting


Tags
awk, first occurrence, print, sed

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-04-2011
jhunter87 jhunter87 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 17 February 2011, 1:17 PM EST
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 Unix and Linux 02-04-2011
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 23 May 2015, 5:01 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,304
Thanks: 135
Thanked 468 Times in 422 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 02-04-2011
jhunter87 jhunter87 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 17 February 2011, 1:17 PM EST
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 Unix and Linux 02-04-2011
yinyuemi yinyuemi is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 24 March 2015, 6:12 AM EDT
Posts: 436
Thanks: 9
Thanked 106 Times in 105 Posts

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

The Following 2 Users Say Thank You to yinyuemi For This Useful Post:
himanshu sood (04-22-2014), jhunter87 (02-04-2011)
Sponsored Links
    #5  
Old Unix and Linux 02-04-2011
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 23 May 2015, 5:01 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,304
Thanks: 135
Thanked 468 Times in 422 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 Unix and Linux 02-04-2011
jhunter87 jhunter87 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 17 February 2011, 1:17 PM EST
Posts: 6
Thanks: 4
Thanked 0 Times in 0 Posts
That's working now. Thanks a lot!
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 05:07 AM.