I want \ to be converted into / in this variable:
PROJECT_PATH=E:\share_folder\Perforce
FOR /f "tokens=*" %%a IN ('echo %PROJECT_PATH% ^| sed "s/\/\//g"/') DO ( set Replace0=%%a )
set WORKSPACE_PATH_FOR_PROP=%Replace0%
echo %WORKSPACE_PATH_FOR_PROP%
Result: E:\share_folder\Perforce
The result is the same, it did not change \ into /.
This is broken: take a sharp look at
sed "s/\/\//g"/
where you tell sed to substitute \\ into g, or somesuch, I don't know the quoting rules under Windows. Anyway,
sed "s,\\,/,g"
should work better. You might need to twiddle the quoting and backslashing, though. If you have it, you might also want to look at tr (translate characters) which is tailor-made for this task
tr "\\" /
Related
I'm using Cmder on Windows and trying to execute the following statement:
echo "c:\Sources\" + (echo "Modules/ASR/branches/1.9" | sed -e "s|Modules/||")
Whenever I issue that command I get the annoying error
sed: -e expression #1, char 13: unknown option to `s'
But when I run the sed command on its own:
(echo "Modules/ASR/branches/1.9" | sed -e "s|Modules/||")
It magically works. Could anyone explain what I'm doing wrong?
Editor's note: This question was originally mis-tagged bash, whereas the symptom described implies cmd.
The error message in the question is due to windows cmd which splits the command with the | and adds closing parenthese ) to sed command.
cmd equivalent for "command substitution" is for /f followed by command between single quotes, pipe must be escaped with ^
for /f %x in ('echo "Modules/ASR/branches/1.9" ^| sed -e "s|Modules/||" ') do #echo "c:\Sources\"%~x
Or when used in script the percent must be doubled
for /f %%x in ('echo "Modules/ASR/branches/1.9" ^| sed -e "s|Modules/||" ') do #echo "c:\Sources\"%%~x
Otherwise expansion allows to replace pattern %var:pat=repl%
set mypath=Modules/ASR/branches/1.9
echo "c:\Sources\"%mypath:Modules/=%
in bash equivalent
echo 'c:\Sources\'"$(echo "Modules/ASR/branches/1.9" | sed -e "s|Modules/||")"
also to avoid to launch a new process this can be done with variable expansion # to remove shortest prefix.
mypath="Modules/ASR/branches/1.9"
echo 'c:\Sources\'"${mypath//Modules\/}"
I have scripts containing the following line:
echo + /etc>>$BUFFER
I would like to edit them with sed and insert another line right after this:
echo + /etc>>$BUFFER
echo - /etc/cache>>$BUFFER
and leave the rest of the file as it was. What is the SED regexp to do this ?
sed -i '$a echo - /etc/cache>>$BUFFER' myfile.txt
The $ says the last line and a is append the -i option writes the file in place.
If you want the match anywhere you can address it with a regex / / like this just make sure to escape the / like / since sed expects the regex to be encapsulated in / /
sed -i '/echo + \/etc>>$BUFFER/a echo - /etc/cache>>$BUFFER' myfile.txt
Since the (copy & paste) of the above statement omits a space prior to the first + sign (likely due to a BUG in the software that runs the posts to this website) - here is version that should work when copied ):
sed -i '/echo \ + \/etc>>$BUFFER/a echo - /etc/cache>>$BUFFER' myfile.txt
or if you want to avoid escaping the single front slash in the future (not really useful in this case since its only one but if you had a lot of front slashes - again weird escaped space is only due to a BUG in the software of this website (or my browser firefox) if you type it on the command line you can put the spacing double yourself as it appears in the askers question) :
sed -i '\#echo \ + /etc>>$BUFFER#a echo - /etc/cache>>$BUFFER' myfile.txt
sed -i '\#^ *echo + /etc>>\$BUFFER *$# a\
echo - /etc/cache>>$BUFFER' YourFile
It will add the line after EACH occurence of echo + /etc>>$BUFFER.
I just add the line delimiter to limit to only this exact line content (but could have heading or trailing space)
If i understood you correctly this should work:
sed 's/echo \+ .*etc>>\$BUFFER/echo + \/etc>>\$BUFFER\necho - \/etc\/cache>>\$BUFFER/'
It isn't maybe best solution (im not sure how to deal with forward slash in pattern - theorethically it should be enough to escape it like: \/ but strangely it isn't working)
This should work though
sed 's_((checksum|compressed)=\").*(\")_\1\2_' -i filename
I am using this command to replace the checksum and compressed filed with empty? But it didn't change anything?
for example, I want change this line " checksum="XXXXX" with checksum="", and also replace
compressed="XXXX" with compressed=""
What is wrong with my sed command?
It's because sed uses a funny regex dialect by default: you have to escape capturing brackets.
If you want to use "normal" regex that you're familiar with, use the -r flag (if you're on unix, GNU sed) or the -E flag (Mac OS X BSD sed):
sed -r 's_((checksum|compressed)=\").*(\")_\1\3_' -i filename
Additionally, note that you have three sets of capturing brackets in your sed, and I think you want to change the \1\2 to \1\3. (\1 contains checksum=", \2 contains checksum, and \3 contains ").
(For interest, here's how you would do it without the extended-regexp (-r/-E) flag, note that capturing brackets and the OR | are only considered in the regex sense if they are escaped:
sed 's_\(\(checksum\|compressed\)=\"\).*\(\"\)_\1\3_' -i filename
)
This might work for you:
echo 'checksum="XXXXX" compressed="YYYYYYY"' |
sed 's/\(checksum\|compressed\)="[^"]*"/\1=""/g'
checksum="" compressed=""
In sed (without the -r switch), ()|+?{}'s must have a \ prepended to give them the qualities of grouping. alternation, one or more, zero or one and intervals. .[]* work as metacharacters either way.
Try:
sed 's/\(\(checksum\|compressed\)\)="[^"]*"/\1=""/' -i filename
In a bash script, files with spaces show up as "File\ with\ spaces.txt" and I want to substitute those slashed-spaces with either _ or +.
How can I tell sed to do that? I had no success using;
$1=~/File\ with\ spaces.txt
ext=$1
web=$(echo "$ext" | sed 's/\ /+/')
I'm open to suggestions if there's a better way than through sed.
[EDIT]: Foo Bah's solution works well, but it substitutes only the first space because the text following it is treated as arguments rather than part of the $1. Any way around this?
sed 's/\\\\ /+/';
\\\\ evaluates to a \\ at the shell level, and then into a literal \ within sed.
Sed recognises \ as space just fine:
bee#i20 ~ $ echo file\ 123 | sed 's/\ /+/'
file+123
Your bash script syntax is all wrong, though.
Not sure what you were trying to do with the script, but here is an example of replacing spaces with +:
ext=~/File\ with\ spaces.txt
web=`echo "$ext" | sed 's/\ /+/g'`
echo $web
Upd:
Oh, and you need the g flag to replace all occurences of space, not only the first one. Fixed above.
you want to escape the slash:
web=$(echo "$ext" | sed 's/\\ /_/g')
single quotes are your friend
the following should be used with single quoted args for $1 and $2
#!/bin/bash
ESCAPE='\\'
if [ $# -ne 2 ];then
echo "$0 <TO_ESCAPE> <IN_STRING>"
echo args should be in single quotes!!
exit 1
fi
TO_ESCAPE="${1}"
IN_STRING="${2}"
if [ ${TO_ESCAPE} = '\' ];then
TO_ESCAPE='\\'
fi
echo "${IN_STRING}" | sed "s/${TO_ESCAPE}/${ESCAPE}${TO_ESCAPE}/g"
I have the following: param="/var/tmp/test"
I need to replace the word test with another word such as new_test
need a smart way to replace the last word after "/" with sed
echo 'param="/var/tmp/test"' | sed 's/\/[^\/]*"/\/REPLACEMENT"/'
param="/var/tmp/REPLACEMENT"
echo '/var/tmp/test' | sed 's/\/[^\/]*$/\/REPLACEMENT/'
/var/tmp/REPLACEMENT
Extracting bits and pieces with sed is a bit messy (as Jim Lewis says, use basename and dirname if you can) but at least you don't need a plethora of backslashes to do it if you are going the sed route since you can use the fact that the delimiter character is selectable (I like to use ! when / is too awkward, but it's arbitrary):
$ echo 'param="/var/tmp/test"' | sed ' s!/[^/"]*"!/new_test"! '
param="/var/tmp/new_test"
We can also extract just the part that was substituted, though this is easier with two substitutions in the sed control script:
$ echo 'param="/var/tmp/test"' | sed ' s!.*/!! ; s/"$// '
test
You don't need sed for this...basename and dirname are a better choice for assembling or disassembling pathnames. All those escape characters give me a headache....
param="/var/tmp/test"
param_repl=`dirname $param`/newtest
It's not clear whether param is part of the string that you need processed or it's the variable that holds the string. Assuming the latter, you can do this using only Bash (you don't say which shell you're using):
shopt -s extglob
param="/var/tmp/test"
param="${param/%\/*([^\/])//new_test}"
If param= is part of the string:
shopt -s extglob
string='param="/var/tmp/test"'
string="${string/%\/*([^\/])\"//new}"
This might work for you:
echo 'param="/var/tmp/test"' | sed -r 's#(/(([^/]*/)*))[^"]*#\1newtest#'
param="/var/tmp/newtest"