好的,我有一些需要帮助的代码
使用AWK扫描文件并提取IP地址192.168.122.1的活动行
打印出3行输出
a) date/time first activity on the IP address was detected
b) date/time last activity on the IP address was detected
c) Total number of events detected on the IP address 发布于 2017-08-16 09:18:41
根据你到目前为止所说的,这样的东西可能对你有用:
# find all lines containing the IP
grep -F 192.168.122.1 FILE > tmp
head -n1 tmp # print first such line
tail -n1 tmp # print last such line
wc -l tmp # count the number of such lines如果你必须使用awk,这里有一种方法:
# invoke as:
# awk -f this_file.awk FILE
BEGIN {
count = 0
}
/192\.168\.122\.1/ {
if (count == 0) {
print $0 # print the first line containing the IP
last = $0 # in case the first line also happends to be the last
count = 1
} else {
count += 1 # record that another line contained the IP
last = $0 # remember this line in case it ends up being the last
}
}
END {
if (count > 0) {
print last # print the last line containing the IP
}
print count
}https://stackoverflow.com/questions/45703664
复制相似问题