# Sed command to replace any type of string in shell script in linux

**URL:** <https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833>\
**Category:** Forum\
**Tags:** Adabas-Natural, Adabas, Adabas-on-Mainframes, Adabas-on-Linux\
**Created:** [May 16, 2024, 11:12am UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833 "2024-05-16T11:12:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiran\_Kumar\_Kunduru](https://avatars.discourse-cdn.com/v4/letter/k/eb9ed0/32.png) [@Kiran\_Kumar\_Kunduru](https://techcommunity.softwareag.com/u/Kiran_Kumar_Kunduru)\
**Post date:** [May 16, 2024, 11:12am UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/1 "2024-05-16T11:12:42Z")

</div>

Hi Team,

I am trying to replace string like below with sed command, but I am not able to replace with new string.

EX: old\_String : watt.server.http.Content-Security-Policy=default-src \‘self’\ \*.abc.com _._.abe.com  
New\_Sting: watt.server.http.Content-Security-Policy=default-src ‘self’ \*.abe.com _._.abc.com

sed -i “s~$old\_String ~$New\_Sting~g” filename.txt

The above sed command is working for other string but not for the above sting.  
in old\_string \‘self’\ will vary each time, ex: \'self’\, \\\‘self’\\\. The \ can be any number of times it wary each time.

Any inputs would be very helpful.

---

<div class="post-metadata">

**Author:** ![Hilmar\_Wantje6](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@Hilmar\_Wantje6](https://techcommunity.softwareag.com/u/Hilmar_Wantje6)\
**Post date:** [May 16, 2024, 1:51pm UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/2 "2024-05-16T13:51:33Z")

</div>

Hi.  
i tried in vain as You did. but could help this construct?:

cat filename.txt # see the content  
in old\_string .self.\ will vary each time, ex: 'self., \.self.\. The \ can be a  
2  
in old\_string .self.\ will vary each time, ex: 'self., \.self.\ZZ

cat filename.txt | tr -d '' # tr is “translate character”  
tr: Warning: Rückschrägstrich () ohne Fluchtzeichen am Ende der Zeichenkette ist nicht portabel  
in old\_string .self. will vary each time, ex: 'self., .self… The can be a  
2  
in old\_string .self. will vary each time, ex: 'self., .self.ZZ  
despite of the warning i got rid of all slashes

regards  
Hilmar

---

<div class="post-metadata">

**Author:** ![Hilmar\_Wantje6](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@Hilmar\_Wantje6](https://techcommunity.softwareag.com/u/Hilmar_Wantje6)\
**Post date:** [May 16, 2024, 1:54pm UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/3 "2024-05-16T13:54:08Z")

</div>

oops. my original file content multiple slashes  
in old\_string .self.\ will vary each time, ex: 'self., \.self.\. The \ can be a  
2  
in old\_string .self.\ will vary each time, ex: 'self., \.self.\ZZ

---

<div class="post-metadata">

**Author:** ![Hilmar\_Wantje6](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@Hilmar\_Wantje6](https://techcommunity.softwareag.com/u/Hilmar_Wantje6)\
**Post date:** [May 16, 2024, 1:54pm UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/4 "2024-05-16T13:54:54Z")

</div>

sending the message deletes multiple slashes

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar\_Kunduru](https://avatars.discourse-cdn.com/v4/letter/k/eb9ed0/32.png) [@Kiran\_Kumar\_Kunduru](https://techcommunity.softwareag.com/u/Kiran_Kumar_Kunduru)\
**Post date:** [May 17, 2024, 7:09am UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/5 "2024-05-17T07:09:12Z")

</div>

HI Wantje,

One more approach tried is below grep the exact string of this

string=`grep "watt.server.http.Content-Security-Policy=" file_name`

EX: output string=watt.server.http.Content-Security-Policy=default-src \‘self’\ \*.abc.com _._ .abe.com

stLine=`grep -n "${string}" file_name`

The above command will give the exact match line number. Next I am deleting the line number like below

sed -i “${stLine}d” file\_name (This will delete the exact matched sting)

Now just add the new string to the file\_name.

echo “watt.server.http.Content-Security-Policy=default-src ‘self’ \*.abc.com _._ .abe.com” \>\> file\_name

I hope this also should work right?

Any inputs would be helpful.

---

<div class="post-metadata">

**Author:** ![Hilmar\_Wantje6](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@Hilmar\_Wantje6](https://techcommunity.softwareag.com/u/Hilmar_Wantje6)\
**Post date:** [May 17, 2024, 7:40am UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/6 "2024-05-17T07:40:50Z")

</div>

Hi

what is going wrong with tr command?

your idea is not clear to me: \<\<\<\<\<\<\<\<\<\<\<\<\<\< i added 2 comments below

---

<div class="post-metadata">

**Author:** ![Kiran\_Kumar\_Kunduru](https://avatars.discourse-cdn.com/v4/letter/k/eb9ed0/32.png) [@Kiran\_Kumar\_Kunduru](https://techcommunity.softwareag.com/u/Kiran_Kumar_Kunduru)\
**Post date:** [May 17, 2024, 7:53am UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/7 "2024-05-17T07:53:13Z")

</div>

I will also test your case and will let you know, the above one I tested so I updated here.

Once I test it, I will let you know

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [November 13, 2024, 7:54am UTC](https://techcommunity.softwareag.com/t/sed-command-to-replace-any-type-of-string-in-shell-script-in-linux/295833/8 "2024-11-13T07:54:02Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
