How to get a substring using sed being the pattern "/" [duplicate] - linux

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I have the following string:
/book/A00001/2018/01/15/Chamber_Music
And I want to get using the sed command:
/book/A00001/2018/01/15/
Thanks
Regards

Maybe are you looking for: sed "s/\(.*\)Chamber_Music/\1/g"

No need to use sed, you can use normal shell string handling:
filename='gash.txt'
new_filename="$filename.new"
while read line
do
line=${line%/*}
echo $line
done <"$filename" >"$new_filename"
#mv "$new_filename" "$filename" # Commented out to be optional
Given your input in your second question:
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15
/book/A00001/2018/01/15

You can change the regexp sed delimiter.... see the s command documentation. If you use, e.g.
sed 's:([a-zA-Z0-9/]*)[a-zA-Z]*$:\1:'
then, the / loses its special treatment and the : character assumes it. Of course, you can store the matching pattern in an environment variable, before to feed it to sed(1), and substitute all / into \/ to escape every /.
pattern=`echo "([a-zA-Z0-9/])[a-zA-Z]*\$" | sed 's/\//\\//'`
sed "s/${pattern}/\\1/"
but this is more complex.

Related

perl replace with bash variable [duplicate]

This question already has answers here:
How can I process options using Perl in -n or -p mode?
(2 answers)
Closed 12 months ago.
could someone please explain, why perl doesn't replace the regexp:
root#machine08:~# VERSIONK8S='${VERSION_KUBERNETES:-1.23.3-00}'
# with sed as i want it
root#machine08:~# sed -e "s/^VERSIONK8S=.*/VERSIONK8S=${VERSIONK8S}/g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
VERSIONK8S=${VERSION_KUBERNETES:-1.23.3-00}
# with perl not exactly as i want it
root#machine08:~# perl -pe "s/^VERSIONK8S=.*/VERSIONK8S=${VERSIONK8S}/g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
VERSIONK8S=-e
From Perldoc:
If the delimiter chosen is a single quote, no variable interpolation is done on either the PATTERN or the REPLACEMENT. Otherwise, if the PATTERN contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time.
Perl tries to expand the substitution string ${VERSION_KUBERNETES:-1.23.3-00} as a perl variable starting with $ and fails. To avoid the expansion, please try:
perl -pe "s'^VERSIONK8S=.*'VERSIONK8S=${VERSIONK8S}'g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
by using a single quote as a delimiter instead of a slash such as s'pattern'replacement'.

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

how to use sed command properly to replace values containing / delimiter [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 5 years ago.
File: abc.properties
tomcat.home=/opt/tomcat
Set to /usr/local/tomcat. Following cmd is working.
sed -i "/tomcat.home=/ s/=.*/="usr\\/local\\/tomcat"/" abc.properties
Set to $WORKSPACE/tomcat. Following cmd is NOT working since value of the $WORKSPACE is having / delimeters.
sed -i "/tomcat.home=/ s/=.*/="$WORKSPACE\\/tomcat"/" abc.properties
Anyone has an idea how to success above cmd.
Thank you and appreciate your support...
Sed lets you use any character you want as the delimiter. Whatever follows the s is used as the separator:
sed -Ee 's/foo/bar/'
sed -Ee 's|foo|bar|'
sed -Ee 's#foo#bar#'
^- All of those are equivalent.
The other option is to escape all your / as \/, but that gets nightmarish fast. Prefer to just pick a separator character that doesn't collide with characters you're trying to use for something else.

Difference sed commands sed 's///' and sed "s###" [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I want to ask 2 questions about sed.
For example, when I try to put a string to sed which contains special character like (.\n*.+) and sed cannot run properly.
set a = ".\n*.\+"
set input = ".\n*.\+adsdfasdf"
Then execute:
echo "$input" | sed 's/'$a'/hi/g' # It will give: sed: No match
but
echo "$input" | sed "s#${a}#hi#g" # It will run but not true
My questions are:
What is the difference between these commands: sed 's///' and sed
"s###"
How to treat input just as it is purely string?
1. What is the difference between these commands: sed 's///' and sed "s###"
--- In your case, / or # is a separator of the crucial "sections":
The s command (as in substitute) is probably the most important in sed
and has a lot of different options. The syntax of the s command is
‘s/regexp/replacement/flags’.
...
The / characters may be uniformly replaced by any other single character within any given s command.
2. How to treat input just as it is purely string?
--- To expand/interpolate a variables within sed command, those variables OR the whole expression should be enclosed with double quotes:
set a = ".\n*.\+"
echo "$input" | sed "s/$a/hi/g"
Well, you have a \n in the input string, which you substitute without quote delimiters in 's/'$a'/hi/g' and is parsed as a space, so you pass actually two parameters to sed(1) and you substitute as only one parameter (with the \n included as one character) in only one parameter in "s#$a#hi#g" (in which double quotes include the variable substitution). There is actually no difference in the character used as delimiter in sed(1), but you have called it differently in the two calls.
Finally, I found out that the separator MUST BE different with any characters in the string!
Or it will cause error!
Thus, now I will change the separator to #. It's like comment character.

Apend the lines in a config file using shell script [duplicate]

This question already has answers here:
How to escape single quote in sed?
(9 answers)
Closed 6 years ago.
I have a config.js file which contents below strings
.constant('Digin_Engine_API', 'http://local.net:1929/')
I want to read this file and replace what ever the things which are there after the .constant('Digin_Engine_API'I tried using sedbut ddnt worked. This is what I used for sed
sed -i 's/^.constant('Digin_Engine_API', .*/http://cloud.lk:8080/' /var/config.js
As a summary my final out put (config.js) file needs to consists below.
Before
.constant('Digin_Engine_API', 'http://local.net:1929/')
After
.constant('Digin_Engine_API', 'http://cloud.lk:8080/')
You need to use double quotes around sed command since single quote is part of pattern
You should use an alternate delimiter since / is used in replacement
You need to capture the first part and use it in replacement
You need to quote the replacement and also add closing )
Sed command:
sed -i.bak "s~\(\.constant('Digin_Engine_API', \).*~\1'http://cloud.lk:8080')~" /var/config.js
cat /var/config.js
.constant('Digin_Engine_API', 'http://cloud.lk:8080')
Here you are:
sed -i -r "s_(\.constant\('Digin_Engine_API').*_\1, <new content>)_" file
Remarks
you cannot use ' in sed command if command is surrounded by '' also,
you must escape all ( and ) that are part or string, and not the sed grouping command,
you must escape . character, because it is also sed replacement for every char,
you must use another sed s separator instead of / if you need to use / in that command, but you can also escape / by \/.

Resources