[CentOS] OT: piped greps with regex

Mon Jan 4 21:14:22 UTC 2010
Les Mikesell <lesmikesell at gmail.com>

Mr. X wrote:
> List,
> 
> Happy NY.
> 
>>From the bash command below, I'm trying to parse out the startsector value:
> $ sudo file mini_vusb.img
> mini_vusb.img: x86 boot sector, Microsoft Windows XP MBR, Serial 0x25d84; partition 1: ID=0xe, active, starthead 1, startsector 32, 390496 sectors
> 
> First try was with grep/egrep but I wanted to capture 'startsector 32' as a group (). Nothing came close to working. Fail 1
> 
> Next attempt was with Perl
> file mini_vusb | perl -lane '$i=0 ;for (@F) {print substr($F[$i+1],0,-1) if ($_ eq "startsector"); $i++}'
> 
> the above works, but it's too much fire power for the job :)
> Actually I came up with it in a minute or two once I remembered the special var @F. Success 1
> 
> Last effort led to this
> $ sudo file mini_vusb.img | grep -Po 'startsector\s+\d+'| grep -Po '\d+'
> 32
> 
> I like this best. Is there a way to shorten it up?
> 


sed -ne 's/.*startsector \([0-9]*\).*/\1/p'
But I'd have used perl.  Your firepower is even more wasted sitting idle 
than running perl - and perl can probably do whatever your next step was 
  going to be in the same program.

-- 
   Les Mikesell
    lesmikesell at gmail.com