[PLUG] Correcting duplicate strings in files

Rich Shepard rshepard at appl-ecosys.com
Tue Jun 19 22:31:40 UTC 2018


On Tue, 19 Jun 2018, Robert Citek wrote:

> Couple of typos and an addition (-F,) :

   I'm not seeing the typos.

> { cat <<eof
> 2012-10-01,14:00,90.7999
> 2012-10-01,15:00,90.8121
> 2012-10-01,16:00,90.8121
> 2012-10-01,16:00,90.8121
> 2012-10-01,18:00,90.8091
> 2012-10-01,19:00,90.8030
> eof
> } | awk -F,  '
> $2 != "16:00" { print ; next }
> flag == 0 && $2 == "16:00" { print ; flag=1 ; next }
> flag == 1 && $2 == "16:00" { $2="17:00"; print; flag=0 ; next }
> '

   I have the code in a file and run it with the '-f' option:
gawk -f correct-double-hour.awk test.dat > out.dat

correct-double-hour.awk:

#!/usr/bin/gawk
#
# This script replaces the second instance of 16:00 with 17:00.

BEGIN { FS=","; OFS="," }
$2 != "16.00" { print ; next }
flag == 0 && $2 == "16:00" { print ; flag=1 ; next }
flag == 1 && $2 == "16:00" { $2=="17:00"; print; flag=0 ; next }

Thanks,

Rich



More information about the PLUG mailing list