Regex 在bash中的2个字符串中查找指定字符串
你好,, 我有这样的文件: 但在我的文件中,大约有200000多行,但它是从“今天”开始到“星期一”结束的 我可以使用以下工具轻松分离一个或所有部分:Regex 在bash中的2个字符串中查找指定字符串,regex,linux,bash,awk,Regex,Linux,Bash,Awk,你好,, 我有这样的文件: 但在我的文件中,大约有200000多行,但它是从“今天”开始到“星期一”结束的 我可以使用以下工具轻松分离一个或所有部分: awk '/today/ {show=1} show; /monday/ {show=0}' file.txt 但我无法找到如何查找带有特殊字符串的节(在本例中为7768) . 有人能帮我吗 1.)每个部分都有随机数量的行 2.)文件不断变化(每天一到两次) 结果应该是这样的: today#456 7768 323
awk '/today/ {show=1} show; /monday/ {show=0}' file.txt
但我无法找到如何查找带有特殊字符串的节(在本例中为7768)
. 有人能帮我吗
1.)每个部分都有随机数量的行
2.)文件不断变化(每天一到两次)
结果应该是这样的:
today#456
7768
32347
monday
多谢各位 使用awk:
awk 'show && c{
if(show=$1==7768)print c;
c=""
}
show;
/monday/{
show=0
}
/today/{
show=1;
c=$0
}
' infile
输出:
$ awk 'show && c{if(show=$1==7768)print c;c=""}show;/monday/{show=0}/today/{show=1;c=$0}' infile
today#456
7768
32347
monday
输入:
$ cat infile
today#123
2934
9236
monday
today#12341
4246
58234
monday
today#456
7768
32347
monday
您可以为此编写bash脚本,比如
recordfinder.sh
。它可以如下所示:
# cat recordfinder.sh
#!/bin/bash
exitfn(){
echo "Usage : recordfinder.sh <filename> <searchstring>"
[ "$1" -eq 1 ] && echo "Couldn't open file" && exit 1
[ "$1" -eq 2 ] && echo "No search string provided" && exit 2
}
[ -f "$1" ] || exitfn 1
[ -z "$2" ] && exitfn 2
awk -v str="$2" -v RS="" '$0 ~ str' "$1"
# ./recordfinder.sh filename 7768
today#456
7768
32347
monday
#cat recordfinder.sh
#!/bin/bash
exitfn(){
echo“用法:recordfinder.sh”
[“$1”-eq 1]&&echo“无法打开文件”&&exit 1
[“$1”-等式2]&&echo“未提供搜索字符串”&&exit 2
}
[-f“$1”]| |退出1
[-z“$2”]&退出2
awk-v str=“$2”-v RS=“”$0~str”“$1”
#./recordfinder.sh文件名7768
今天#456
7768
32347
星期一
希望它能给你一些灵活性:-)下面的
awk
也可以帮助你。我正在设置一个名为value
的变量,在这个变量中,除了名为value的变量的值之外,您可以提供您想要查找的任何值,并且不需要在代码内部进行任何更改
awk -v value="7768" '
/monday/ && flag{
print;
flag=val=""
}
/today/{
val=$0;
next
}
$0 ~ value{
flag=1;
print val RS $0;
next
}
flag && val
' Input_file
输出如下
today#456
7768
32347
Monday
解释:现在也为上述代码添加解释
awk -v value="7768" ' ##Creating a variable named value where OP could define its variable value which OP wants to search in any line.
/monday/ && flag{ ##Searching for a string monday in any line and variable flag is NOT NULL then do following:
print; ##printing the current line then.
flag=val="" ##Nullifying the values of variable flag and val here.
}
/today/{ ##Searching for a string today here if it is found on any line then do following.
val=$0; ##Assigning current line value to variable val here.
next ##next is out of the box keyword of awk it will skip all further statements from here.
}
$0 ~ value{ ##Checking condition here if any line value is equal to variable value then do following:
flag=1; ##Making variable flag value to 1 or in other words making flag value to TRUE here.
print val RS $0; ##Printing the value of variable val with RS(record separator, whose default value is a new line) and current line then.
next ##Mentioning next will skip all further statements now.
}
flag && val ##checking condition here if variable flag and val is NOT NULL then do following.
' Input_file ##mentioning Input_file name here.
测试:
$sed-n'/today/{:a;/monday/{\n4246\n/p;b};n;ba}''/today/{show=1}(show^^$0~/srchTarget/)代码>?祝你好运。它不起作用了:/它显示语法错误,awk不知道^^^,我尝试只使用一个,但没有匹配:(也很抱歉打扰您,但您能解释一下^^^^应该怎么做吗?^抱歉,可能是个愚蠢的问题,但我是一个新来的,在第1行中找不到关于2^^^^^的任何信息。对不起,输入错误,^^
应该是&
。您好,Akshay,这看起来很酷,但它只有在字符串位于第一行时才有效。)在“今天”之后加上e。字符串可以在“今天”和“星期一”之间的任意位置,你也可以解释它为什么有效(仅适用于第一行)?对不起,我试图理解,但对初学者来说并不容易:/谢谢!!!这对我的目的来说非常有效!!但是,你也能解释为什么吗?有一个想法,awk显示了以“今天”开头的部分第二行是带“value”的行(忽略“today”和“value”之间的所有行),然后正常继续。但为了我的目的,我需要知道第一行以“today”开头的完整字符串
awk -v value="7768" ' ##Creating a variable named value where OP could define its variable value which OP wants to search in any line.
/monday/ && flag{ ##Searching for a string monday in any line and variable flag is NOT NULL then do following:
print; ##printing the current line then.
flag=val="" ##Nullifying the values of variable flag and val here.
}
/today/{ ##Searching for a string today here if it is found on any line then do following.
val=$0; ##Assigning current line value to variable val here.
next ##next is out of the box keyword of awk it will skip all further statements from here.
}
$0 ~ value{ ##Checking condition here if any line value is equal to variable value then do following:
flag=1; ##Making variable flag value to 1 or in other words making flag value to TRUE here.
print val RS $0; ##Printing the value of variable val with RS(record separator, whose default value is a new line) and current line then.
next ##Mentioning next will skip all further statements now.
}
flag && val ##checking condition here if variable flag and val is NOT NULL then do following.
' Input_file ##mentioning Input_file name here.
sed -n '/today/{:a;/monday/{/\n4246\n/p;b};N;ba}'
^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | |
dont print all lines-----+ | | | | | | | |
| | | | | | | |
if found start of block (today)+ | | | | | | |
then start loop with label (a)-----+ | | | | | |
if found end of block (monday)----------+ | | | | |
then check if patterm (4246) found---------------+ | | | |
if found, then print this buffer--------------------+ | | |
break the loop------------------------------------------+ | |
load another line into buffer--------------------------------+ |
and loop (goto label (a))---------------------------------------+
$ sed -n '/today/{:a;/monday/{/\n4246\n/p;b};N;ba}' <sample.txt
today#12341
4246
58234
monday