简体   繁体   English

如何从Linux中两个相似字符串之间的文件中提取文本?

[英]How to extract the text from a file between two similar strings in linux?

i Have a file that contains text as below 我有一个包含以下内容的文件

Jun  9 2014
some text...
Jun 10 2014
some text...
some text...
Jun 10 2014
some text...
Jun 10 2014
some text...
Jun 11 2014
some text...
Jun 12 2014
some text...

I have tried with sed command something like this: 我已经尝试使用sed命令,如下所示:

sed /"Jun 10 2014"/,/"Jun 10 2014"/p file_name.txt

But this will give only the text between first and second string Jun 10 2014 但这只会提供第一和第二个字符串之间的文本2014年6月10日

I want to extract all the text between the 1st occurrence of the string Jun 10 2014 to the last occurrence of the same string Jun 10 2014, including the text followed by the last Jun 10 2014 up to the text Jun 11 2014. 我想提取字符串Jun 10 2014的第一个匹配项到同一字符串Jun 10 2014的最后一个匹配项之间的所有文本,包括其后跟最后一个Jun 10 2014直到文本Jun 11 2014的文本。

You can use the range from Jun 10 2014 to Jun 11 2014 , but do additional filtering inside: 您可以使用Jun 10 2014Jun 11 2014 Jun 10 2014Jun 10 2014Jun 11 2014 ,但可以在内部进行其他过滤:

$ sed -n '/Jun 10 2014/,/Jun 11 2014/ {/Jun 1[01] 2014/!p}' file_name.txt
some text...
some text...
some text...
some text...

Here is an awk solution: 这是一个awk解决方案:

awk '/^Jun 10/ {f=1} /^Jun/ && !/^Jun 10/ {f=0} f && !/^Jun/' file
some text...
some text...
some text...
some text...

This prints from Jun 10 until its no more Jun 10 这从打印Jun 10 ,直到其没有更多的Jun 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM