Find the answer to your Linux question:
Results 1 to 6 of 6
Hi! I need some help with bash scripting. I have a string of the type: 'nuber1/number2/number3 some text' I need to extract numerical values in the variables. What is the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    7

    help with a script needed


    Hi!

    I need some help with bash scripting.

    I have a string of the type:

    'nuber1/number2/number3 some text'

    I need to extract numerical values in the variables. What is the easiest way to do this?

    Thank you!

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429
    Sounds like Homework?
    Homework Questions arnt allowed around here.
    Small advice. Use SED or AWK.
    New Users, please read this..
    Google first, then ask..

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    7
    Thanks for the hint. No, this is not a homework... I need to extract number of cylinders, heads and sectors from the log file and to use them to start the other program. Here is my code... It works, but seems a bit ugly, are there any better way to do this?

    Code:
    cyl=`grep "number of cyl" log.txt | sed -n 's/\([0-9][0-9]*\)\/[0-9][0-9]*\/[0-9][0-9]*.*/\1/p'`
    head=`grep "number of cyl" log.txt | sed -n 's/[0-9][0-9]*\/\([0-9][0-9]*\)\/[0-9][0-9]*.*/\1/p'`
    sect=`grep "number of cyl" log.txt | sed -n 's/[0-9][0-9]*\/[0-9][0-9]*\/\([0-9][0-9]*\).*/\1/p'`

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,429
    I guess its fine like this. I cant do sed at all so if it works it should just be fine right? ^^
    What you might want to do is:
    Code:
    Share=`grep "number of cyl" log.txt | sed -n 's/\([0-9][0-9]*\)\/[0-9][0-9]*\/[0-9][0-9]*.*/\1/p'`
    cyl=$Share
    head=$Share
    sect=$Share
    Since I noticed you did read the same logfile for the same thing three times.
    New Users, please read this..
    Google first, then ask..

  6. #5
    Just Joined!
    Join Date
    Nov 2007
    Posts
    7
    I read 3 different values from the same line, which contains keyword...

    cyl/head/sect keyword and more text...

    May be someone knows a way to do this in one pass?

  7. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Show us the strings you have and the output you desire.

    Regards

Posting Permissions

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