Reputation: 686
I have a very big CSV file (aprox. 10.000 rows and 400 columns) and I need to modify certain columns (like 15, 156, 220) to change format from 20140321132233
to 2014-03-21 13:22:33
. All fields that I need to modify are datetime.
I saw some examples using awk but for math modifications. Can I use something like this for doing the above change?
file.csv example:
19238328932|123233443|123|0|||||123123|20140321132233|1|0|0|....|20130211122143|...
12332312211|222321233|111|0|||||234432|20150222122354|1|0|0|....|20120112123133|...
Upvotes: 2
Views: 436
Reputation: 4950
Please save following awk
script as awk.src:
function date_str(val) {
Y = substr(val,0,4);
M = substr(val,5,2);
D = substr(val,7,2);
date = sprintf("%s-%s-%s",Y,M,D);
return date;
}
function time_str(val) {
h = substr(val,9,2);
m = substr(val,11,2);
s = substr(val,13,2);
time = sprintf("%s:%s:%s",h,m,s);
return time;
}
BEGIN {
FS="|"
}
#
## MAIN Block
#
{
for (i=1;i<=NF;i++) {
if (i==10) {
printf "%s %s", date_str($i), time_str($i);
}
else { printf $i; }
if (i!=NF) {
printf FS;
}
else { printf "\n"; }
}
}
Now try it, it should print:
$ awk -f awk.src csv
19238328932|123233443|123|0|||||123123|2014-03-21 13:22:33|1|0|0|....|20130211122143|...
12332312211|222321233|111|0|||||234432|2015-02-22 12:23:54|1|0|0|....|20120112123133|...
Upvotes: 4