换行grep查找字符串 [英] Grep search strings with line breaks

查看:90
本文介绍了换行grep查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用grep在下面给出的输入文件字符串'导出到Excel'的输出出现?具体来说,如何处理搜索字符串之间发生的换行符?是否有一个grep的交换机可以做这件事或其他一些命令可能?

输入文件:

文件a.txt中:


  

等等等等......出口结果
  Excel的...结果
  等等等等..


文件b.txt:


  

等等等等...导出到Excel ...结果
  等等等等..



解决方案

你只是想找个包含模式,忽略换行符,或者你想真正看到的匹配行的文件?

如果是前者,你可以使用 TR 来换行转换为空格:

  TR的'\\ n'''| grep的导出到Excel

如果是后者,你可以做同样的事情,但你可能需要使用-o标志仅打印实际相符。那么你要调整你的正则表达式,包括你想要的任何额外的上下文。

How to use grep to output occurrences of the string 'export to excel' in the input files given below? Specifically, how to handle the line breaks that happen in between the search strings? Is there a switch in grep that can do this or some other command probably?

Input files:

File a.txt:

blah blah ... export to
excel ...
blah blah..

File b.txt:

blah blah ... export to excel ...
blah blah..

解决方案

Do you just want to find files that contain the pattern, ignoring linebreaks, or do you want to actually see the matching lines?

If the former, you can use tr to convert newlines to spaces:

tr '\n' ' ' | grep 'export to excel'

If the latter you can do the same thing, but you may want to use the -o flag to only print the actual match. You'll then want to adjust your regex to include any extra context you want.

这篇关于换行grep查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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