取消注释范围内带有条件的行



如何取消注释从 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($0~/smtpd_client_restrictions/){sub(/^#/,"")}} 1' Input_file


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

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

最新更新