Using sed command how can I replace Linux path to Windows path - linux

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.

Related

regex replace in linux for multiple files

I have 200k files in a single folder in linux server where i need to transform this files using regex
here is the regex to find in text file
([^\s]+?.*)=((.*(?=,$))+|.*).*
now I need to replace it with below substitution value
"$1":"$2",
the above regex is working fine when i used them in python. the server which i am working does not
support python, so i need to use bash commands. i have tried below bash command but it is not working
command:
sed -r 's/([^\s]+?.*)=((.*(?=,$))+|.*).*/"$1":"$2",/g' *20200502*
the above bash command is not working
Fixed your regex:
sed -E 's/([^[:space:]]+?.*)=((.*(=?,$))+|.*).*/"\1":"\2",/g' *20200502*
Replaced inappropriate [^\s] by its POSIX ERE syntax equivalent [^[:space:]].
Fixed the misplaced optional marker ?=,$ with =?,$ instead.
Fixed the invalid capture group reference syntax "$1":"$2" with "\1":"\2".
Kinda difficult to test but just analyzing your approach this might work:
sed -i -E "s/([^\s]+?.*)=((.*(?=,$))+|.*).*/\1$1\2$2/" *20200502*
\1 and \2 in the second part are references to the groups captured in the first part
-E for extended regular expressions (+ and grouping)

How to replace a line (directory path) with a variable containing another line (new directory path) in a Linux file?

I have a file containing contents like:
aaaaaaaaaa
export ORACLE_HOME=/home/oracle/test/prod/db_1
bbbbbbbbbb
Now I want to replace the line of
export ORACLE_HOME=/home/oracle/test/prod/db_1
with something like below:
export ORACLE_HOME=/u01/app/oracle/product/11204/dbhome_1
I tried numerous ways found through Google like using "sed" or "awk" but neither one worked. I tried:
export D1="export ORACLE_HOME=/u01/app/oracle/product/11204/dbhome_1"
sed -i -e "s+export ORACLE_HOME=/home/oracle/test/prod/db_1+$D1+" file.lst
Note: as one Google search says, since the strings contains "/", so a different delimiter like "+" needs to be used.
Can you share with me the right command to do so?
sed -r 's~(ORACLE_HOME=).*~\1new/path/here~'
here ~ is used as the delimiter. If the new path is a bash variable, you can escape the quotes
sed -r 's~(ORACLE_HOME=).*~\1'$D1'~'
If you are still having trouble with the substitution, you can use this variation which will explicitly find the line containing export ORACLE_HOME before attempting the path substitution. The expression makes use of alternate delimiters for the substitution '|'.
To edit in place (Linux), you can use the '-i' option (and add '-i.bak' to make a backup of the original). If you are using a mac or other OS without the '-i' option available, use redirection to create a new file which you can copy over the current file.
sed -i '/^export\sORACLE_HOME/s|=.*$|=/u01/app/oracle/product/11204/dbhome_1|' \
yourfilename
or without '-i':
sed '/^export\sORACLE_HOME/s|=.*$|=/u01/app/oracle/product/11204/dbhome_1|' \
yourfilename > newfilename
cp yourfilename yourfilename.sav && mv -f newfilename yourfilename
(note: the '\' at the end of the sed command line is a simple line-continuation)
Using a Variable for Replacement
From your comment, if you want to use a variable instead of a hardcoded path for the replacement string, then simply replace the single-quotes with double-quotes and include the variable as the replacement string. For example:
npath=/u01/app/oracle/product/11204/dbhome_1
sed -i "/^export\sORACLE_HOME/s|=.*$|=$npath|" dat/opath.txt

Find and replace text on multiple files

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.

sed code to match http://www.domain.com/ and replace with just a / in all files in a directory

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.

Search and replace text in all files of a linux directory

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>

Resources