简体   繁体   English

使用 sed 查找匹配两个日期的一系列行

[英]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.

相关问题 在Linux中使用sed在两行中匹配文本 - Match the text in two lines using sed in linux 使用sed在行范围之间查找和替换文件中的文本 - Find and replace text in a file between range of lines using sed 我需要使用 sed 查找匹配项并删除该匹配项之前的 2 行和之后的 3 行 - I need to find a match using sed and deletes 2 lines before this match and 3 lines after it 尝试使用 sed 删除范围内的某些行 - Trying to Delete Certain Lines in a Range Using sed 使用sed和regex匹配不同的行 - Using sed with regex to match varying lines sed 如果一行匹配条件,则替换与模式范围匹配的行 - sed to replace lines matched by a pattern range if one line match a condition 如何使用SED在文件的两个连续行中搜索两个不同的模式,并在模式匹配后打印下4行? - How can I search for two different patterns in two consecutive lines in a file using SED and print next 4 lines after pattern match? 如何使用Sed删除之前的5行和模式匹配之后的6行? - How to delete 5 lines before and 6 lines after pattern match using Sed? fIm 试图在两个匹配模式之间找到一个字符串,然后使用 sed 在一个模式之前添加该字符串 - fIm trying to find a string betwen two match patterns and then add that string before a pattern using sed 查找并修改:删除行 - find & sed: remove lines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM