I wrote a script which takes values from a CSV file and inserts in into a postgres table, but was facing a problem.
The date columns in the CSV file are in ‘YYYYMMDDHH24MISS’ format, and the same column in the table are defined as Timestamp, and when inserting using the copy command, I am getting the below error,
ERROR: 22008: date/time field value out of range: "20000125200942"
So I tried few things and figured out if I have the entries in ‘YYYYMMDD HH24MISS’ format, the insert works fine.
Now the problem is I don’t know how to format the entries of the CSV file in ksh.
I tried using the gsub command with awk, but it didn’t seem to work.
awk 'BEGIN {FS=OFS=","} {gsub(/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/, "\1\2\3 \4\5\6", $1)} 1' input_file.csv > output_file.csv
All your help would be appreciated 🙂
4
Answers
Assumptions:
YYYYMMDDHHMMSS
formatSample input:
One
awk
idea where we redefine the 1st field ($1
) with a pair of substrings separated by a space:This generates:
With GNU
awk
:With GNU
sed
:Output:
With your shown samples please try following GNU
awk
code.