Find the answer to your Linux question:
Results 1 to 3 of 3
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: Code: temp=$"buildroot_tag: buildroot_dev_sdev_v2b0064_20111222_dcm" Using a regex ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    4

    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:

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

    Code:
    v[0-9][a-fA-F][0-9]*_[0-9]*[^_.*]
    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:

    Code:
    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:
    v2b0064_20111222

    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.
    Code:
    echo $temp | awk -F_ '{print $5 "_" $6}'
    Last edited by SI_TW; 07-25-2012 at 08:31 PM.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    you could pipe it to sed, and let it do the stripping/substituting:

    Code:
    #!/bin/bash
    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
    Just Joined!
    Join Date
    Dec 2007
    Posts
    4
    That works great. Thanks

Posting Permissions

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