Sac*_*hin 7 regex awk grep sed
抓猫.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
Run Code Online (Sandbox Code Playgroud)
执行以下操作后,我需要过滤此文本
预期输出
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
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试了什么?
cat grab.txt | sed -r '/^\s*$/d; /Lecture/,$!d'
Run Code Online (Sandbox Code Playgroud)
在搜索了一点和一些试错之后,我能够删除空行并删除第一次出现之前的所有行,但无法删除最后一次出现之后的所有行。
注意 - 即使我现有的命令使用的是 sed,如果答案是 awk、perl 或 grep,也没关系
你能不能试试以下。使用 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
Run Code Online (Sandbox Code Playgroud)
说明:为以上添加详细说明。
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.
Run Code Online (Sandbox Code Playgroud)
只需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
Run Code Online (Sandbox Code Playgroud)
(注意:这只是抓取所有包含 的行Lecture。请参阅@RavinderSingh13 答案以防止Lecture其间出现非行)