有关linuxjishu/13830.html target=_blank class=infotextkey>awk日期格式与提取的代码,研究awk的不错的小例子。
一、给定的时间区间提取目标行:
复制代码 代码如下:
start="2011 09 07 09 30 00"
end="2011 09 07 10 00 59"
awk '$1~/ERROR/{ print $0} ' catalina.out | awk -F '[-: ]+' -v s="$start" -v e="$end" 'mktime($2" "$3" "$4" "$5" "$6" "substr($7,1,2))>=mktime(s)&&mktime($2" "$3" "$4" "$5" "$6" "substr($7,1,2))<=mktime(e)'
二、提取时间段:
复制代码 代码如下:
#!/bin/bash
start="2011 09 07 09 30 00"
end="2011 09 07 10 00 59"
awk -F '[-: ]+' -v s="$start" '{ if ( $1~/ERROR/ && mktime($2" "$3" "$4" "$5" "$6" "substr($7,1,2))
>=mktime(s)) print NR
}' catalina.out > tmp1
awk -F '[-: ]+' -v e="$end" '{ if ( $1~/ERROR/ && mktime(e)>=mktime($2" "$3" "$4" "$5" "$6"
"substr($7,1,2))) print NR }
' catalina.out > tmp2
t1=`head -n 1 tmp1`
t2=`tail -n 1 tmp2`
sed -n ''$t1','$t2'p' catalina.out
rm -rm tmp1
rm -rm tmp2