Find and replace string with "/" recursively in a directories in RHEL 7.4 - linux

I have to find and replace all the occurrence of a string in all files /eOffice/eofficev6 to /eOffice/SAPS/eofficev6 recursively in a directory in RHEL 7.4.
Problem is that I using sed -i but my string also containing / slash.
How to replace all string having /?

You have two ways to achieve what you want.
One: Escape slashes (E.G. sed -i "s/\/eOffice\/eofficev6/\/eOffice\/SAPS\/eofficev6/" file).
Two: change the delimiter (E.G. sed -i "s|/eOffice/eofficev6|/eOffice/SAPS/eofficev6|" file).

You could do
sed -i 's/\(\/eOffice\)\(\/eofficev6\)/\1\/SAPS\2/' input_file_name
The "/eOffice" and "eofficev6" parts are grouped and the "SAPS" is inserted in between.
For example, if the input is:
/eOffice/eofficev6
the output will be
/eOffice/SAPS/eofficev6
Forward slashes are escaped with \s.
Or without grouping just,
sed -i 's/\/eOffice\/eOfficev6/\/eOffice\/SAPS\/eOfficeb6/' input_file

Related

How to copy string before comma in file and paste it later in file after slash

How can I use sed
to copy before comma and paste after slash in a file?
Example:
text1,Information /
text2,Information /
text3,Information /
and I want
text1,Information /text1
text2,Information /text2
text3,Information /text3
This works with your example:
sed -r 's#(.*),(.*)/#\1,\2/\1#' <file_path>
The idea is that groups (between parentheses) are referenced in the substitution part.
This might work for you (GNU sed):
sed -E 's/([^,]*).*/&\1/' file
Capture everything before the , and append to the end of the line.

How to find and replace a file path (along with special characters) using SED command

How to find and replace a file path like a/b/c/d into x/y/z using sed command?
I tried sed -i -e 's/a/\b/\c/\d/x/\y/\z' file.txt but it did not work.
Just use another separator for s command. You can use any character. Just / is the most popular one. I like # or #. Then you have to escape that character.
sed 's#a/b/c/d#x/y/z#'
but it did not work
To escape / use \/ not /\. The \ needs to be in front of /. You could:
sed 's/a\/b\/c\/d/x\/y\/z/'

How can I use sed to perform this replacement?

I need to replace a line in file with something
#Banner none
to
Banner /etc/motd
I tried:
sed -i "s/^#Banner none/Banner /etc/motd/" /root/testfile.sh
which is not working. Do you know how I can do what I want?
Since you have slashes in the replacement, you should use other separators than slashes, e.g. commas:
sed -i "s,^#Banner none,Banner /etc/motd/," /root/testfile.sh
Otherwise, you'd have to escape the slashes in the path:
sed -i "s/^#Banner none/Banner \/etc\/motd/"
You'll need to escape your slash or use a different separator than "/".
Could use
sed -i "s/^#Banner none/Banner \/etc\/motd/" /root/testfile.sh
or
sed -i "s,^#Banner none,Banner /etc/motd," /root/testfile.sh

Add many quotation in sed instruction

I have to update line with value containg many quotation characters
sed -i 's/.*sonar.links.issue.*/property "sonar.links.issue", "http://jra.url"'
I get error there. What should i do to do it correctly?
Your replacement string contains slashes "http://", so you shouldn't use / as the delimiter for this sed command, use # as shellter proposes.
To complete its answer, I would add the "g" flag, at the end of the sed expression, to ensure the subsitution is made on the whole line, and not only on the first match with ".sonar.links.issue.".
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#g' file
Try
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#' file
IHTH

difficult string to replace in script using SED

I know, many people have had questions on how to use sed to replace a string, but I have a difficult one here.
I have a file I need to replace a string of text that prompts the user to enter content. I want to automate this so the user does not interact. By replacing this string with a static file path. but the text is a bash script and has ' and " within the string I want to replace. It does not work. Either because I have syntax errors in my formatting, or it simply is not possible to do this action with sed. Please advice!
Here is what I am attempting to do:
I want to replace this long string
read -e -p 'Enter path for Boot Partition : ' BOOTUSERFILEPATH
with a string that looks like this:
BOOTUSERFILEPATH=../board-support/prebuilt-images
My attempt:
sed -i "/read -e -p 'Enter path for Boot Partition : ' BOOTUSERFILEPATH/BOOTUSERFILEPATH="../board-support/prebuilt-images"" file_to_search.sh
Update: I fixed the syntax error, but file still is not updated with the new path information... :(
found the problem. the search was not finding the strings because of an extra space in my search command. It works now!
There are forward slashes in your string so one needs to use a different delmiter. Here I use '|' as the delimiter.
sed "s|read -e -p \'Enter path for Boot Partition : \' BOOTUSERFILEPATH|BOOTUSERFILEPATH=../board-support/prebuilt-images|g" oldfile > newfile
You may note that the -i option to sed which allows files to be edited in place is not a POSIX supported option.
However if you wish to use it:
sed -i "s|read -e -p \'Enter path for Boot Partition : \' BOOTUSERFILEPATH|BOOTUSERFILEPATH=../board-support/prebuilt-images|g" oldfile
You may find it easier to use a pattern with sed which matches part of this string and then replaces its entirety:
sed 's|read -e -p .* BOOTUSERFILEPATH|BOOTUSERFILEPATH=../board-support/prebuilt-images|g' filename > newfilename
From the POSIX specification page for sed:
s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the pattern space. Any character other than <backslash> or <newline> can be used instead of a slash to delimit the BRE and the replacement.
You need to use an actual substitute command, and you need to avoid the slashes in the replacement text from confusing sed. Personally, I'd probably use:
sed -i.bak "s%^read .* BOOTUSERFILEPATH$%BOOTUSERFILEPATH=../board-support/prebuilt-images%" file_to_search.sh
or even more likely:
BOOTUSERFILEPATH="../board-support/prebuild-images"
sed -i.bak "s%^read .* BOOTUSERFILEPATH$%BOOTUSERFILEPATH=$BOOTUSERFILEPATH%" file_to_search.sh
The s%%% uses % instead of / to delimit the parts of the command. I cheated on the match pattern, working on the assumption that you don't have many similar lines in the file.

Resources