我是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
最佳答案
这将严格删除您要求的内容:
$ 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。如果需要,它也可以在其他awks中轻松实现,但是在我们确定这是否是您正在寻找的解决方案之前,不会付出任何努力。
在字符串中有坏:
$ 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......
关于linux - Linux删除文件中的多行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27164854/