Anton Teremshonok
Anton Teremshonok

Reputation: 3

Uncomment rows in range with condition

How do I uncomment lines starting from 29 lines to 39 and if a certain text is present?

File contents 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

Tried the commands:

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

But it will unlock 5 next lines from 29 to 33

Please specify how to do this, smtpd_client_restrictions can be replaced with other text.

Thank you!

Upvotes: 0

Views: 73

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133730

EDIT: After seeing OP's comment looks like OP may need to un-comment only matched line which comes between 29th to 39th line number in that case try:

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


Could you please try following. This will remove comments from all lines from 29th to 39th in case matched string found.

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

Upvotes: 1

Related Questions