我有我的日志文件中的几个字段,我已经能够使用下面的脚本将它们转换为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."
variables“dt”是格式29 / Nov / 2007:20:42:09。 我想把它分成两部分。 一个格式为29 / Nov / 2007,第二个格式为20:42:09。 我有围绕削减命令,但我一直无法做到这一点。 任何帮助,将不胜感激。 谢谢
cut -d: -f1 <<< $dt 29/Nov/2007 cut -d: -f2- <<< $dt 20:42:09