Find the answer to your Linux question:
Results 1 to 4 of 4
First, I apologize if I've reached the wrong forum for this scripting related question but a forum search didn't find me any useful hits so I came here. What I'm ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    0

    Need to extract a string from unstructured file.

    First, I apologize if I've reached the wrong forum for this scripting related question but a forum search didn't find me any useful hits so I came here.

    What I'm trying to accomplish is to be able to locate and extract a copy of a string from an unstructured text file. I can use various commands combined with a regexp to location virtually anything I want but it returns whole lines. The only tools I'm familiar with require columnized data with known delimiters so I'm stuck.

    So for example:
    # egrep '..:..:..:..:..:..' dmesg
    e100: eth0: e100_probe: addr 0xc0110000, irq 20, MAC addr 00:0D:60:07:92:22
    #

    Now I need to grab only '00:0D:60:07:92:22' from the returned line of text without knowing the delimiter or position of what I'm looking for. If anybody knows a technique for doing this without having to loop through the data, it'd really be helpful.

    Any ideas are welcome--thanks.

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    I'd imagine there is a more elegant solution than this, but it works for your example.

    Code:
    chris@angua:~/dev/scratch$ cat test.txt
    e100: eth0: e100_probe: addr 0xc0110000, irq 20, MAC addr 00:0D:60:07:92:22
    chris@angua:~/dev/scratch$ sed -n 's/^\(.*\)\(..:..:..:..:..:..\)\(.*\)$/\2/p' test.txt
    00:0D:60:07:92:22
    Let us know how you get on.

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    0
    Perfect--thanks. I was hoping to do this on one line. Now to beef up my grasp of reg expressions so I can figure out what's going on with your sed command.

  4. #4
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    The search is breaking the line into three parts.

    ^ anchors the search to the start of the line.
    (.*) will match any amount of anything
    (..:..:..:..:..:..) matches the mac address.
    (.*) matches any amount of anything
    $ anchors the search to the end of the line

    Putting things in brackets groups them so they can be referred to later \1, \2 and \3. We are only interested in \2. The brackets have to be escaped with a \ to tell sed that we are grouping things rather than looking for brackets in the text.

    Finally use the -n switch to prevent sed outputting everything along with the 'p' flag on the search command to print the line we are interested in.

    I'm glad it worked. I've only recently started using regular expressions myself. awk has saved me weeks of effort already and I'm looking into learning perl.

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

Posting Permissions

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