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

Richard Body richard.body at shaw.ca
Wed Aug 9 18:26:53 PDT 2006


Hi Larry, 

	I think you can use the matching operator "~" to get what
you want. This gawk program works correctly on the sample input you
gave.

Cheers ~ richard

{
  for (i = 1; i < NF; i++){
          PD=$i
	  ND=$(i+1)
          if( ND ~ PD ) continue
	  printf("%s ", PD)
  } 
  print $NF
}

On Wed, 2006-08-09 at 17:13 -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