Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    81

    bash: Parse variable


    I have a variable with the following content (could vary from 1 to many .img files):

    Code:
    disk = ['file:/OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/e4e3e02b7a29403ea9861576c671d5a1.img,xvda,w', 'file:/OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/3efcfaf98183408487ea6b1144034b5b.img,xvdb,w']
    I need to parse out each *img starting with /OVS and ending with .img. I am looking to have a new var or list that would look similar to:

    Code:
    /OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/e4e3e02b7a29403ea9861576c671d5a1.img
    /OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/3efcfaf98183408487ea6b1144034b5b.img
    I am unsure how to approach this. Any ideas?

  2. #2
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    81
    I have this, but wonder if there is a more elegant way to accomplish this:

    Code:
    echo $TEST | sed 's/file\:/\nfile:/g' | grep ^file | cut -d / -f 2-6 | cut -d , -f 1

  3. #3
    Quote Originally Posted by jdh239 View Post
    I have a variable with the following content (could vary from 1 to many .img files):

    Code:
    disk = ['file:/OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/e4e3e02b7a29403ea9861576c671d5a1.img,xvda,w', 'file:/OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/3efcfaf98183408487ea6b1144034b5b.img,xvdb,w']
    I need to parse out each *img starting with /OVS and ending with .img.
    that is NOT a bash variable.
    actually, it looks a lot like something i've seen in ruby recently.
    some sort of array.
    i question the whole approach - why do you need to parse an array from a different language in bash?
    I am not a "Linux Guru"! Get off me! The Forum software won't let me change it!
    How to ask smart questions | Don't be a Help Vampire | How to Use Code Tags
    You can post a link by removing "http://www." from it.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    81
    This is a line from a file, and I read it into a variable.

    Bash is the language I am scripting in (as it is the only one I know)

  6. #5
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,460
    Hi.

    Suppose file z4 contains this:
    Code:
    disk = ['file:/OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/e4e3e02b7a29403ea9861576c671d5a1.img,xvda,w', 'file:/OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/3efcfaf98183408487ea6b1144034b5b.img,xvdb,w']
    then running:
    Code:
    grep -o -P '/OVS.*?img' z4
    produces:
    Code:
    /OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/e4e3e02b7a29403ea9861576c671d5a1.img
    /OVS/Repositories/0004fb0000030000da07e5ae9ffb9517/VirtualDisks/3efcfaf98183408487ea6b1144034b5b.img
    On a system like:
    Code:
    OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
    Distribution        : Debian 8.9 (jessie) 
    grep (GNU grep) 2.20
    See man grep for details. The ".*?" indicates that a (perl) minimal match is desired.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #6
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    81
    Awesome, thanks drl! I learned something new!

Posting Permissions

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