linux删除文件中的多行

时间:2014-11-27 07:02:28

标签: linux sed

我是iOS开发人员,不熟悉Linux,我搜索了很长时间但仍然无法解决我的问题。我希望有人可以帮助我,谢谢!

删除前:

Other contents......

#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

Other contents......

删除后:

Other contents......
Other contents......

更新

我正在编写 shell 脚本(不能使用其他语言)。在用户系统上有一个名为abc的文本文件,内容可能是这样的:

# Other comments......
# Other comments......
# Other comments......
Other contents......
# Other comments......

#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

# Other comments......
Other contents......
# Other comments......
# Other comments......

哪个Other contents ......是虚构的,不能将其用作已删除的条件。

我只需要删除以下部分:

#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

不需要删除此文件中的其他内容。

因为它在用户的系统上运行,所以我不知道要删除的内容的行号。

最后看起来像这样:

# Other comments......
# Other comments......
# Other comments......
Other contents......
# Other comments......

# Other comments......
Other contents......
# Other comments......
# Other comments......

我认为答案可能如下:

sed xxxxxx abc.txt

awk xxxxxx abc.txt

4 个答案:

答案 0 :(得分:2)

这是一个Perl解决方案。

perl -0777 -pe 's/\n+(#.*\n)*\n*function\s+start_proxy\s*\{(.*\n)+?function\s+stop_proxy\s*\{(.*\n)+?\s*\}//'

与之前发布的sed解决方案相比,它更精确,也更省内存,因为它在处理之前将整个文件读入内存。

正则表达式试图寻找

  • \n+之前的所有空行...
  • (#.*\n)* ...任意数量的评论行,以#开头,后跟......
  • \n* ...之前可选的空行...
  • function\s+start_proxy\s*\{
  • (.*\n)+? ...尽可能少的行到......
  • function\s+stop_proxy\s\{
  • (.*\n)+? ...并且尽可能少的行直到......
  • \s*\} ...以下结束括号。

量词+?寻找最短的匹配,而不仅仅是+,它总是抓住最长的匹配(贪婪的匹配)。

答案 1 :(得分:1)

您可以使用行号范围删除不必要的行。

示例:

sdlcb@Goofy-Gen:~/AMD/SO$ cat File
This is Line 1
This is Line 2
This is Line 3
This is Line 4
This is Line 5
This is Line 6
This is Line 7
sdlcb@Goofy-Gen:~/AMD/SO$ sed -i.bak '3,6d' File
sdlcb@Goofy-Gen:~/AMD/SO$ cat File
This is Line 1
This is Line 2
This is Line 7

删除第3到第6行。

否则:

sed -n '/^Other contents/,/^Other contents/{ /^Other contents/!d; /^Other contents/!d; p; }' File

<强>详细信息:

-n option => do not print
/a/,/b/ => select lines between the lines matching /a/ and /b/ (inclusive)
/a/!d => delete lines not matching /a/
/b/!d => delete lines not matching /b/
in our case a and b = '^Other contents' which means beginning with 'Other contents'
p => print

示例:

sdlcb@Goofy-Gen:~/AMD/SO$ cat File
Other contents......

#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

Other contents......
sdlcb@Goofy-Gen:~/AMD/SO$ sed -n '/^Other contents/,/^Other contents/{ /^Other contents/!d; /^Other contents/!d; p; }' File
Other contents......
Other contents......

答案 2 :(得分:1)

我会做像

这样的事情
$ sed '/^#/d; /^function/, /}/ d; /^$/d ' input
Other contents......
Other contents......

它的作用是什么?

  • d删除模式空间。

  • ^#匹配以#

  • 开头的行
  • /^function/, /}/指定范围

  • ^$匹配空行

修改

$ sed ' /^#$/d; /^# Proxy/, /}/d;  /function stop_proxy/,  /}/ d ' input
# Other comments......
# Other comments......
# Other comments......
Other contents......
# Other comments......



# Other comments......
Other contents......
# Other comments......
# Other comments......

注意上述解决方案有一个限制,即删除没有内容的空注释

#

答案 3 :(得分:1)

这将严格删除您要求的内容:

$ cat bad
#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

$ cat file
# Other comments......
# Other comments......
# Other comments......
Other contents......
# Other comments......

#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

# Other comments......
Other contents......
# Other comments......
# Other comments......

$ awk -v RS='^$' -v ORS= 'NR==FNR{bad=$0;next} s=index($0,bad){$0=substr($0,1,s-1) substr($0,s+length(bad))} 1' bad file
# Other comments......
# Other comments......
# Other comments......
Other contents......
# Other comments......

# Other comments......
Other contents......
# Other comments......
# Other comments......
$

它使用GNU awk进行多字符RS。如果需要的话,它也很容易在其他方面做到,但在我们发现这是否是您正在寻找的解决方案之前不会付出努力。

字符串中的错误:

$ awk -v RS='^$' -v ORS= -v bad="#
# Proxy server for iTerm & Terminal Setup Script
# version 0.1
# VincentSit
# Nov 26, 2014
#
function start_proxy {
 export http_proxy='abc.efgh.com:1234'
 export HTTPS_PROXY='abc.efgh.com:1234'
 }

function stop_proxy {
 export http_proxy=''
 export HTTPS_PROXY=''
 }

" 's=index($0,bad){$0=substr($0,1,s-1) substr($0,s+length(bad))} 1' file
# Other comments......
# Other comments......
# Other comments......
Other contents......
# Other comments......

# Other comments......
Other contents......
# Other comments......
# Other comments......