我想将日期格式更改为 2013-08-01
并且我文件中的当前格式为 8/10/13
并且该文件包含数千条记录我也有大量文件。
请就此提出建议,如何转换。
+abcd 2013^8/1/13^Bing^Milwaukee_abcd High Volume^Abcd^izvd4PZo^^Wisconsin^Chicago^http://ad.abcd.net/clk;256581924;80198650;s;u=ms&sv1=izvd4PZo&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyacar.com/specials?srchid=|Milwaukee_cdhdf+High+Volume|_%2Berhefn_2013^Broad^5^1^1.47^
+sjhrejk models^8/1/13^Bing^Milwaukee_shdh High Volume^fhekj^9NT23JfC^^Wisconsin^Chicago^http://ad.fhd.net/clk;256581924;80198650;s;u=ms&sv1=9NT23JfC&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyhfrkeu.com/specials?srchid=|Milwaukee_djkjfhkd+High+Volume|_%2Bdhk_models^Broad^14^4^5.58^
buy a +jkdsh^8/1/13^Bing^Milwaukee_kjfheu High Volume^jkdfhdj^T0ncFpv4^^Wisconsin^Chicago^http://ad.jfd.net/clk;256581924;80198650;s;u=ms&sv1=T0ncFpv4&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyfjj.com/specials?srchid=|Milwaukee_jefrhjd+High+Volume|buy_a_%2Bfhd^Broad^15^1^2.44^
谢谢!!
最佳答案
您可以使用 -d
来替换您的日期以传递当前格式,并使用 "+..."
来指示新格式:
$ date -d"8/10/13" "+%Y-%m-%d"
2013-08-10
要使用此格式更新文件中的所有内容,请继续读取行,获取这些类型的字段并对其进行格式化:
while IFS= read -r line
do
mydate=$(grep -Po '[0-9]+/[0-9]+/[0-9]+' <<< "$line") # gets 8/1/13
# perform the replacement in case there is date to process
if [[ ! -z "$mydate" ]]; then
newdate=$(date -d"$mydate" "+%Y-%m-%d") # converts to 2013-08-01
sed -i "s#$mydate#$newdate#" file # replaces in the text (-i option)
fi
done < file
根据您的输入输出:
+abcd 2013^2013-08-01^Bing^Milwaukee_abcd High Volume^Abcd^izvd4PZo^^Wisconsin^Chicago^http://ad.abcd.net/clk;256581924;80198650;s;u=ms&sv1=izvd4PZo&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyacar.com/specials?srchid=|Milwaukee_cdhdf+High+Volume|_%2Berhefn_2013^Broad^5^1^1.47^
+sjhrejk models^2013-08-01^Bing^Milwaukee_shdh High Volume^fhekj^9NT23JfC^^Wisconsin^Chicago^http://ad.fhd.net/clk;256581924;80198650;s;u=ms&sv1=9NT23JfC&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyhfrkeu.com/specials?srchid=|Milwaukee_djkjfhkd+High+Volume|_%2Bdhk_models^Broad^14^4^5.58^
buy a +jkdsh^2013-08-01^Bing^Milwaukee_kjfheu High Volume^jkdfhdj^T0ncFpv4^^Wisconsin^Chicago^http://ad.jfd.net/clk;256581924;80198650;s;u=ms&sv1=T0ncFpv4&sv2={AdId}&sv3=3392oik17016&sv4={keyword}&sv5={matchtype};?http://www.chicago.buyfjj.com/specials?srchid=|Milwaukee_jefrhjd+High+Volume|buy_a_%2Bfhd^Broad^15^1^2.44^
关于linux - 如何在 unix 中更改 .csv 文件中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18657411/