[Discuss] simple (?) awk logic/syntax problem

noel at natnix.com noel at natnix.com
Thu Aug 10 11:28:37 PDT 2006


I kept trying for a simple one-liner, but this is as close as I got:

#! /usr/bin/perl

# split input like: /dev/sda /dev/sda1 /dev/sdb /dev/sdc /dev/sdc1
while(<>) { push(@l, split); } 

# reverse the list so only the last /dev/xxxNNN is printed
@l = reverse(sort(@l));

# print unique devs
foreach (@l) { 
    $s = $_; 
    s/\d+$//; 
    next if( $_ eq $last ); 
    print "$s\n"; 
    $last = $_;
}

--Noel


On Wed, Aug 09, 2006 at 05:13:51PM -0700, Larry Gagnon wrote:
> I have a simple string (one liner) with device names as such:
> 
> /dev/sda /dev/sda1 /dev/sdb /dev/sdc /dev/sdc1
> 
> I want to use awk to read that line and compare each following field to
> the previous field such that if they are the same device but the
> following field contains a partition then drop the previous field
> from the output. For example the above line would become:
> 
> /dev/sda1 /dev/sdb /dev/sdc1
> 
> My attempt at awk is:
> 
> #!/bin/gawk -f
> {
> for (i=1; i<=NF; i++) 
>     PD = $i
>     ND = $(i+1) 
>     if (substr(PD,1,8) == substr(ND,1,8)) 
>                 print ND 
> }
> 
> This does not work. Any ideas appreciated.
> 
> Larry
> _______________________________________________
> Discuss mailing list
> Discuss at vlug.org
> http://ladybug.vlug.org/cgi-bin/mailman/listinfo/discuss


More information about the Discuss mailing list