[PLUG] Reformating date fields in text files [FIXED]
Rich Shepard
rshepard at appl-ecosys.com
Sun Jan 16 14:04:23 UTC 2022
On Sat, 15 Jan 2022, Russell Senior wrote:
> It doesn't actively hurt you, but it might confuse you later. You
> don't need to set OFS (output field separator) if you aren't going to
> use it.
>
> Fwiw, if you want commas, this is what I came up with:
>
> awk -F\| 'BEGIN { OFS = "," } { print $6 ; split($6,a,"/") ; str =
> sprintf("%04d-%02d-%02d",a[3],a[1],a[2]) ; $6 = str ; print $0 }'
Russell,
The date field is part of the entire line. I need to change the date format
and print the entire line with the new format.
Specifying FS=OFS="|" when a script re-orders the fields in a line works;
the output file has all fields separated by the pipe. What I discovered
using printf is that it changes the OFS to a comma regardless of the
command. Reading about printf() (man page, books, web sites) I've not yet
found how to keep the field separator on each line. Still working on that.
Thanks,
Rich
More information about the PLUG
mailing list