删除在线评论,但仅在阵列和前11个事件之后

发布于 2025-02-07 10:33:57 字数 2374 浏览 0 评论 0原文

在脚本中对我的元语句的文献过多。 有很多。它们都从这样的东西开始:

#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

...

# Script continues
outsidethewire=( "key 971" ) # Leave this comment here
somearray=( "sliced apples" "pie dough" ) # Mother's secret recipe

...我需要它成为这个...

#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch )
kingdom=( "Island" )
zipcode=( 90210 )
datatype=( 0-9- )
favoritepie=( "Cherry" )
aoptions=( "Home address" "Work address" "Mobile" )
boptions=(  )
aopttypes=( string string phonenum )
bopttypes=(  )
sourced=(  )
subscripts=( installusr )

...

# Script continues
outsidethewire=( "key 971" ) # Leave this comment here
somearray=( "sliced apples" "pie dough" ) # Mother's secret recipe
  • 只有11行这样的行,他们首先是这样。 以后脚本中的任何这样的评论都必须单独保留。
  • 在这些行之前可能会有注释,但是在脚本到脚本之间,11个数组之前的行数量都不同。
  • 每个评论在一致的模式)# ...之后开始,

我需要

在这些数组语句之后删除评论。

我拥有的

可以运行的...

sed 's/ ) # .*/ )/' *

但是,我想将其限制为每个文件的前11个发生。

来自这个答案我有一个模式可以匹配第一场比赛,给了我这个

sed '0,/ ) # .*/s// )/' *

...适用于第一次发生。

我可以将其放入一个循环中:

#!/bin/bash

counter=1

while [ "$counter" -le "11" ]; do

  sed '0,/ ) # .*/s// )/' *
  counter=$(expr $counter + 1)

done

这是“适当的”吗?

该循环假设所有文件都将均匀匹配,为所有文件均值运行。如果可能的话,我希望循环为每个文件运行,而不是基于计数器的所有文件。但是,我不确定该怎么做。

这是最好的方法吗?或者,是否使用其他Linux工具有更“正确的”,故障安全的方式?

What I have

I over-documented my meta statements in my scripts. There are many. They all start with something like this:

#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

...

# Script continues
outsidethewire=( "key 971" ) # Leave this comment here
somearray=( "sliced apples" "pie dough" ) # Mother's secret recipe

...I need it to become this...

#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch )
kingdom=( "Island" )
zipcode=( 90210 )
datatype=( 0-9- )
favoritepie=( "Cherry" )
aoptions=( "Home address" "Work address" "Mobile" )
boptions=(  )
aopttypes=( string string phonenum )
bopttypes=(  )
sourced=(  )
subscripts=( installusr )

...

# Script continues
outsidethewire=( "key 971" ) # Leave this comment here
somearray=( "sliced apples" "pie dough" ) # Mother's secret recipe
  • There are only 11 lines like this and they are first like this. Any such comments later in the scripts must be left alone.
  • There might be comments before these lines, but the number of lines before the 11 arrays varies from script to script.
  • Every comment starts after the consistent pattern ) #...

What I need

I need to remove the comments after these array statements.

What I have

I can run this...

sed 's/ ) # .*/ )/' *

But, I want to limit that to only the first 11 occurrences per file.

From this answer I get a pattern to match the first single match, giving me this...

sed '0,/ ) # .*/s// )/' *

...but that only works for the first occurrence.

I could put it into a loop:

#!/bin/bash

counter=1

while [ "$counter" -le "11" ]; do

  sed '0,/ ) # .*/s// )/' *
  counter=$(expr $counter + 1)

done

Is that 'proper'?

This loop makes the assumption that all files will match evenly, running blankly for all files. If possible, I'd like the loop to run for each file, not for all files based on a counter. But, I'm not sure how to do that.

Is that the best way to do this? Or, is there a more "proper", fail-safe way using other Linux tools?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2025-02-14 10:33:57

如果我正确理解了这一点,则您要在关闭括号后匹配注释,并删除文件中的前11个事件,无论在结束括号发生后是否有类似的匹配项。

假设文件的内容为;

$ cat input_file
#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

...

# Script continues

使用sed,匹配感兴趣的行,在与条件匹配的前11行上执行动作。

$ sed '/) #/{1,+10s/#.*//}' input_file
#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch )
kingdom=( "Island" )
zipcode=( 90210 )
datatype=( 0-9- )
favoritepie=( "Cherry" )
aoptions=( "Home address" "Work address" "Mobile" )
boptions=(  )
aopttypes=( string string phonenum )
bopttypes=(  )
sourced=(  )
subscripts=( installusr )

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

