取消注释范围内的行

Uncomment rows in range with condition

如何取消注释从 29 行到 39 行以及是否存在特定文本?

文件内容master.cf:

#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master" or
# on-line: http://www.postfix.org/master.5.html).
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (no)    (never) (100)
# ==========================================================================
smtp      inet  n       -       y       -       -       smtpd
#smtp      inet  n       -       y       -       1       postscreen
#smtpd     pass  -       -       y       -       -       smtpd
#dnsblog   unix  -       -       y       -       0       dnsblog
#tlsproxy  unix  -       -       y       -       0       tlsproxy
#submission inet n       -       y       -       -       smtpd
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_tls_auth_only=yes
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=
#  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#smtps     inet  n       -       y       -       -       smtpd
#  -o syslog_name=postfix/smtps
#  -o smtpd_tls_wrappermode=yes
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=
#  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#628       inet  n       -       y       -       -       qmqpd
pickup    unix  n       -       y       60      1       pickup
cleanup   unix  n       -       y       -       0       cleanup

尝试了命令:

sed -e '29,/smtpd_client_restrictions/s/^#\ //' master.cf

但它会解锁 5 下一行从 29 到 33

请具体说明如何操作,smtpd_client_restrictions可以用其他文字代替。

谢谢!

编辑: 看到 OP 的评论后,OP 可能需要取消评论第 29 到 39 行之间的匹配行在这种情况下的数字尝试:

awk 'FNR>=29 && FNR<=39{if([=10=]~/smtpd_client_restrictions/){sub(/^#/,"")}} 1' Input_file


能否请您尝试以下。如果找到匹配的字符串,这将删除从第 29 行到第 39 行的所有行的注释。

awk '
FNR>=29 && FNR<=39{
  if([=11=]~/smtpd_client_restrictions/){
    found=1
  }
  dup_val=(dup_val?dup_val ORS:"")[=11=]
  sub(/^#/,"")
  val=(val?val ORS:"")[=11=]
  if(FNR==39 && found){
    print val
    val=""
  }
  else{
    print dup_val
    dup_val=""
  }
  next
}
1
'  Input_file