我目前每周处理一批 -50plus csv 文件,其时间戳显示为 Tue Oct 01 10:59:59 PDT 2013。我需要能够逐行浏览并将格式更改为 10/01/13 10:59:59。有些文件将时间戳作为第一个字符串,有些文件将时间戳记在第三个字符串中。我运气不好...
这是两个 csv 文件的片段。
1.csv
Tue Oct 01 10:59:59 PDT 2013,data1,1,Databcd,Dataxyz,0,0,431,0
Tue Oct 01 11:59:59 PDT 2013,data1,1,Databcd,Dataxyz,0,0,401,0
2.csv
data1,0,Databcd,0,0,0,Tue Oct 01 11:59:59 PDT 2013,Dataxyz
data1,0,Databcd,0,0,0,Tue Oct 01 12:59:59 PDT 2013,Dataxyz
提前致谢 -
这是我上次运行的脚本..
#!/bin/bash
for f in $*
do
echo "Processing [$f]..."
ftemp=$f.TMP
#echo "ftemp=$ftemp"
#this uses sed to delete the day(word) frm the timestamp.
sed -e 's/Mon //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed -e 's/Tue //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed -e 's/Wed //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed -e 's/Thu //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed -e 's/Fri //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed -e 's/Sat //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed -e 's/Sun //g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
#strip out the PDT & Year from end of each line
sed -e 's/\ PDT / /g' -e 's/\ PST / /g' <$f >$ftemp
mv $ftemp $f #copy it back over the original
sed --date="Oct 01 00:59:59 2013" +%D <$f >$ftemp
mv $ftemp $f #copy it back over the original
#echo "10/01/2013" | sed -E 's/([a-z ]?)\/([0-9][0-9 ]?)\/([0-9][0-9][0-9][0-9]
#/\3-\2-\1/' <$f >$ftemp
# tr 'Oct' '10/' <$f >$ftemp
# mv $ftemp $f #copy it back over the original
done
echo "Done."
如您所见,我有一些我尝试过的选项被注释掉了