在bash代码中使用sed在注释块后添加一个空行。

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

Adding an empty newline after a comment block in bash code using sed

问题

我正在尝试使用sed重新格式化一系列相当大的bash脚本。

它们包含很多类似于以下内容的代码:

# 这里是第一个注释

# 这里是第二个注释
if [[ -n "$some_variable" ]]; then
    do_something
fi

或者:

# 这里是第一个注释

# 这里是第二个注释
# 这里是第三个注释
if [[ -n "$some_variable" ]]; then
    do_something
fi

我应该如何利用sed在注释(即以#开头的行)和任何非注释代码之间添加一个换行,就像这样:

# 这里是第一个注释

# 这里是第二个注释
                                          <--- 由sed插入的新行
if [[ -n "$some_variable" ]]; then
    do_something
fi

或者,对于第二个示例:

# 这里是第一个注释

# 这里是第二个注释
# 这里是第三个注释
                                          <--- 由sed插入的新行
if [[ -n "$some_variable" ]]; then
    do_something
fi
英文:

I am trying to reformat a series of rather large bash scripts using sed.

They contain a lot of code that looks something like this:

# first comment goes here

# second comment goes here
if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

or:

# first comment goes here

# second comment goes here
# third comment goes here
if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

How can I leverage sed to add a newline in-between a comment (so a line that starts with #) and any non-comment code, like so?:

# first comment goes here

# second comment goes here
                                      &lt;--- newline inserted here by sed
if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

or, for the second example:

# first comment goes here

# second comment goes here
# third comment goes here
                                      &lt;--- newline inserted here by sed
if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

答案1

得分: 1

使用任何awk:

$ awk '{pCom=cCom; cCom=/^#/} NF && !cCom && pCom{$0=ORS $0} 1' file
# 第一个注释在这里

# 第二个注释在这里

if [[ -n "$some_variable" ]]; then
    做某事
fi

或者:

# 第一个注释在这里

# 第二个注释在这里
# 第三个注释在这里

if [[ -n "$some_variable" ]]; then
    做某事
fi

以上是在此输入文件上运行的:

$ cat file
# 第一个注释在这里

# 第二个注释在这里
if [[ -n "$some_variable" ]]; then
    做某事
fi

或者:

# 第一个注释在这里

# 第二个注释在这里
# 第三个注释在这里
if [[ -n "$some_variable" ]]; then
    做某事
fi

如果您更喜欢您的脚本更难理解,您可以将以上内容编写为:

```shell
awk '{p=c;c=/^#/}NF&&!c&&p{$0=ORS$0}1' file
英文:

Using any awk:

$ awk &#39;{pCom=cCom; cCom=/^#/} NF &amp;&amp; !cCom &amp;&amp; pCom{$0=ORS $0} 1&#39; file
# first comment goes here

# second comment goes here

if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

or:

# first comment goes here

# second comment goes here
# third comment goes here

if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

The above was run on this input file:

$ cat file
# first comment goes here

# second comment goes here
if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

or:

# first comment goes here

# second comment goes here
# third comment goes here
if [[ -n &quot;$some_variable&quot; ]]; then
    do_something
fi

If you prefer your scripts more indecipherable you could write the above as:

awk &#39;{p=c;c=/^#/}NF&amp;&amp;!c&amp;&amp;p{$0=ORS$0}1&#39; file

答案2

得分: 1

这可能适用于你(GNU sed):

sed '/^\s*#/{:a;n;//ba;/\S/{x;p;x}}' file

如果一行以注释开头,则打印该行并获取下一行。

如果该行以注释开头,则重复上述步骤。

否则,如果该行不为空,则打印一个空行。

始终打印当前行。

英文:

This might work for you (GNU sed):

sed &#39;/^\s*#/{:a;n;//ba;/\S/{x;p;x}}&#39; file

If a line begins with a comment, print it and fetch the next line.

If the that line begins with a comment, repeat above.

Otherwise, if the line is not empty, print an empty line.

Always print the current line.

答案3

得分: 0

看起来很简单。

sed '/^[^ \t]*\(#\|$\)/{
     { 
        :comment
        n
        //bcomment
     }
     s/^/\n/
}'
英文:

Looks simple.

sed &#39;/^[^ \t]*\(#\|$\)/{
     { 
        :comment
        n
        //bcomment
     }
     s/^/\n/
}&#39;

答案4

得分: 0

for __ in 

   # 第一个注释在这里
   
   # 第二个注释在这里
   if [[ -n "$some_variable" ]]; then
       做一些事情
   fi 

   # 第一个注释在这里   
   
   # 第二个注释在这里
   # 第三个注释在这里
   if [[ -n "$some_variable" ]]; then
       做一些事情
   fi ; do printf '%s' "$__" | 
---  
>     mawk 'NF-!_ || ($!NF = RS $_)(FS = "$")' FS='^#'
---
done

     1	# 第一个注释在这里
     2	
     3	# 第二个注释在这里
     4	
     5	if [[ -n "$some_variable" ]]; then
     6	
     7	    做一些事情
     8	
     9	fi
    
     1	# 第一个注释在这里   
     2	
     3	# 第二个注释在这里
     4	# 第三个注释在这里         
     5	
     6	if [[ -n "$some_variable" ]]; then
     7	
     8	    做一些事情              
     9	
    10	fi
英文:
for __ in 

   &#39;# first comment goes here
    
    # second comment goes here
    if [[ -n &quot;$some_variable&quot; ]]; then
        do_something
    fi&#39; 

   &#39;# first comment goes here   

    # second comment goes here
    # third comment goes here
    if [[ -n &quot;$some_variable&quot; ]]; then
        do_something
    fi&#39;               ; do printf &#39;%s&#39; &quot;$__&quot; | 

> mawk 'NF-!_ || ($!NF = RS $_)(FS = "$")' FS='^#'

done

     1	# first comment goes here
     2	
     3	# second comment goes here
     4	
     5	if [[ -n &quot;$some_variable&quot; ]]; then
     6	
     7	    do_something
     8	
     9	fi

     1	# first comment goes here   
     2	
     3	# second comment goes here
     4	# third comment goes here         
     5	
     6	if [[ -n &quot;$some_variable&quot; ]]; then
     7	
     8	    do_something              
     9	
    10	fi

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

发表评论

匿名网友

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

确定