Overwrite a url config file using Sed [duplicate] - bash

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I have a flag on a file
APP_URL=http://localhost
I want to update it to
APP_URL=http://aws.test
I want to overwrite it, I tried
sed -i -e 's/APP_URL=http://localhost/APP_URL=http://aws.test/g' .env
and
sed -i -e 's/APP_URL="http://localhost"/APP_URL="http://aws.test"/g' .env
I kept getting
sed: 1: "s/APP_URL="http://local ...": bad flag in substitute command: 'l'
How would one go about debugging this further?

You have too many forwardslashes in your command. Either escape the ones in the url with \/ or use a different separator for sed, ie:
sed 's#replace/this/string#with/this/one#g'

Related

sed -i ‘’ -e '/javaagent.jar/ s/$/ proxyPort=8080/' this command should append string only if the line is uncommented [duplicate]

This question already has answers here:
sed - Commenting a line matching a specific string AND that is not already commented out
(10 answers)
sed in-place flag that works both on Mac (BSD) and Linux
(15 answers)
Ignore comments (#) using sed, but keep the lines untouched
(5 answers)
Closed 2 years ago.
Im using below to append proxyport=8080 to the end of the line where javaagent.jar string found and below is working fine, but this should ignore if there is a # (commented line) in the text file
sed -i '' -e '/javaagent.jar/ s/$/ proxyPort=8080/'
Example input:
#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar
Example output should be:
#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar proxyPort=8080
Assuming you have a FreeBSD sed version (as you have -i '' in your command), you can use
sed -i '' -e '/^#/!s/javaagent\.jar.*/& proxyPort=8080/' file
See the online demo.
The /^#/! part stops processing lines starting with # (add [[:space:]]* after ^ to account for indentation, any leading whitespace), and if there is no # at the start, s/javaagent\.jar.*/& proxyPort=8080/ finds javaagent.jar + the rest of the line and replaces this match with itself (with &) and adds a space and proxyPort=8080.

How do I remove a line that does not contain a specific character? [duplicate]

This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 2 years ago.
For example, I want do remove all lines in a textile that do not contain the character '#'
I have already tried to use sed like so
sed '/#/!d' data.txt
What am I missing? Shouldn't this work?
I prefer using ed over the non-standard sed -i, especially if it needs to be portable:
printf "%s\n" "v/#/d" w | ed -s filename
This deletes every line that doesn't contain a #, and saves the changed file back to disc.
sed -n '/#/p' [file]
-n suppress default printing
/#/ match on # anywhere on the line
p print if it matches
Add -i for in-place editing of the file (if supplied).

Replace <br /> using sed does not work [duplicate]

This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 4 years ago.
I want to remove <br /> from a file using sed -i ....
The / gives me trouble.
This works:
sed -i e 's/<br/''/g' File.txt
But this didn't work:
sed -i e 's/'<br />'/''/g' File.txt
Use any other char after the s
sed -i 's_/_+_'
Should work
the s subcommand from sed means substitution, and uses the next char as a delimiter, it could be almost anything. but it is a good pratice to not use any char that is related to your target text (HTML in this case), like <>/#.
Also it is advisable to avoid shell relevant ones $!()

How to delete first column from the file in Unix [duplicate]

This question already has answers here:
Delete a column from a delimited file in linux
(7 answers)
Closed 8 years ago.
I have a file like the one below -
|A|B|C|D
|1|2|3|4
I want the result -
A|B|C|D
1|2|3|4
I have tried using cut but I'm not getting the desired output. Please suggest how the first column can be removed?
Using sed, delete the leading pipe symbol:
sed 's/^|//' file
There's an outside chance that on some versions of sed you'd need to escape the pipe. You might be able to use the over-write mode too (though not all versions of sed support that):
sed -i .bak 's/^\|//' file

Print new line with sed using variable [duplicate]

This question already has an answer here:
SED command not being run from bash script
(1 answer)
Closed 8 years ago.
I'm trying to print a new line after 'string' using the variable THIS into file.
sed -i '' 'string/a\
${THIS}
' "${f}"
It prints "${THIS}" into file, literally. I've tried using double quotes for sed but that leads to error message:
"sed: 1: "...": command a expects \ followed by text"
I've also tried to Google this and have been browsing this excellent forum but couldn't find anything.
Any idea, please? Thank you!
It should look like this:
THIS="foo"
sed 's/string/string\n'"${THIS}"'/g' file.txt
Btw, if you are unsure I encourage you to be careful using the -i option. I would play with the sed command until I'm sure and only then use -i (which will overwrite the original file)
If you are replacing a literal string (no regex), the replace command might be better than sed here:
replace string "string"$'\n'"${THIS}" file.txt

Resources