How to replace '/' with '/\' using sed in shell scripting - shell

I am trying to replace
prakash/annam/DevOps ---> prakash/\annam/\Devops
I am using this:
sed "s/'[//]''///\\/g"
Unfortunately, it is not giving the required output can anyone please help with this!!!

you can use a separator other than slash:
$ sed 's#/#\\/#g' <<< "a/b/c"
a\/b\/c
$ sed 's#/#/\\#g' <<< "a/b/c"
a/\b/\c

You can use sed with -i flag to place in place changes to the file
*nix
$ cat test
prakash/annam/DevOps
$ sed -i 's/\//\/\\/g' test
$ cat test
prakash/\annam/\DevOps
MacOS
$ cat test
prakash/annam/DevOps
$ sed -i '' 's/\//\/\\/g' test
$ cat test
prakash/\annam/\DevOps

Use
sed -E 's/\//\/\\/g'
e.g.
$ echo "prakash/annam/DevOps" | sed -E 's/\//\/\\/g'
prakash/\annam/\DevOps

Related

Im trying to change words in a Unix file using sed but I keep getting an error

I keep getting this error message and don't understand why. How do I fix it. I am using the bash shell on a Unix system.
$ sed -i 's/ he/ she/g' S13a4sed
sed: illegal option -- i
sed command parameter option i is not available in some of the unix environment, for example SunOS Just use like below it will work for you.
$ sed 's/ he/ she/g' S13a4sed
Just test it like below:-
echo " he is a girl" | sed 's/ he/ she/g'
Output
she is a girl
sed's -i option is a feature of GNU's sed. Unfortunately you can't use it on your system. But you can use perl as well:
$ cat S13a4sed
he is a girl
$ perl -pi -e 's/he/she/g' S13a4sed
$ cat S13a4sed
she is a girl
From here: sed -i + what the same option in SOLARIS

how to keep file format when using sed editor?

suppose a file that contains:
a=1234
b=5678
c=word
i am writing a bash script that will change a's value. i use following command:
echo `sed "s/\(a=\).*/\1 new/" file` >file
it gives:
a=new b=5678 c=word
i also tried following (using anchors) still getting same format:
echo `sed "s/\(a=\).*[\n]$/\1 new/" file` >file
echo `sed "s/\(a=\).*'\n'$/\1 new/" file` >file
where i want:
a=new
b=5678
c=word
how to do that?
Use the -i argument to edit the file in place:
sed -i 's/\(a=\).*/\1new/' file
You could a following sed too once.
sed -i '/^a=/s/=.*/=new/g' Input_file

Compress working directory

I want to know the best way to compress the current working directory so that only the last directory's full name is visible. Let me give an example:
$ echo $PWD
/Users/mac/workshop/project1/src
I want to be able to pipe it through a series of commands such that I should get
~/w/p/src
I can obtain the first part of getting the leading ~ by doing ${PWD/#$HOME/\~}
$ echo ${PWD/#$HOME/\~}
~/workshop/project1/src
What do I pipe the rest through so that I get
$ echo ${PWD/#$HOME/\~} | ...
~/w/p/src
echo ${PWD/#$HOME/\~} |
sed -e 's%/\([^/]\)[^/]\{1,\}/%/\1/%g' \
-e 's%/\([^/]\)[^/]\{1,\}/%/\1/%g'
The repeated regex is necessary because the pattern matches, for example, the /src/ in ~/src/perl/DBD/Informix/lib/DBD/Informix, and the matching resumes with the p of perl, which isn't a slash, so it doesn't match immediately. You can see what happens by replacing the g with gp.
Example:
$ echo $PWD
/Users/jleffler/src/perl/DBD-Informix/lib/DBD/Informix
$ echo ${PWD/#$HOME/\~} |
> sed -e 's%/\([^/]\)[^/]\{1,\}/%/\1/%g' -e 's%/\([^/]\)[^/]\{1,\}/%/\1/%g'
~/s/p/D/l/D/Informix
$
$ echo ${PWD/#$HOME/\~} |
> sed -e 's%/\([^/]\)[^/]\{1,\}/%/\1/%gp' -e 's%/\([^/]\)[^/]\{1,\}/%/\1/%gp'
~/s/perl/D/lib/D/Informix
~/s/p/D/l/D/Informix
~/s/p/D/l/D/Informix
$
Command :
sed "s/\/home\/[^\/]*/~/;s_/\(.\)[^/]*_/\1_g" <<<"/home/ssam/Documents/so"
Output :
~/D/s
Command :
sed "s/\/home\/[^\/]*/~/;s_/\(.\)[^/]*_/\1_g" <<<"/homey/singo/Documents/so"
Output :
/h/s/D/s

How to use variables in 'sed' command?

Tried with following code:
revision=(revision="5.0")
sed -i "s/revision="1.0"/${revision}/g" /File-path/composite.xml)
But In the file the contents of revision="1.0" is replaced by ${revision}
Need to quote: "
sed -i "s/revision=\"1.0\"/${revision}/g" /File-path/composite.xml
Some syntax errors:
-i switch tell sed to execute infile replacement.
(...) tell bash to store an array of variables.
leading ) seem wrong.
My purpose:
revision='revision="5.0"'
sed -e "s/revision=\"[0-9.]*\"/${revision}/g" /File-path/composite.xml
or if you want file /File-path/composite.xml to be modified:
sed -e "s/revision=\"[0-9.]*\"/${revision}/g" -i /File-path/composite.xml
or even:
sed -e "s/revision=\"[0-9.]*\"/${revision}/g" -i.bak /File-path/composite.xml

sed: file skripta.txt line 1: unknown option to `s'

I try to use:
sed -e 's/miza/stol/g' datoteka1.txt | sed -e '/klop/d' | sed -e '/^$/d' | sed -e 's/janez/Janez/g'
in a file named skripta.txt with "sed -f skripta.txt > datoteka2.txt" to save it in another file and I get this error mentioned in title.
If I run this code seperately it works just fine.
What is wrong here???
This is a shell script that uses sed, not a sed script.
Run it with bash skripta.txt > datoteka2.txt
So let me get this straight:
You have a file called skripta.txt that contains only this line:
sed -e 's/miza/stol/g' datoteka1.txt | sed -e '/klop/d' | sed -e '/^$/d' | sed -e 's/janez/Janez/g'
And you try to run it with
$ sed -f skripta.txt
Is that correct?
The error is not surprising then, because it expects just the sed commands, not a call to sed itself, inside the script file.
It interprets the initial s of the first sed as 'search and replace', but the following syntax ed doesn't match.
You can either change the skripta.txt into a shell script: (You could also change it's name into skripta.sh):
#!/usr/bin/env bash
sed -e 's/miza/stol/g' datoteka1.txt | sed -e '/klop/d' | sed -e '/^$/d' | sed -e 's/janez/Janez/g'
change it's mode to executable:
$ chmod u+x skripta.sh
Then you can just call it:
$ ./skripta.sh
Or you can turn it into a sed script by removing all the seds:
s/miza/stol/g
/klop/d
/^$/d
s/janez/Janez/g
Then you can run it with
$ sed -f skripta.txt < datoteka1.txt > datoteka2.txt

Resources