Search for a URL in a file and replace with local file path from commandline - sed

I have a Python file on disk, and I want to search a line in the function and replace the URL in the line with a local file path.
def showBuilderHelp():
from webbrowser import open as openUrl
openUrl('https://github.com/jobyski/public_help_v1.1.pdf')
when I tried with sed
sed -i 's/https://github.com/jobyski/public_help_v1.1.pdf/file:///on/disk/path/file/public_help_v1.1.pdf/g' thefile.py
but this throws error
sed: couldn't open file https:/github.com/jobyski/public_help_v1.1.pdf/g No such file or directory
I am not an expert in sed or grep.

Aha, that was simple enough:
sed -i 's/https:\/\/github.com\/jobyski/file:\/\/\/on/disk\/path\/file/g' thefile.py
Updates the file.

Related

Bash Script to append for specific file extensions in a directory

How do I prepends a special character in front of all the lines in all .txt files in my directory? Im new to writing bash scripts and having trouble doing this. I only know of using the grep function but thats only to search for keyword.
For now, I have this,
sed -i 's/^/#/' Machine1.txt
However, this is only for that specific .txt file. I want to do this for all files with a .txt extension in my directory. There are other extensions like .tar, .rpm, .sh files which I want to ignore. Thank you!
Just give a wildcard filename argument.
sed -i 's/^/#/' *.txt
you can use for loop.
For example
cd your_folder
for f in *.txt; do
sed -i 's/^/#/' "$f";
done

How to Replace a path with another path using sed in a chef resource

I have a file which has below path-
JVM == /home/user/tools/jdk/bin/java
I want to replace "/home/user/tools/jdk/bin/java" with "/apps/java/bin"
I use below command in chef resource but it does not work-
sed -i -e 's//home/user/tools/jdk/bin/java//apps/bin/java/' testfilename
I get below error-
STDERR: sed: couldn't open file ser/tools/jdk/bin/java//apps/bin/java/: No such file or directory
Check out the line cookbook for this kind of operation.

sed: can't read No such file or directory

sed -i -e "s#^ filename:.*.infos.log# filename:${log_dir}/infos.log#" ${default_config_dir}/logging.conf
I tried to execute the above command but it always tells me that
sed: can't read /logging.conf: No such file or directory
even though there is a file in that location with that name.
The leading slash in your message is a clear indication that the variable ${default_config_dir} is either unset or empty.

sed outputs correct change on console but doesn't edit in file

I am trying to edit my file using this sed command
sudo sed "s/192[^:]\+ /192.168.56.109/" file
The command outputs the complete file with correct change, but doesn't edit the file. If I open the file it is same as before.
if inplace -i option is not working you can do this
sed "s/192[^:]\+ /192.168.56.109/" file > temp && mv temp file
make sure you have a backup though.

sed to replace value in .bashrc

I have the following line in my .bashrc:
APP_HOME=/home/user/app/1.0;export APP_HOME; ## ADDED BY INSTALLER - PLEASE DO NOT EDIT OR DELETE THIS LINE
I want to replace APP_HOME=/home/user/app/1.0 with a different path, say /home/user/app/2.0 this could really be anything.
I have the following:
sed s,APP_HOME=,"/home/user/app/2.0", -i ~/.bashrc
However, what I get in the file is the replacement path appended to the original. What am I doing wrong?
You are replacing 'APP_HOME=' with the new path. Try this:
sed 's,APP_HOME=[^;]*,"APP_HOME=/home/user/app/2.0",' -i ~/.bashrc