I have a website directory where I need to change all hardcoded links from one domain to another. Looking for a single (grep? sed?) bash command that will allow me to change all occurrences of text in all files in the directory?
The following will do it:
sed -i 's/old_link/new_link/g' file...
Don't forget to escape any slashes, dots, and any other regex special chars in the link addresses with a backslash.
Also, try:
perl -p -i -e <regex> <folder>
Related
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
I want to change the Linux path to a Windows Path, using "sed" command, for example:
Linux path: /opt/test/dash/apps/tomcat to Windows Path:
c:\\test\\dash\\apps\\tomcat
I tried with:
sed -i 's|'/opt/test/dash/apps/tomcat'|'c:\\\\\\\test\\\\\\\dash\\\\\\\apps\\\\\\\tomcat'|g' /filename - But no luck!!
What I exactly want all /opt/ should replace by c:\\ and rest of the "/" should be replace by "\\".
NOTE: I am executing this command remotely using ssh2_exec, All "sed" commands are working except the above.
Thanks in advance!!
I would do it in two steps:
$>echo '/opt/test/dash/apps/tomcat' | sed 's#/opt#c:#g'|sed 's#/#\\\\#g'
c:\\test\\dash\\apps\\tomcat
First changing the /opt with c:, then change the / with \ that you have to escape
I'd use regular expressions and so:
sed -r 's#/(.*)/(.*)/(.*)/(.*)/(.*)#C:\\\\\2\\\\\3\\\\\4\\\\\5#'
Using -r to enable interpretation of regular expressions and # as the sed separator, split the path in 5 parts and then refer to them in the translated section with \1 \2 etc.
Let's say I want to rename all the files inside all the subfolders of a folder from foo.txt to bar.txt using zmv.
I've tried zmv '**/foo.txt' 'bar.txt' but this creates bar.txt in the root folder. How can I keep the files in their corresponding subfolder?
You need to reference the directory part in the target. You can do that by putting the wildcards in parentheses and using $1 to refer to the part matched by the parenthetical group. The ** wildcard is a little special and requires that the parentheses are around **/, no more, no less.
zmv '(**/)foo.txt' '${1}bar.txt'
You can use the -w flag to have each wildcard automatically made into a parenthetical group.
zmv -w '**/foo.txt' '${1}bar.txt'
Or you can use the -W flag and use wildcards in the replacement text — with this flag, the wildcards in the replacement text are turned into $1, $2, etc.
zmv -W '**/foo.txt' '**/bar.txt'
Alternatively, you can use $f to refer to the source path.
zmv '**/foo.txt' '$f:r.txt'
I am trying to replace a specific link which exists on many html pages with its https version. I have tried:
grep -rl "http://server.iad.liveperson.net/hc/88956865/" ./ | xargs sed -i "s/http:\/\/server.iad.liveperson.net\/hc\/88956865\//https:\/\/server.iad.liveperson.net\/hc\/88956865\//g"
When I do this, even as sudo, I am getting
sed: couldn't open temporary file ./customers/sedTR3AMu: Permission denied
customers is just the first directory in ./. So, it is hanging on the first file I reckon, but not sure what is wrong beyond that.
Any help is appreciated!
First thing you should try is to run the sed command as stand alone, for a file that you previously know that contains that string. I have the feeling that the sed command might be complaining about the / characters...
You should try changing the sed command to something like:
sed -i 's;http://server.iad.liveperson.net/hc/88956865/;https://server.iad.liveperson.net/hc/88956865/;g'
That is, using ; instead of / as the delimiter, so you don't have to escape the / every time using \.
Had to run the command logged in as root because sed -i creates temporary files in /tmp and needed write access.
Thanks:Used jim's syntax with the semicolons which worked fine. ooga, I did not have to escape the literal periods.
I've been searching for this answer for three hours now and I still can't get anything to work. When I run things like this:
sed -i 's/http\:\/\/www\.domain\.org\//\//g checkout_*.php
It drops me into another command line (sorry, I'm very new to sed).
I just want to cd to a dir, grep the dir to see if the string is there then run a replace so I can change my paths from absolute to relative.
You need to close your '. You can also make your command cleaner by using a different sed delimiter to / so that you don't have to escape all those forward slashes in your URL. For example, you can use !, as shown below:
sed -i 's!http://www\.domain\.org/!/!g' checkout_*.php
You just appear to be missing the closing '
sed -i 's/http\:\/\/www\.domain\.org\//\//g' checkout_*.php
Should do what you want ok. But I'd warn you against doing the -i switch without first doing a dry run.