英文:
Merging various sed commands is not working well
问题
我正在尝试使用sed命令按以下步骤压缩HTML文件:
使用sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba'
命令删除注释。
使用sed '/^\s*$/d'
命令删除空白行。
使用sed 's/^\s*//g'
命令删除开头的空格。
使用sed 's/\s*$//g'
命令删除末尾的空格。
使用sed ':a; N; $!ba; s/\n//g'
命令将所有行合并成一行。
上述命令来自网络上不同来源,包括Stackoverflow的答案。
当我使用管道(|
)运算符链接上述命令时,它运行得非常好。然而,当我尝试使用单个sed命令时,某些功能不起作用:
sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba; /^\s*$/d; s/^\s*//g; s/\s*$//g; :a; N; $!ba; s/\n//g'
注释没有被移除,空格也没有被删除,尽管所有行都被合并成一行。
问题出在哪里,如何解决?
英文:
I am trying to compress an html file with following steps using sed command:
Remove comments by sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba'
Remove blank lines by: sed '/^\s*$/d'
Remove leading spaces by: sed 's/^\s*//g'
Remove trailing spaces by: sed 's/\s*$//g'
Merging all lines into one by: sed ':a; N; $!ba; s/\n//g'
Above commands have been taken from different sources on the web including Stackoverflow answers.
When I chain above commands using pipe (|
) operator, it works very well. However, when I try to use a single sed command, some functions are not working:
sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba; /^\s*$/d; s/^\s*//g; s/\s*$//g; :a; N; $!ba; s/\n//g'
The comments are not removed and the blank spaces are also not removed, though all the lines are getting merged into one.
Where is the problem and how can it be solved?
答案1
得分: 3
Where is the problem
问题在于某些命令旨在在文件的每一行上工作,而 N
将下一行附加到模式空间中,b
修改了脚本执行流程。这导致进一步的命令仅在文件的末尾执行,没有更多可读的行了。
注意:要解析 XML 文件,请使用 XML 解析器,而不是 sed。
how can it be solved?
可以通过“编程”来解决 - 逐个分析 sed 命令的目的,分析每个 sed 脚本的预期流程,并重写代码,以便按预期工作。
英文:
> Where is the problem
The problem is that some commands are intended to work on each line of the file, while N
appends the next line into pattern space and b
modifies script execution flow. This causes further commands to be executed only on the end of the file, with no more lines available to read.
Note: to parse XML files use XML parser, not sed.
> how can it be solved?
It can be solved by "programming" - analyzing the purpose of the sed commands one by one and analyzing the intended flow of each sed script and rewriting the code that will work as intended.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论