[PLUG] Sed syntax error

Paul Heinlein heinlein at madboa.com
Tue Nov 15 22:34:10 UTC 2016


On Tue, 15 Nov 2016, Rich Shepard wrote:

>   Trying to change the date format from a forward slash (/) to a dash (-).
> There's a syntax error in my sed script: "file change-date-format.sed line
> 6: invalid reference \3 on `s' command's RHS" and I'm not seeing why. Here's
> the script:
>
> #!/usr/bin/sed
> #
> # Change date format from / to -.
>
> s/[0-9]\{4\}\/[0-9]\{2\}\/[0-9]\{2\}/\1-\2-\3/g
>
>   Please show me what I've done incorrectly here.

I'm late to this party, but in this case, you can use GNU date, e.g.,

for D in "2014/04/10" "2015/05/11" "2016/06/12"; do
   date -d $D +%F
done

The %F output format is exactly what you want.

As an added bonus, date is smart enough to recognize a few different 
input formats automatically (note year placement and missing leading 
zero):

for D in "04/10/2014" "2015/05/11" "6/12/2016"; do
   date -d $D +%F
done


-- 
Paul Heinlein <> heinlein at madboa.com <> http://www.madboa.com/



More information about the PLUG mailing list