加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何从sed / awk文件中删除文本块(模式)

发布时间:2020-12-13 23:22:25 所属栏目:Linux 来源:网络整理
导读:我有几千个已导入的文本文件,其中包含我要删除的一段文本. 它不仅仅是一个文本块,而是一个模式. !--# Translator(s):## username1 email1# username2 email2# usernameN emailN#-- 如果出现阻止,则会列出一个或多个用户的电子邮件地址. 解决方法 我有另一个
我有几千个已导入的文本文件,其中包含我要删除的一段文本.

它不仅仅是一个文本块,而是一个模式.

<!--
# Translator(s):
#
# username1 <email1>
# username2 <email2>
# usernameN <emailN>
#
-->

如果出现阻止,则会列出一个或多个用户的电子邮件地址.

解决方法

我有另一个小的awk程序,可以在很少的代码行中完成任务.它可用于从文件中删除文本模式.可以设置启动以及停止正则表达式.

# This block is a range pattern and captures all lines between( and including )
# the start '<!--' to the end '-->' and stores the content in record $0. 
# Record $0 contains every line in the range pattern.
# awk -f remove_email.awk yourfile

# The if statement is not needed to accomplish the task,but may be useful.
# It says - if the range patterns in $0 contains a '@' then it will print
# the string "Found an email..." if uncommented.

# command 'next' will discard the content of the current record and search
# for the next record.
# At the same time the awk program begins from the beginning.


/<!--/,/-->/ {
    #if( $0 ~ /@/ ){
        # print "Found an email and removed that!"
    #}
next
}

# This line prints the body of the file to standard output - if not captured in
# the block above.
1 {
    print
}

将代码保存在“remove_email.awk”中并通过以下方式运行:?awk -f remove_email.awk你的文件

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读