我在文件夹中有多个逗号分隔的文件。 每个CSV文件如下所示:
"Column1","Date","Column2"
"fdfsd","20151023","rwer"
"fsdsf","20151023","jjfg"
.
.
我需要修改第二个日期列以此格式显示日期" 10/14/2015"或" 2015-10-14"。
请有人帮我这个吗?
由于
这就是我的尝试:
cat test.csv | while read line ; do echo $line\;$(date -d "%Y%m%d" "+ YYYY/MM/DD") ; done
用法:日期[-jnu] [-d dst] [-r seconds] [-t west] [-v [+ | - ] val [ymwdHMS]] ... [-f fmt date | [[[mm] dd] HH] MM [[cc] yy] [.ss]] [+格式] "列1""日期""列2&#34 ;;
用法:日期[-jnu] [-d dst] [-r seconds] [-t west] [-v [+ | - ] val [ymwdHMS]] ... [-f fmt date | [[[mm] dd] HH] MM [[cc] yy] [.ss]] [+格式] " fdfsd"" 20151023"" rwer&#34 ;;
用法:日期[-jnu] [-d dst] [-r seconds] [-t west] [-v [+ | - ] val [ymwdHMS]] ... [-f fmt date | [[[mm] dd] HH] MM [[cc] yy] [.ss]] [+格式] " fsdsf"" 20151023"" jjfg&#34 ;;
答案 0 :(得分:3)
由于您只需要在日期中插入连字符,这是一个字符串操作问题,而不是日期操作问题:
sed -i.bak -re 's/([0-9]{4})([0-9]{2})([0-9]{2})/\1-\2-\3/' *.csv
答案 1 :(得分:2)
您可以使用此awk
重新格式化给定的date
字符串:
awk 'BEGIN{FS=OFS=","} NR>1{cmd = "date -d \"" $2 "\" \"+%Y-%m-%d\"";
cmd | getline out; $2=out; close("uuidgen")} 1' file
"Column1","Date","Column2"
"fdfsd",2015-10-23,"rwer"
"fsdsf",2015-10-14,"jjfg"
答案 2 :(得分:1)
Python有一个非常强大的csv模块。
你可以这样做:
$ python -c '
import csv
import datetime
import fileinput
def line_out(line):
print ",".join(["\"{}\"".format(e) for e in line])
csv_data=csv.reader(fileinput.input())
line_out(next(csv_data))
for line in csv_data:
line[1]=datetime.datetime.strptime(line[1], "%Y%m%d").date().isoformat()
line_out(line)' file
打印:
"Column1","Date","Column2"
"fdfsd","2015-10-23","rwer"
"fsdsf","2015-10-23","jjfg"