Dil*_*eep 1 unix linux csv shell unix-timestamp
我想将日期格式更改为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^
Run Code Online (Sandbox Code Playgroud)
谢谢 !!
您可以使用-d以替换日期来传递当前格式并"+..."指示新格式:
$ date -d"8/10/13" "+%Y-%m-%d"
2013-08-10
Run Code Online (Sandbox Code Playgroud)
要使用此格式更新文件中的所有内容,请继续阅读行,获取这些类型的字段并格式化它们:
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
Run Code Online (Sandbox Code Playgroud)
根据您的输入输出:
+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^
Run Code Online (Sandbox Code Playgroud)