[PLUG] Correcting duplicate strings in files

Robert Citek robert.citek at gmail.com
Tue Jun 19 22:40:59 UTC 2018


$2 != "16.00" { print ; next }  <= the decimal should be a colon, 16:00 vs 16.00
flag == 1 && $2 == "16:00" { $2=="17:00"; print; flag=0 ; next } <=
equality should be assignment, $2= vs $2==

Here's a refactored version that you can put in a file:

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


BTW, in your sample data set the 15:00 and 16:00 entries are identical
in the last field.  Is that expected or coincidental?

Regards,
- Robert


On Tue, Jun 19, 2018 at 3:31 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> 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
> _______________________________________________
> PLUG mailing list
> PLUG at pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug



More information about the PLUG mailing list