Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    2

    Question Unanswered: 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

  2. #2
    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 08:05.

  3. #3
    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

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •