bash - 删除 BASH 内联注释,但仅在数组和前 11 次出现之后

标签 bash sed gnu

我有什么

我在脚本中过度记录了我的元语句。 有很多。它们都以这样的方式开始:

#!/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 次。

来自this answer我得到了一个与第一个匹配的模式,给了我这个......

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

...但这仅适用于第一次出现。

我可以将它放入循环中:

#!/bin/bash

counter=1

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

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

done

这“合适”吗?

此循环假设所有文件将均匀匹配,对所有文件运行空白。如果可能的话,我希望循环针对每个文件运行,而不是针对基于计数器的所有文件。但是,我不知道该怎么做。

这是最好的方法吗?或者,是否有使用其他 Linux 工具的更“正确”、万无一失的方法?

最佳答案

如果我理解正确的话,您希望匹配右括号后的注释 ) 并删除文件中的前 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

关于bash - 删除 BASH 内联注释,但仅在数组和前 11 次出现之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72624785/

相关文章:

linux - Shell:如何将多个文件移动到一个目录并用不同的名称压缩该目录?

emacs - GNU emacs 相当于 func-menu

c++ - -Wconversion-null 不考虑所有构造函数

linux - 如何确定第三方可执行文件的最低支持版本

bash - vim 终端 OS X Mountain Lion 中的光标颜色

bash shell 重做变量用下划线替换点

sed:删除匹配行和之后的所有内容

regex - sed:将部分输出复制到表中

regex - 当第二个模式不匹配时,以逗号分隔的模式地址执行 sed 命令

bash - 在 Bash 中删除空行并修剪周围的空格