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

Larry Gagnon lggagnon at uniserve.com
Wed Aug 9 17:13:51 PDT 2006


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


More information about the Discuss mailing list