Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I escape $1 and && in a SED command

Status
Not open for further replies.

EdLentz

Technical User
Mar 20, 2002
85
0
0
US
I have a sed command that gets most of what I want done with the exception of the '$1' && I haven't figured out how to make it enter the line like I want.
Here is my command:
sed -i 's/# UserParameter=/UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet $1 && echo 1 || echo 0/g' /etc/zabbix/zabbix_agent2.conf

Here is the entry that results:

UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet \ # UserParameter=# UserParameter= echo 1 || echo 0

What I need is:
UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet $1 && echo 1 || echo 0

I am running this from the cli. I know why it is doing this but I haven't figured out how to make it work the way I need it to. Can someone show me how to I believe the phrase is to escape the '$1' and the &&

Thanks alot

 
Hi

EdLentz said:
Can someone show me how to I believe the phrase is to escape the '$1' and the &&
[ul]
[li]$1 needs no escaping as it has no special meaning. ( The placeholder for the 1[sup]st[/sup] captured group is [tt]\1[/tt]. )[/li]
[li][tt]&[/tt] can be escaped by prefixing it with a backslash : [tt]\&[/tt].[/li]
[/ul]
So should be :
Code:
sed -i 's/# UserParameter=/UserParameter=systemd.unit.is-active[*],systemctl is-active --quiet $1 [COLOR=red yellow]\[/color]&[COLOR=red yellow]\[/color]& echo 1 || echo 0/g' /etc/zabbix/zabbix_agent2.conf

Note that this may depend on the [tt]sed[/tt] implementation you use.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top