繁体   English   中英

如何删除字符串第一次出现之前和最后一次出现之后的所有行?

[英]How to delete all lines before the first and after the last occurrence of a string?

猫抓.txt

My Dashboard
Fnfjfjf. random test
00:50

1:01:56
My Notes
No data found.

                                
Change Language                                                                                                                  + English                                                          

Submit


Estimation of Working Capital Lecture 1

Estimation of Working Capital Lecture 2

Estimation of Working Capital Lecture 3

Money Market Lecture 254

Money Market Lecture 255

Money Market Lecture 256

International Trade Lecture 257

International Trade Lecture 258

International Trade Lecture 259
Terms And Conditions
84749473837373
Random text fifjfofifofjfkfkf

执行以下操作后,我需要过滤此文本

  1. 删除单词第一次出现之前的所有行 - Lecture
  2. 删除最后一个单词后的所有行 - Lecture
  3. 删除所有空行

预期 output

Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259

到目前为止我尝试了什么?

cat grab.txt | sed -r '/^\s*$/d; /Lecture/,$!d'

在搜索了一些和一些试错之后,我能够删除空行并在第一次出现之前删除所有行,但在最后一次出现之后无法删除所有行。

注意 - 即使我现有的命令正在使用 sed,如果答案在 awk、perl 或 Z64A037FBAC16ZEC756C3C

请您尝试以下操作。 使用 GNU awk使用所示示例编写和测试。

awk '
/Lecture/{
  found=1
}
found && NF{
  val=(val?val ORS:"")$0
}
END{
  if(val){
    match(val,/.*Lecture [0-9]+/)
    print substr(val,RSTART,RLENGTH)
  }
}'  Input_file

说明:为上述添加详细说明。

awk '                                        ##Starting awk program from here.
/Lecture/{                                   ##Checking if a line has Lecture keyword then do following.
  found=1                                    ##Setting found to 1 here.
}
found && NF{                                 ##Checking if found is SET and line is NOT NULL then do following.
  val=(val?val ORS:"")$0                     ##Creating va and keep adding its value in it.
}
END{                                         ##Starting END block of this code here.
  if(val){                                   ##Checking condition if val is set then do following.
    match(val,/.*Lecture [0-9]+/)            ##Matching regex till Lecture digits in its value.
    print substr(val,RSTART,RLENGTH)         ##Printing sub string of matched values here to print only matched values.
  }
}' Input_file                                ##Mentioning Input_file name here.

只需使用grep 'Lecture' file和您在file中显示的输入即可:

$ grep 'Lecture' file
Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259

注意:这只是抓取包含Lecture的所有行。请参阅@RavinderSingh13 答案以防止其间出现非Lecture行)

您可以使用您选择的工具将以下正则表达式的匹配项(设置了多行标志)替换为空字符串。 正则表达式引擎只需要支持负前瞻。

\A(?:^(?!.*\bLecture\b).*\r?\n)*|^\r?\n|^.*\r?\n(?![\s\S]*\bLecture\b)

启动你的引擎!

正则表达式引擎执行以下操作。

\A                  : match beginning of string (not line)    
(?:                 : begin a non-capture group
  ^                 : match beginning of line
  (?!.*\bLecture\b) : assert the line does not contain 'Lecture'
  .*\r?\n           : match the line
)                   : end non-capture group
*                   : execute the non-capture group 0+ times
|                   : or
^\r?\n              : match an empty line
|                   : or
^.*\r?\n            : match a line
(?!                 : begin a negative lookahead
  [\s\S]*           : match 0+ characters, including line terminators
  \bLecture\b       : match 'Lecture'
)                   : end negative lookahead

从模式的第一次出现开始打印所有内容,反转文件,从模式的第一次出现开始打印所有内容,然后反转结果:

awk "/Lecture/,0" file.txt | tac | awk "/Lecture/,0" | tac

暂无
暂无

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

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