Displaying only data matching a pattern?

Steven W. Orr steveo at syslang.net
Sun Mar 15 11:20:39 EDT 2009


On Monday, Feb 2nd 2009 at 11:19 -0000, quoth kenta:

=>I've been working with some large files and need to extract a piece of
=>info, unfortunately there's a bunch of junk around the part that I
=>want.  Example:
=>foofoo:A1234567890B\barbar
=>foofoo:C9234567890E\barbar
=>foofoo:A8234567890B\barbar
=>foofoo:F7234567890D\barbar
=>
=>What I had done the first pass to get what I wanted was to use sed and
=>do a  s/foofoo:// to get rid of the stuff in front, and then do the
=>same for the \barbar in the back.  However what would be easier for me
=>is if I could just extract the pattern of [a-fA-F0-9] when they appear
=>n times in a row. I couldn't seem to figure out if sed could display
=>only the part I wanted.  So I figured it'd be a better job for grep,
=>however It appears that it's printing the entire matching line and I
=>only want the match on the pattern to display.
=>
=>Otherwise, I want as an end result:
=>
=>A1234567890B
=>C9234567890E
=>A8234567890B
=>F7234567890D
=>
=>Here's a caveat: I need to do this on a Windows box and am limited to
=>using the ports of  GNU utilities (using
=>http://unxutils.sourceforge.net/) otherwise I'd do this in perl and
=>wouldn't be asking :/   I'm thinking maybe I'm just missing something
=>simple here.
=>
=>Any quick solutions?  My Google-fu is weak today.

I'm a little behind in my mail, but I looked at the proffered solutions 
and people seem to want to use all manner of external tools, sed, awk, 
perl, cut, ad nauseum. Just use bash.

If you set qq to one of the example values
qq='foofoo:A1234567890B\barbar'
then all you have to do is to use bash to take it apart. No external 
programs required.

ww=${qq#foofoo:*}
echo ${ww%\\barbar}

so to put it all together

while read a_line
do
    ww=${qq#foofoo:*}
    echo ${ww%\\barbar}
done < some_data_file

echo 'Ta da'
exit "$time_to_drink_beer"

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


More information about the gnhlug-discuss mailing list