[英]Using sed to find a range of lines that match two dates
excuse me for my English, I used google translator请原谅我的英语,我使用了谷歌翻译
I'm trying to create a script in bash to find in a file a certain range of lines that coincide with an initial and final time passed by parameters.我正在尝试在 bash 中创建一个脚本,以在文件中查找与参数传递的初始和最终时间一致的特定范围的行。
I make the call to the program as follows我按如下方式调用程序
bash script_name.sh filename.txt epoch1 epoch2
the script is incomplete and is the following脚本不完整,如下
#!/bin/bash
#--getting first and last ocurrency of sessionA
f_line=$(grep -n -m 1 SessionA "$1")
e_line=$(grep -n SessionA "$1" | tail -n1)
#-getting line number by ocurrency
n_line1=$(echo "${f_line}"|awk -F ':' '{print $1}')
n_line2=$(echo "${e_line}"|awk -F ':' '{print $1}')
#--getting first and last timestamp in file for ocurrency
epoch1=$(echo "${f_line}"|awk -F '|' '{print $1}'|awk -F ' ' '{print $14}')
epoch2=$(echo "${e_line}"|awk -F '|' '{print $1}'|awk -F ' ' '{print $14}')
cont=1
#--Comparing input epoch and in file epoch to validate file to search
if [ $2 -ge $epoch1 ] && [ $3 -le $epoch2 ]
then
#--since here i want search for a timestamp near or equals to first epoch parameter and last
#--this will be a lineal search first from head and after from tail() and i like say to sed that
#--print lines in range and only SessionA word in line
sed -n ${n_line1},${n_line2}p,/SessionA/p "$1" | tail -n1 | while read -r a_line ; do
cont=$((cont+1))
epoch_c=$(echo "${a_line}"|awk -F '|' '{print $1}'|awk -F ' ' '{print $14}')
#echo $epoch_c $2
if ! [ $epoch_c -le $2 ]
then
n_linec=$cont
echo $a_line
fi
done
fi
A file has thousands of the following lines, they are cgn traces:一个文件有成千上万的以下行,它们是 cgn 跟踪:
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionW> 1577877723|1577877746|10.247.32.280|152.207.177.36|1055|172.217.3.130|443|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionW> 1577877739|1577877746|10.242.17.186|152.206.221.168|1099|160.44.199.23|443|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionW> 1577877723|1577877746|10.247.32.28|152.207.177.36|1052|34.204.165.176|443|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionW> 1577877738|1577877746|10.247.16.24|152.207.177.202|1157|172.217.2.142|443|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionW> 1577877739|1577877746|10.247.16.24|152.207.177.202|1158|190.92.112.12|443|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionW> 1577877730|1577877746|10.247.32.24|152.207.177.36|1107|104.154.126.1|80|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionA> 1577877746|-|10.242.16.132|152.206.221.78|1167|157.185.128.201|80|6
Jan 1 06:22:25 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionA> 1577877746|-|10.242.16.132|152.206.221.78|1168|8.37.236.225|80|6
Jan 1 06:22:26 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionA> 1577877746|-|10.242.16.132|152.206.221.78|1169|8.37.236.225|80|6
Jan 1 06:22:26 11.11.20.2 1 <2020> <Jan> <01> <06:22:26> 11.11.20.2 _nohostname - <NAT444>:<SessionA> 1577877746|-|10.242.16.132|152.206.221.78|1170|8.37.236.225|80|6
The traces with sessionA are the ones that interest me and also the ones ordered by times, they are the ones that I would use to know the beginning and end of the range, knowing the line numbers I would use sed to return all the range found sessionA 的轨迹是我感兴趣的轨迹,也是按时间排序的轨迹,它们是我用来知道范围开始和结束的轨迹,知道行号我会用 sed 返回找到的所有范围
Thanks in advance提前致谢
如果我明白你想要什么,试试这样的 -
awk -F'[|: ]' -v lo="$2" -v hi="$3" '/SessionA/{ if ( $21 >= lo && $22 <= hi ) { print $0 } }' "$1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.