【问题标题】:How to change date format in .csv file in unix如何在 unix 中更改 .csv 文件中的日期格式
【发布时间】:2013-09-06 12:03:07
【问题描述】:

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

谢谢!!

【问题讨论】:

    标签: linux shell csv unix unix-timestamp


    【解决方案1】:

    您可以使用-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^
    

    【讨论】:

    • +1 .... 但是你的回答没有解决 OP 的问题,OP 想要 8/10/13 -> 2013-08-01 不是 2013-08-10 :D :D。 ..(我相信这是一个错字)
    • 哈哈那我们可以试试echo 2013-08-01 :))
    • @Dileep 检查更新的答案。我添加了一个条件来跳过没有日期的行。
    • @fedorqui 我不能一次提供多个文件吗?错误:-bash:KeywordsClicksgreaterthan0*.csv:不明确的重定向
    • @Dileep 不,表达式while ... do ... done &lt; file 一次只接受一个文件。你可以用for file in KeywordsClicksgreaterthan0*.csv; do while ... do ... done &lt; $file; done之类的东西将我的代码包装在一个更大的块中。
    【解决方案2】:

    这是我的解决方案。

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

    将其保存到文件并运行bash script.sh &lt; your_file &gt; 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^
    

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 2018-05-22
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多