Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Awk to match string

    I can't figure out how to pull part of a string that I need using awk.

    I have a string that looks like this:

    temp=$"buildroot_tag: buildroot_dev_sdev_v2b0064_20111222_dcm"
    Using a regex simulator I found I can use:

    to extract the value I want: v2b0064_20111222

    I can't assume too much about the string other than it will be something like *_v*b*_SomeDate_*

    I've been able to get to the point of extracting just this line from a document but can't figure out how to get just the part of this string I need.

    I thought it should be:

    temp=$(echo $temp | awk /v[0-9][a-fA-F][0-9]*_[0-9]*[^_.*]/)
    but that just spits out the same thing I started with. I want temp to be equal to:

    How can I do this?

    This works but I don't think I can assume it will always work because the number of fields can vary.
    echo $temp | awk -F_ '{print $5 "_" $6}'
    Last edited by SI_TW; 07-25-2012 at 08:31 PM.

  2. #2
    you could pipe it to sed, and let it do the stripping/substituting:

    temp=$"buildroot_tag: buildroot_dev_sdev_v2b0064_20111222_dcm"
    echo $temp |\
     awk /v[0-9][a-fA-F][0-9]*_[0-9]*[^_.*]/ |\
     sed -e 's|^.*\(v[0-9]b[0-9].*_[0-9]*\)_dcm$|\1|'

  3. #3
    That works great. Thanks

  4. $spacer_open

Posting Permissions

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