使用以下方法:
grep -A1 -B1 "test_pattern" file
会在文件中匹配到的内容前后各产生一行。是否有一种方法可以显示指定数量的字符而不是行数?
我的文件中的行很长,所以我不想打印整行,而是只想观察上下文中的匹配。有没有关于如何做到这一点的建议?
前3个字符和后4个字符
$> echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}'
23_string_and
brew install homebrew/dupes/grep
,然后以ggrep
的形式运行它。 - kenorbgrep -E -o ".{0,5}test_pattern.{0,5}" test.txt
这将匹配你的模式前后最多5个字符。-o开关告诉grep仅显示匹配项,-E表示使用扩展正则表达式。确保在你的表达式周围加上引号,否则可能会被shell解释。
{0,255}
有效,而{0,256}
会出现grep: invalid repetition count(s)
的错误。 - CodeMonkey你可以使用
awk '/test_pattern/ {
match($0, /test_pattern/); print substr($0, RSTART - 10, RLENGTH + 20);
}' file
grep -o '.\{0,20\}test_pattern.\{0,20\}' file
这将在test_pattern
两侧打印最多二十个字符。 \{0,20\}
符号类似于 *
,但它指定了零到二十个重复,而不是零或多个。-o
表示仅显示匹配本身,而不是整行。
grep: Invalid content of \{\}
- Alexander Pravdinecho zzzabczzzz | grep -o -P '.abc..'
,添加或删除任意数量的点。 - barlop我永远不会容易地记住这些晦涩的命令修饰符,所以我采用顶级答案并将其转化为我的~/.bashrc
文件中的一个函数:
cgrep() {
# For files that are arrays 10's of thousands of characters print.
# Use cpgrep to print 30 characters before and after search pattern.
if [ $# -eq 2 ] ; then
# Format was 'cgrep "search string" /path/to/filename'
grep -o -P ".{0,30}$1.{0,30}" "$2"
else
# Format was 'cat /path/to/filename | cgrep "search string"
grep -o -P ".{0,30}$1.{0,30}"
fi
} # cgrep()
这是实际运行时的样子:
$ ll /tmp/rick/scp.Mf7UdS/Mf7UdS.Source
-rw-r--r-- 1 rick rick 25780 Jul 3 19:05 /tmp/rick/scp.Mf7UdS/Mf7UdS.Source
$ cat /tmp/rick/scp.Mf7UdS/Mf7UdS.Source | cgrep "Link to iconic"
1:43:30.3540244000 /mnt/e/bin/Link to iconic S -rwxrwxrwx 777 rick 1000 ri
$ cgrep "Link to iconic" /tmp/rick/scp.Mf7UdS/Mf7UdS.Source
1:43:30.3540244000 /mnt/e/bin/Link to iconic S -rwxrwxrwx 777 rick 1000 ri
所涉及的文件是一条连续的25K行,使用普通的grep
无法找到您所需要的内容。
请注意,有两种不同的方式可以调用cgrep
,类似于grep
方法。
有一种更加妙手的创建函数方式,只有在设置了"$2"时才会传递,这将节省4行代码。我现在没有方便的方式。类似于${parm2} $parm2
如果我找到它,我将修改函数和这个答案。
grep -E -o ".{0,5}test_pattern.{0,5}" test.txt
gawk
,您可以使用 match 函数: x="hey there how are you"
echo "$x" |awk --re-interval '{match($0,/(.{4})how(.{4})/,a);print a[1],a[2]}'
ere are
perl
,那么更加灵活的解决方案是:在实际模式之前打印出模式之前的三个字符,然后是模式本身,最后打印出模式之后的五个字符。echo hey there how are you |perl -lne 'print "$1$2$3" if /(.{3})(there)(.{5})/'
ey there how
echo hey there how are you |perl -lne 'print $1 if /(\w+) there/'
hey
echo hey there how are you |perl -lne 'print $2 if /(\w+) there (\w+)/'
how
以下将在模式前打印一个单词,然后是实际单词,最后是模式后的一个单词:
echo hey there how are you |perl -lne 'print "$1$2$3" if /(\w+)( there )(\w+)/'
hey there how
我个人做法与发布的答案类似。但由于点键像任何键盘键一样可以被轻敲或按住,而且我通常不需要太多上下文(如果我需要更多,我可能会像grep -C那样做行数,但通常像你一样,我不想要前后几行),所以我发现为了输入命令更快,只需轻敲点键即可输入多少个点/多少个字符,如果是几个,则轻敲该键,或者按住它进行更多操作。
例如:echo zzzabczzzz | grep -o '.abc..'
将具有一个点之前和两个点之后的abc模式。(在正则表达式语言中,点匹配任何字符)。其他人也使用点,但用花括号指定重复次数。
如果我想要严格限制在(0或x)个字符和恰好y个字符之间,那么我会使用花括号和-P,就像其他人所做的那样。
有一个设置关于点是否匹配换行符,但如果这是一个问题/兴趣,您可以研究一下。
使用 ugrep,您可以通过选项 -o
(--only-matching
)指定上下文为 -ABC
,以显示匹配项及其前后的额外字符,使匹配项加上上下文宽度不超过指定的 -ABC
。例如:
ugrep -o -C30 pattern testfile.txt
提供:
1: ... long line with an example pattern to match. The line could...
2: ...nother example line with a pattern.
-k
(--column-number
)以上下文和列号分别显示每个匹配项:
上下文宽度是显示的 Unicode 字符数(UTF-8/16/32),而不仅仅是 ASCII。