How to replace # in particular line?

Hi All,

I need your help to make the changes through sed , put # in starting of the line in /etc/rsyslog.conf file
please help me

$ActionExecOnlyWhenPreviousIsSuspended on
&@@10.10.0.10.
& /var/log/localbuffer
$ActionExecOnlyWhenPreviousIsSuspended off

Looking Result ....

#$ActionExecOnlyWhenPreviousIsSuspended on
#&@@10.10.0.10.
#& /var/log/localbuffer
#$ActionExecOnlyWhenPreviousIsSuspended off

Any attempts / ideas / thoughts from your side?

Yes I tried, but below is the result, which is not exact result.

  1. $ActionExecOnlyWhenPreviousIsSuspended on
    command-
sed     's/^$ActionExecOnlyWhenPreviousIsSuspended/#$ActionExecOnlyWhenPreviousIsSuspended/' /etc/rsyslog.conf

Result- #$ActionExecOnlyWhenPreviousIsSuspended on - its done, and fine

  1. &@@10.10.10.10
    Command
sed 's/^&@@10.10.10.10/#&@@10.10.10.10/' /etc/rsyslog.conf

Result- #&@@10.10.10.10@@10.10.10.10 - its replacing "#", but its replacing wrong

  1. & /var/log/localbuffer
    Command- sed 's/^& /var/log/localbuffer/#& /var/log/localbuffer/' /etc/rsyslog.conf
    Result- sed: -e expression #1, char 11: unknown option to `s'

  2. $ActionExecOnlyWhenPreviousIsSuspended off
    Command- sed 's/^$ActionExecOnlyWhenPreviousIsSuspended/#$ActionExecOnlyWhenPreviousIsSuspended/' /etc/rsyslog.conf
    Result- #$ActionExecOnlyWhenPreviousIsSuspended off - fine its good

Only problem in 2 & 3 .
so, looking help on this only.

Try

sed 's/^/#/' file

Thanks for reply,but its replacing wrong.

 sed 's/^&@@10.10.10.10/#&@@10.10.10.10/' /etc/rsyslog.conf
#&@@10.10.10.10@@10.10.10.10- its replacing wrong

No, it is not.

That is not what I proposed. Please specify EXACTLY what you need.

Actually the actual sentence is ..

&@@10.10.10.10

I am looking to change -

#&@@10.10.10.10 

just add # before the line.

A decent specification will help everybody to save time and effort. Try

sed '/&@@10.10.10.10/ s/^/#/' file
1 Like

Add #

sed '/^&@@10.10.10.10/ s/^/#/' /etc/rsyslog.conf

Remove #

sed '/^#&@@10.10.10.10/ s/^#//' /etc/rsyslog.conf

The & is special in sed in the replacement string. This is avoided now.
But:
The $ is special in a RE (the search string) if it's the last character.
The . is special in a RE: it means any character (not only a . character).

Better use the shell:

while IFS= read -r line
do
  case $line in
  # add a # character
  ( '&@@10.10.10.10'* ) line=#${line};;
  # remove a # character
  # ( '#&@@10.10.10.10'* ) line=${line#\#};;
  esac
  printf "%s\n" "$line"
done < /etc/rsyslog.conf

Within a 'string' the remaining problem character is ' . The quick workaround is '\'' .

1 Like

Welldone sir, thank you so much :-).
you are great.