bash 匹配前后的 Grep 字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8101701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grep characters before and after match?
提问by Legend
Using this:
使用这个:
grep -A1 -B1 "test_pattern" file
will produce one line before and after the matched pattern in the file. Is there a way to display not lines but a specified number of characters?
将在文件中匹配的模式前后产生一行。有没有办法不显示行而是显示指定数量的字符?
The lines in my file are pretty big so I am not interested in printing the entire line but rather only observe the match in context. Any suggestions on how to do this?
我的文件中的行非常大,所以我对打印整行不感兴趣,而只是在上下文中观察匹配。关于如何做到这一点的任何建议?
回答by ДМИТРИЙ МАЛИКОВ
3 characters before and 4 characters after
前 3 个字符和后 4 个字符
$> echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}'
23_string_and
回答by ekse
grep -E -o ".{0,5}test_pattern.{0,5}" test.txt
This will match up to 5 characters before and after your pattern. The -o switch tells grep to only show the match and -E to use an extended regular expression. Make sure to put the quotes around your expression, else it might be interpreted by the shell.
这将匹配您的模式前后最多 5 个字符。-o 开关告诉 grep 只显示匹配和 -E 使用扩展的正则表达式。确保在表达式周围加上引号,否则它可能会被 shell 解释。
回答by amit_g
You could use
你可以用
awk '/test_pattern/ {
match(grep -o '.\{0,20\}test_pattern.\{0,20\}' file
, /test_pattern/); print substr( x="hey there how are you"
echo "$x" |awk --re-interval '{match(echo hey there how are you |perl -lne 'print "" if /(.{3})(there)(.{5})/'
ey there how
,/(.{4})how(.{4})/,a);print a[1],a[2]}'
ere are
, RSTART - 10, RLENGTH + 20);
}' file
回答by ruakh
You mean, like this:
你的意思是,像这样:
echo hey there how are you |perl -lne 'print if /(\w+) there/'
hey
?
?
That will print up to twenty characters on either side of test_pattern
. The \{0,20\}
notation is like *
, but specifies zero to twenty repetitions instead of zero or more.The -o
says to show only the match itself, rather than the entire line.
这将在test_pattern
. 该\{0,20\}
标记是一样*
的,但指定零到二十重复,而不是零或more.The-o
说,只显示了比赛本身,而不是整条生产线。
回答by P....
With gawk
, you can use match function:
使用gawk
,您可以使用匹配功能:
echo hey there how are you |perl -lne 'print if /(\w+) there (\w+)/'
how
If you are ok with perl
, more flexible solution : Following will print three characters before the pattern followed by actual pattern and then 5 character after the pattern.
如果您同意perl
,更灵活的解决方案:以下将在模式前打印三个字符,然后是实际模式,然后是模式后的 5 个字符。
echo hey there how are you |perl -lne 'print "" if /(\w+)( there )(\w+)/'
hey there how
This can also be applied to words instead of just characters.Following will print one word before the actual matching string.
这也可以应用于单词而不仅仅是字符。以下将在实际匹配字符串之前打印一个单词。
echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}' | grep string
Following will print one word after the pattern:
以下将在模式后打印一个单词:
##代码##Following will print one word before the pattern , then the actual word and then one word after the pattern:
以下将在模式之前打印一个单词,然后是实际单词,然后是模式之后的一个单词:
##代码##