...

# Script continues

If I understand this correctly, you want to match comments after a closing bracket ) and delete the first 11 occurances in the file, regardless of if similar matches after a closing bracket occur.

Assume the contents of your file are;

$ cat input_file
#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

...

# Script continues

Using sed, match the lines of interest then, carry out the action on the first 11 lines that matched the condition.

$ sed '/) #/{1,+10s/#.*//}' input_file
#!/bin/bash

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch )
kingdom=( "Island" )
zipcode=( 90210 )
datatype=( 0-9- )
favoritepie=( "Cherry" )
aoptions=( "Home address" "Work address" "Mobile" )
boptions=(  )
aopttypes=( string string phonenum )
bopttypes=(  )
sourced=(  )
subscripts=( installusr )

# Title and other notes information
## I may have other lines here, or not

ruler=( monarch ) # Type of leader
kingdom=( "Island" ) # Type of territory
zipcode=( 90210 ) # Standard, 3-12 digits, hyphens allowed
datatype=( 0-9- ) # Datatype
favoritepie=( "Cherry" ) # A happy memory
aoptions=( "Home address" "Work address" "Mobile" ) # List custom options
boptions=(  ) # List secondary options
aopttypes=( string string phonenum ) # Corresponding datatypes for options
bopttypes=(  ) # Corresponding datatypes for secondary options
sourced=(  ) # Sourced text in this script, such as settings
subscripts=( installusr ) # Valid BASH scripts that this script may call

...

# Script continues
寒尘 2025-02-14 10:33:57

如果我正确理解您的要求,则以下将有效:

#!/bin/bash

for file in *; do
    temp=$(mktemp tmp.XXXXXX)
    awk '
        /\)[[:space:]]*#/ && c++ < 11 {sub(/[[:space:]]*#.*/, "")}
        1
    ' "$file" > "$temp"
    mv -f -- "$file" "$file".O          # backup file
    mv -f -- "$temp" "$file"
done

如果GNU AWK可用,则-i Inplace选项将有效地覆盖文件而不是创建temp文件:

#!/bin/bash

for file in *; do
    gawk -i inplace -v inplace::suffix=.O '
        /\)[[:space:]]*#/ && c++ < 11 {sub(/[[:space:]]*#.*/, "")}
        1
    ' "$file"
done

或简单:

#!/bin/bash

gawk -i inplace -v inplace::suffix=.O '
    /\)[[:space:]]*#/ && c++ < 11 {sub(/[[:space:]]*#.*/, "")}
    1
' *

If I'm understanding your requirements correctly, following will work:

#!/bin/bash

for file in *; do
    temp=$(mktemp tmp.XXXXXX)
    awk '
        /\)[[:space:]]*#/ && c++ < 11 {sub(/[[:space:]]*#.*/, "")}
        1
    ' "$file" > "$temp"
    mv -f -- "$file" "$file".O          # backup file
    mv -f -- "$temp" "$file"
done

If GNU awk is available, -i inplace option will work to overwrite the file instead of creating a temp file:

#!/bin/bash

for file in *; do
    gawk -i inplace -v inplace::suffix=.O '
        /\)[[:space:]]*#/ && c++ < 11 {sub(/[[:space:]]*#.*/, "")}
        1
    ' "$file"
done

Or simply:

#!/bin/bash

gawk -i inplace -v inplace::suffix=.O '
    /\)[[:space:]]*#/ && c++ < 11 {sub(/[[:space:]]*#.*/, "")}
    1
' *
假装爱人 2025-02-14 10:33:57

这可能对您有用(GNU SED):

sed -E 'x;/x{11}/{x;b};x;/\) #.*/{s//)/;x;s/^/x/;x}' file

本质上,请在持有空间中保持反击,如果已删除了11条所需类型的评论,则无需进一步处理线路。

检查保留空间柜台,如果是11,则保释。

否则,如果该行匹配所需的条件,请删除注释并在保留空间中增加计数器。

在所有其他情况下,没有进行处理。

This might work for you (GNU sed):

sed -E 'x;/x{11}/{x;b};x;/\) #.*/{s//)/;x;s/^/x/;x}' file

In essence, keep a counter in the hold space and if 11 comments of the required type have been removed, no further processing of a line is necessary.

Check the hold space counter and if it is 11, bail out.

Otherwise, if the line matches the required criteria, remove the comment and increment the counter in the hold space.

In all other cases, no processing is carried out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文