我的日志文件中有几个字段,我已经能够使用以下脚本将它们转换为 CSV。
#!/bin/bash
INPUT=/home/MSC11/khanm/wwwp11dorganiser.co.uk_log
OUTPUT="/home/MSC11/khanm/output-$(basename $INPUT | cut -d'.' -f1).csv"
# field names
s=1
ip=
dt=
method=
target=
protocol=
statuscode=
reference=
echo -n "Processing..."
# empty output file
>$OUTPUT
# read input line by line
while IFS= read -r line
do
# get data
ip=$(cut -d" " -f1<<<"$line")
dt=$(cut -d" " -f4<<<"$line")
method=$(cut -d" " -f6<<<"$line")
target=$(cut -d" " -f7<<<"$line")
protocol=$(cut -d" " -f8<<<"$line")
statuscode=$(cut -d" " -f9<<<"$line")
reference=$(cut -d" " -f11<<<"$line")
# write cvs formatted output
echo "${s},${ip},${dt},${target},${method},${protocol},${statuscode},${reference}" >>$OUTPUT
# update counter
s=$(( ++s ))
done < "$INPUT"
echo "done."
echo "Total ${s} line processed and wrote to $OUTPUT cvs file."
变量“dt”的格式为 29/Nov/2007:20:42:09。我想把它分成两部分。一个日期格式为 2007 年 11 月 29 日,第二个日期格式为 20:42:09。我已经解决了剪切命令,但我无法做到这一点。任何帮助,将不胜感激。谢谢