Replace a sting in shell with special character #shellscripting - shell

Please help me to replace a sting with new updated stinging in multiple files.
Input:
DNEXUS_USERNAME="$(NEXUS_USER_NAME)" -DNEXUS_PASSWORD="$(NEXUS_PASSWORD)"
New string:
DNEXUS_USERNAME_NEW='$(NEW_NEXUS_TKN_NAME)' -DNEXUS_PASSWORD_NEW='$(NEW_NEXUS_TKN)
Issue: when I'm trying to replace the sting by sed command it's being consider as a special character i.e. ($) . please suggest me the correct way to do this task

There doesn't seem to be a need to reference the $ in the file at all:
sed -i 's/NEXUS_USER_NAME/NEW_NEXUS_TKN_NAME/g;s/NEXUS_PASSWORD/NEW_NEXUS_TKN/g' file

Related

How to replace a string after a particular string in some line in txt file using sed in shell scripting?

I have a text file say file.txt containing following data:
APP_ARTIFACT=xxxx
APP_DOMAIN=xxx
APP_NAME=xxx
APP_OWNER_EMAIL=xxx
APP_PATH=xxx
IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:2.4
NAMESPACE=xxx
PAAS=xxxnce1
Now, I want to write a script in .sh which can replace the version after 'IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:' to a different version.
It means i want to replace 2.4 (or it may contain some other string as well) with some other string., while keeping the rest of the file as it is.
How can I do this using some inbuilt linux tool like 'sed'?
after replacing the string containg version with 'mystring;, the expected output after substitution should be :
APP_ARTIFACT=xxxx
APP_DOMAIN=xxx
APP_NAME=xxx
APP_OWNER_EMAIL=xxx
APP_PATH=xxx
IMAGE=dockerhub.ccc.xx.net:5005/aaa/xxx-rhel7:mystring
NAMESPACE=xxx
PAAS=xxxnce1
Assuming you want the new version to be 555, you could do:
sed -re 's/(^IMAGE.*):(.*)/\1:555/'
The above matches the line starting with IMAGE and looks at everything after it. It makes sure you have a colon (:) and uses the first match (\1) and appends a colon 555 (:555) for your new version.
You could use a variable as well:
myver="555"
sed -re "s/(^IMAGE.*):(.*)/\1:$myver/"

Delete a string in a file using bash script

We have a file which has unformatted xml content in a single line
<sample:text>Report</sample:text><sample:user name="11111111" guid="163g673"/><sample:user name="22222222" guid="aknen1763y82bjkj18"/><sample:user name="33333333" guid="q3k4nn5k2nk53n6"/><sample:user name="44444444" guid="34bkj3b5kjbkq"/><sample:user name="55555555" guid="k4n5k34nlk6n711kjnk5253"/><sample:user name="66666666" guid="1n4k14nknl1n4lb1"/>
If we find a particular string suppose "22222222", i want to remove the entire string that surrounds the matched string. In our case the entire portion around 22222222 i.e., <sample:user name="22222222" guid="aknen1763y82bjkj18"/> should be removed and the file has to be saved.
How can we do it? Please help
You can do it using sed utility by invoking it like this:
sed -i file -e 's/<[^<]*"22222222"[^>]*>//'

Replacing String Between two special charters in shell script

I am trying to replace a string in my shell script from localhost:7101/RPAS to sound.mojo.com:5555/rpas in my HTML file.
${myDeployment} =<\/td><\td width="0">http://localhost:7101/RPAS<\/td>
My output should be:
${myDeployment} =<\/td><\td width="0">http://sound.mojo.com:5555/rpas<\/td>
The strings that are fixed are ${myDeployment} and </td> and localhost:7101 is not a fixed string.
http://occurs multiple times in document and the above mentioned string occurs at the last , but not at the end of the document.
Is there a way to extend this command to only change the string of the last occurring pattern in the file.
sed -e 's#>http://[^/]*/#>http://sound.mojo.com:5555\/rpas/#' tst.html
Please let me know if there is a way to do it.
You can use this sed command:
sed 's#>http://[^/]*/#>http://sound.mojo.com:5555#' file
${myDeployment} =<\/td><\td width="0">http://sound.mojo.com:5555RPAS<\/td>

shell script - edit lines in shadow file (':' delimiter)

In the following shadow-type file:
user1:*:11111:0:88888:7:::
user2:*:11111:0:88888:7:::
user3:!!:11111:0:88888:7:::
user4:!!:11111:0:88888:7:::
user5:!!:11111:0:88888:7:::
user6:!!:11111:0:88888:7:::
I need find user 4, for example, and replace the '!!' with 'something_else'. Does anyone know how I could do this?
EDIT: Sorry that I wasn't clear - the '!!' could be anything.
You were near with your sed command. Better to use the substitution command only in the line searched, and instead of trying to match a literal use [^...] to match while a character not found, in this case the colon. Try:
sed '/^user4:/ s/:[^:]*/:something_else/' infile
It yields:
user1:*:11111:0:88888:7:::
user2:*:11111:0:88888:7:::
user3:!!:11111:0:88888:7:::
user4:something_else:11111:0:88888:7:::
user5:!!:11111:0:88888:7:::
user6:!!:11111:0:88888:7:::
try this:
sed '/^user4:/s/!!/something else/' file

How can I replace a word at a specific line in a file in unix

I've researched other questions on here, but haven't really found one that works for me. I'm trying to select a specific line from a file and replace a string on that line with another string. So I have a file named my_course. I'm trying to modify a line in my_course that starts with "123". on that line I want to replace the string "0," with "1,". Help?
One possibility would be to use sed:
sed '/^123/ s/0/1/' my_course
In the first /../ part you just have to specify the pattern you are looking for ^123 for a line starting with 123.
In the s/from/to/ part you have specify the substitution to be performed.
Note that by default after substitution the file will be written to stdout. You might want to:
redirect the output using ... > my_new_course
perform the substitution "in place" using the -e switch to sed
If you are using the destructive in place variant you might want to use -iEXTENSION in addition to keep a copy with the given EXTENSION of the original version in case something goes wrong.
EDIT:
To match the desired lined with a prefix stored in a variable you have to enclose the sed script with double quotes " as using single qoutes ' will prevent variable expansion:
sed "/^$input/ s/0/1/" my_course
Have you tried this:
sed -e '[line]s/old_string/new_string/' my_course
PS: the [ ] shouldn't be used, is there just to make it clear that you should put the number right before the "s".
Cheers!
In fact, the -e in this case is not necessary, I can write just
sed '<line number>s/<old string>/<new string>/' my_course
This is what worked for me on Fedora 36, GNU bash, version 5.2.15(1)-release (x86_64-redhat-linux-gnu):
sed -i '1129s/additional/extra/' en-US/Design.xml
I know you said you couldn't use line numbers; I don't know how to address that part, but this replaced "additional" with "extra" on line 1129 of that file.

Resources