cant figure out proper syntax for sed using double quotes - bash

I have an variable
qsubFile="submitJob.sh"
echo $qsubFile returns submitJob.sh without the double quotes.
Now, I want to find the line containing the string qsubFile="someOtherFile.sh" and replace it to qsubFile="submitJob.sh" in the file "write.sh".
I tried using
sed -i '/qsubFile=/c\qsubFile="'"$qsubFile"'"' write.sh
qsubFile=""
I can't seem to get the proper syntax for this.
but it replaces it as

You just need single quotes for sed to do this, there is no problem with the double quotes inside the single quotes:
sed -i 's/qsubFile="someOtherFile.sh"/qsubFile="submitJob.sh"/g' write.sh
If "someOtherFile.sh" isn't a fixed string in write.sh than use the follow to replace them all:
$ sed -i 's/qsubFile="[^"]*"/qsubFile="submitJob.sh"/g' write.sh
Regex "[^"]*":
" # double quote
[^"]* # Anything not a double quote
" # double quote
Seems I misread the question the first time the correct quoting is to use the variable $qsubFile is, you missed the last /:
sed -i 's/qsubFile="[^"]*"/qsubFile="'"$qsubFile"'"/g' write.sh

You're somewhat on the right track if you need to use the shell variable though.
sed -i 's/qsubFile="someOtherFile.sh"/qsubFile="'"$qsubfile"'"/g' write.sh
or if you want to make sure you get the whole line
sed -i 's/^\(qsubFile=\).*$/\1"'"$qsubfile"'"/g' write.sh

Related

replace file path name from multiple file using sed

I want to replace <lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/> with null in multiple files.
The code is given below.
sed -i s|<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>||g *
Here I am getting this error:
< was unexpected at this time.
Please clarify for me what is not working here.
Could you please try following and let me know if this helps you. By using # as sed's separator you need not to escape / in it only need to escape ., ? not to take their special meaning
sed -E 's#<lexicon uri="file://C:/image/png/grammars/custom/image-custom\.lex\?SWI\.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom\.lex\?SWI\.type=backup"/>##' Input_file
Tested it with:
sed --version
GNU sed version 4.2.1
works with #
sed -i -e 's#<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>##g' test.txt
The pattern contains shell metacharacters, which need to be quoted or escaped. Usually, in Bash, you should use single quotes around strings, unless you need the shell to interpolate variables and command substitutions and interpret backslash sequences (in which case use double quotes) or to also perform whitespace tokenization and wildcard expansion (in which case use no quotes). See also When to wrap quotes around a shell variable?
sed -i 's|<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>||' *
I also took out the g flag, which only makes sense if you expect multiple matches within a single line. (Perhaps you do after all, in which case obviously put it back.)

Variables not working in sed command

How to use a variable instead of 3 in the command below?
sed -i '3s/$/ newvalue/' filename
I tried
var=1
sed -i '$vars/$/ newvalue/' filename
sed -i "$vars/$/ newvalue/" filename
First, you need to use double quotes to allow shell parameters/variables to expand. Then, you need to use braces to isolate the variable name if text that follows the variable could be interpreted as part of variable name (before${var}after). Finally, to use literal $ under double quotes, you should escape it a blackslash. All together:
var=3
sed -i "${var}s/\$/ newvalue/" filename
One alternative is to use alternating double and single quotes (under which no character is treated specially, including $ for parameter expansion):
sed -i "$var"'s/$/ newvalue/' filename

I want to concat a character before and after a word in bash

I have declared a variable in a file like this:
export psw=text
And I would like to concat the ' single character before and after the value of the variable. I mean, I want to replace the value for something like this:
export psw='text'
How can I get that done?
I want to do it though a command. I don't want to do it manually.
Simple and easy with Perl
perl -i -lpe 's/\=(\w+)$/='"'\\1'"'/' your-file
-i save in-palce
output
export psw='text'
how it works
s/.../ this part matches want you wnat
/.../ this part is for substitution that part that you have matched already.
So in the first step you match =(\w+)$
and equal sign and a word and it should be end of the line. Okay after that you change this part to
/='"'\\1'"'
that means put an equal singe and a single quote that what that matched by match group operator () and then another single quote.
So it matches: =text
then substitute it with ='text'
-i is for save the result
-p is for printing to the screen + a while loop
-l put a new line
-e a temporary program.
Just play with it without -i and then you little by little realize how it works.
NOTE
'"' is just for escape the single quote in bash.
\\1 as well this one
This sed command will do your job:
sed -i.bak -E "s/(export[ \t]+[[:alnum:]]+=)([^']+)/\1'\2'/" file
The above expression would only add the single quotes when they are not there.
if you prefer the echo approach, you could also do it this way
pwd=text
export psw=$(echo "\'$pwd\'")
\ are needed to escape the single quotes so it'll be part of the string and you assign the output of echo of you variable with added quotes to itself

Insert single quote with sed

I want to add some text on a line:
sudo sed -i '5imytext 16/16' /file
Now I've added mytext 16/16 on line 5 of the file, but I actually want to add the text 'mytext' 16/16 (mytext between single quotes).
I tried
sudo sed -i '5i'mytext' 16/16' /file
but it didn't work. Can someone help me?
The single quotes that you're trying to use in your insertion string are interfering with the ones around the sed command.
The simplest thing to do is to use different quotes around your sed command:
"5i'mytext' 16/16"
Normally it's best to use single quotes around a sed command but it would be more tricky in this case:
'5i'"'"'mytext'"'"' 16/16'
Basically, you need to put the single quotes inside double quotes somehow and in this case there's no reason not to double quote the whole command.
As suggested by 123 in the comments, an alternative would be to put your sed command into a script file:
5i'mytext' 16/16
Then use the -f switch to sed:
sed -f script
This avoids the need to use two kinds of quotes.
Use double quote in these cases. Because:
Single quote can't have single quote inside it. ('\'' won't work)
Double quote can have both single quote and double quote inside it. ("'\"" will work)
Example:
sudo sed -i "5i'mytext' 16/16" /file
You could use double quotes around your sed command, but that won't help you if you also need to insert double quotes. An alternative would be to use: \x27
Example: echo a|sed 's/a/\x27/' ➡ '

variable in sed

how can I use a variable as a pattern finder for sed? for example:
sed -i '/$pc/ s/off/on/' ~/Documents/Mantenimiento
I know there is a $ in between the '' but there gotta be a way! please help!
Use double quotes instead of single quotes, or close the quotes just before the variable and reopen them just after.
sed -i "/$pc/ s/off/on/"
sed -i '/'$pc'/ s/off/on/'
This will let bash perform the variable evaluation normally.

Resources