猫抢.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
执行以下操作后我需要过滤此文本
- 删除单词第一次出现之前的所有行 - 讲座
- 删除单词最后一次出现后的所有行 - 讲座
- 删除所有空行
预期产出
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 或 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
解释:对上述内容添加详细解释。
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)