If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > finding first occurence and get 4 chars after it

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-11-03, 14:56
tina2000 tina2000 is offline
Registered User
 
Join Date: Sep 2003
Posts: 2
Question finding first occurence and get 4 chars after it

Hi to all,

i've never programmed a shell skriptbefore and i need your help please!

i have to search the first occurence of a string in a file -> then read the next 4 characters!

fileexample:

lsap state: LSAP_DISCONNECTES, slsap_sel: 0x10, dlsap_sel: 0xff, (IrCOMM)
lsap state: LSAP_DISCONNECTES, slsap_sel: 0x0, dlsap_sel: 0xff, (IrIAS srv)


what i need ist the 4 characters after the first occurence of 'slsap_sel: ' which is 0x10


I know that that's no problem for you!

Thanks for any help
tina
Reply With Quote
  #2 (permalink)  
Old 09-12-03, 06:58
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
This should do it...

awk -F"," ' # -F flag sets the field delimiter to a ","
$2~/slsap/ # If the 2nd field matches "sslap"...
{
# print the the substring of 2nd field,
# starting from 2 characters positions after the ":" in the second field
print substr($2,index($2,": ")+2);
# once you've found a match, you don't need to process anymore
exit }' yourFile

Without comments...

awk -F"," '$2~/slsap/ {print substr($2,index($2,": ")+2); exit}' yourFile

HTH

Last edited by Damian Ibbotson; 09-12-03 at 07:05.
Reply With Quote
  #3 (permalink)  
Old 09-13-03, 07:56
tina2000 tina2000 is offline
Registered User
 
Join Date: Sep 2003
Posts: 2
Hi Damian,

thanks for your answer!
i already got it, and my script is very close to yours ;-)

awk '/slsap_sel: /{
if ( substr($0,index($0,"slsap_sel: ")+11,4 )=="0x6a" )
... do somethin ...
exit
}' irlmp

bye tina

Quote:
Originally posted by Damian Ibbotson
This should do it...

awk -F"," ' # -F flag sets the field delimiter to a ","
$2~/slsap/ # If the 2nd field matches "sslap"...
{
# print the the substring of 2nd field,
# starting from 2 characters positions after the ":" in the second field
print substr($2,index($2,": ")+2);
# once you've found a match, you don't need to process anymore
exit }' yourFile

Without comments...

awk -F"," '$2~/slsap/ {print substr($2,index($2,": ")+2); exit}' yourFile

HTH
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On