匹配前后的 Grep 字符? [英] Grep characters before and after match?

查看:23
本文介绍了匹配前后的 Grep 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这个:

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?

推荐答案

前 3 个字符后 4 个字符

3 characters before and 4 characters after

$> echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}'
23_string_and

这篇关于匹配前后的 Grep 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