我的日志文件中有几个字段,我可以使用以下脚本将它们转换为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。我想把它分成两部分。一个是格式日期为29 / Nov / 2007,第二个格式为20:42:09。我已经解决了切割命令,但我无法做到这一点。任何帮助,将不胜感激。感谢
答案 0 :(得分:3)
cut -d: -f1 <<< $dt
29/Nov/2007
cut -d: -f2- <<< $dt
20:42:09