如何使用bash将此日期和时间格式更改为yyyy-mm-dd hh:mm:ss,这是来自apache日志文件

时间:2017-11-17 16:19:57

标签: regex bash awk

49,10.96.25.156,1274x40x1,-,02/Nov/2017:21:14:31,http-nio-443-exec-20,"POST/rest/webResources/1.0/resourcesHTTP/1.1",503,40,0,"-","SilkPerformer17.0""-"
50,10.96.25.156,1274x41x1,-,02/Nov/2017:21:14:31,http-nio-443-exec-25,"POST/rest/webResources/1.0/resourcesHTTP/1.1",503,40,0,"-","SilkPerformer17.0""9myyx9"

这是日志文件的外观,任何帮助将不胜感激。谢谢!

4 个答案:

答案 0 :(得分:1)

您可以使用dateawk进行转换。 如果您安装了dateutils软件包,则可以使用

这是一个简单的命令
dateutils.dconv -S -i "%d/%b/%Y:" -f "%F " < file.log
  • -i指定输入日期格式
  • -f指定输出日期格式
  • -S sed模式,仅处理匹配的日期字符串

输入

49,10.96.25.156,1274x40x1,-,02/Nov/2017:21:14:31,http-nio-443-exec-20

输出

49,10.96.25.156,1274x40x1,-,2017-11-02 21:14:31,http-nio-443-exec-20

答案 1 :(得分:1)

这是一个纯粹的awk解决方案:

vim /usr/sap/SAPBusinessOne/ServiceLayer/conf/b1s.conf

(...)
"CorsEnable": true,
 "CorsAllowedOrigins": "*"}

使用它:

#!/usr/bin/env awk -f

BEGIN {
  FS = OFS = ","

  # build our lookup of zero-padded month numbers
  split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", marr, "|")
  for (i = 1; i <= 12; i++) {
    mhash[marr[i]] = sprintf("%02d", i)
  }
}

{
  # 1:dd 2:mmm 3:yyyy 4:hh 5:mi 6:ss
  split($5, dtarr, "[:/]")

  # replace the column with our reformatted date
  $5 = dtarr[3] "-" mhash[dtarr[2]] "-" dtarr[1] " " dtarr[4] ":" dtarr[5] ":" dtarr[6]

  # print the whole line
  print
}

或者你可以让它可执行:

$ awk -f redate.awk access_log
49,10.96.25.156,1274x40x1,-,2017-11-02 21:14:31,http-nio-443-exec-20,"POST/rest/webResources/1.0/resourcesHTTP/1.1",503,40,0,"-","SilkPerformer17.0""-"

直接运行:

$ chmod +x redate.awk

答案 2 :(得分:0)

仅限bash转换:

declare -A m
m=([Jan]=01 [Feb]=02 [Mar]=03 [Apr]=04 [May]=05 [Jun]=06 [Jul]=07 [Aug]=08 [Set]=09 [Oct]=10 [Nov]=11 [Dec]=12)
IFS='/:' read -r -a a <<< '02/Nov/2017:21:14:31'
echo "${a[2]}-${m[${a[1]}]}-${a[0]} ${a[3]}:${a[4]}:${a[5]}"

修改

我的意思是这样读取文件并分割行

printline() {
    local l=("$@")
    echo -n "${l[0]}"
    for f in "${l[@]:1}"
    do
        echo -n ",$f"
    done
    echo
}

declare -A m
m=([Jan]=01 [Feb]=02 [Mar]=03 [Apr]=04 [May]=05 [Jun]=06 [Jul]=07 [Aug]=08 [Set]=09 [Oct]=10 [Nov]=11 [Dec]=12)
while read line
do
    IFS=',' read -r -a l <<< "$line"
    IFS='/:' read -r -a a <<< ${l[4]}
    l[4]="${a[2]}-${m[${a[1]}]}-${a[0]} ${a[3]}:${a[4]}:${a[5]}"
    printline "${l[@]}"
done

答案 3 :(得分:0)

使用日期进行转换可能就像这样

'providers' => [
    ...
    'GoogleMaps\ServiceProvider\GoogleMapsServiceProvider',
]

'aliases' => [
    ...
    'GoogleMaps' => 'GoogleMaps\Facade\GoogleMapsFacade',
]

首先,我们将日期字段更改为日期能够理解的内容。 然后我们根据需要选择输出格式。