我有一个bash脚本,用于从xferlog获取日期,如下所示(这是示例代码):
#!/bin/bash
xferlog="/var/log/xferlog"
xfer1="test"
xfer2="test2"
xfer1last=$(tac "$xferlog" | awk -v pattern="$xfer1" '$9 ~ pattern {print; exit}')
xfer2last=$(tac "$xferlog" | awk -v pattern="$xfer2" '$9 ~ pattern {print; exit}')
day1=${xfer1last:8:2}
month1=${xfer1last:4:3}
year1=${xfer1last:20:4}
hour1=${xfer1last:11:2}
minute1=${xfer1last:14:2}
if [ "$month1" == "Jan" ];then
monthlast1=01
fi
if [ "$month1" == "Feb" ];then
monthlast1=02
fi
if [ "$month1" == "Mar" ];then
monthlast1=03
fi
if [ "$month1" == "Apr" ];then
monthlast1=04
fi
if [ "$month1" == "May" ];then
monthlast1=05
fi
if [ "$month1" == "Jun" ];then
monthlast1=06
fi
if [ "$month1" == "Jul" ];then
monthlast1=07
fi
if [ "$month1" == "Aug" ];then
monthlast1=08
fi
if [ "$month1" == "Sep" ];then
monthlast1=09
fi
if [ "$month1" == "Oct" ];then
monthlast1=10
fi
if [ "$month1" == "Nov" ];then
monthlast1=11
fi
if [ "$month1" == "Dec" ];then
monthlast1=12
fi
daylast1=$day1
if [ "$day1" == " 1" ];then
daylast1=01
fi
if [ "$day1" == " 2" ];then
daylast1=02
fi
if [ "$day1" == " 3" ];then
daylast1=03
fi
if [ "$day1" == " 4" ];then
daylast1=04
fi
if [ "$day1" == " 5" ];then
daylast1=05
fi
if [ "$day1" == " 6" ];then
daylast1=06
fi
if [ "$day1" == " 7" ];then
daylast1=07
fi
if [ "$day1" == " 8" ];then
daylast1=08
fi
if [ "$day1" == " 9" ];then
daylast1=09
fi
echo "$daylast1.$monthlast1.$year1 $hour1 $minute1"
Xferlog样本如下:
Thu Sep 2 09:52:00 2004 50 192.168.20.10 896242 /home/test/file1.tgz b _ o r suporte ftp 0 * c
我的脚本工作并告诉我我想要的东西,如下:
02.09.2004 09:52
所以我需要更改月份格式(例如,从Jan到01),如果日值是单个字符,则在前一天写入0。
对此更好的建议?也许我可以用更小的代码来做到这一点。
谢谢。
答案 0 :(得分:1)
如果您有权访问GNU
date
:
$ date -d"Thu Sep 2 09:52:00 2004" +"%d.%m.%Y %H:%M"
02.09.2004 09:52