1

我想将日期格式更改为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^

谢谢 !!

4

2 回答 2

6

您可以使用来替换您的日期-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^
于 2013-09-06T12:05:41.970 回答
0

这是我的解决方案。

#!/bin/bash
IFS='^'
while read -ra LINE; do
    LINE[1]=$(exec date -d "${LINE[1]}" '+%F')
    echo "${LINE[*]}"
done

将其保存到文件并运行bash script.sh < your_file > output.

示例输出:

+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

另一种形式(确保行以 ^ 结尾):

#!/bin/bash
IFS='^'
while read -r LINE; do
    read -ra TOKENS <<< "$LINE"
    TOKENS[1]=$(exec date -d "${TOKENS[1]}" '+%F')
    if [[ $LINE == *^ ]]; then
        echo "${TOKENS[*]}^"
    else
        echo "${TOKENS[*]}"
    fi
done

输出:

+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^
于 2013-09-06T14:11:31.473 回答