合并多个sed命令效果不佳。

huangapple go评论47阅读模式
英文:

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 &#39;s/&lt;!--.*?--&gt;//g;/&lt;!--/N;//ba&#39;

Remove blank lines by: sed &#39;/^\s*$/d&#39;

Remove leading spaces by: sed &#39;s/^\s*//g&#39;

Remove trailing spaces by: sed &#39;s/\s*$//g&#39;

Merging all lines into one by: sed &#39;:a; N; $!ba; s/\n//g&#39;

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 &#39;s/&lt;!--.*?--&gt;//g;/&lt;!--/N;//ba; /^\s*$/d; s/^\s*//g; s/\s*$//g; :a; N; $!ba; s/\n//g&#39; 

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.

huangapple
  • 本文由 发表于 2023年6月16日 11:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定