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

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.

Related

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

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.

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.

Using wildcard characters while running system command in Perl Script

How can we use wildcard characters while running system command in Perl Script.
For example using *.src to edit a file with sed - something like :
system("sed -i -e 's/foo/bar/g' $baseDirPath/*.src");
It gives an error: sed: can't read /home/test/*.src: Not a Directory
Here, $baseDirPath is initialized to /home/test
Try to chomp the variable $baseDirPath before using it, As the line you have written should just work.

How to ignore read-only files with `perl -i`?

Perl’s -i switch appears to modify read-only files:
$ echo 'foobar' > tmp.txt
$ chmod -w tmp.txt
$ perl -pi -w -e 's/foobar/FOOBAR/' tmp.txt
$ cat tmp.txt
FOOBAR
This is unexpected, as the command should not have been able to modify the file per its permissions. Expectedly, trying to update it via other means fails:
$ echo 'barbaz' > tmp.txt
-bash: tmp.txt: Permission denied
Why is Perl modifying read-only files (and how?), and, most importantly: how can I get Perl to not do so?
The only somewhat informative resource I can find on this is in the Perl FAQ:
The permissions on a file say what can happen to the data in that file. … If you try to write to the file, the permissions of the file govern whether you're allowed to.
Which ultimately seems like its saying it shouldn’t be able to write to it, since the file system says you cannot.
Filter #ARGV in a BEGIN block:
perl -pi -e 'BEGIN{#ARGV=grep{-w $_}#ARGV} s/foobar/FOOBAR/' files
Now if none of the files on the command line are writable, #ARGV will be empty and the ARGV filehandle will try to read from STDIN. I can think of two ways to keep this from being a problem:
Close STDIN in the BEGIN block, too
perl -pi -e 'BEGIN{close STDIN;#ARGV=grep{-w $_}#ARGV}s/foobar/FOOBAR/' files
Always call this one-liner redirecting input from /dev/null
perl -pi -e 'BEGIN{#ARGV=grep{-w $_}#ARGV}s/foobar/FOOBAR/' files < /dev/null
See the documentation in perlrun:
renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements
(...)
For a discussion of issues surrounding file permissions and -i, see "Why does Perl let me
delete read-only files? Why does -i clobber protected files? Isn't this a bug in Perl?" in
perlfaq5.
From perlrun:
-i
specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements.
So it is doesn't really modify the file. It moves the file out of the way (which requires directory write permissions, not file write permissions) and then creates a new one with the old name.
how can I get Perl to not do so?
I don't think you can when you use -i.

What causes Permission denied when using sed?

Running the following line in my script gives me a Permission denied error:
$config_out | sed -e 's#name="in" value=""#name="in" value="1600"#' > $config_out".tmp"
Before I had $config_out | sed -e 's#name="in" value=""#name="in" value="1600"#' > $config_out,so without the .tmp at the end. I read somewhere that you get the error when trying to pipe the output to the same file that you are reading it from. But when I'm writing it to another file, I don't know why I get that error.
Is $config_out the name of a file or a command? If it's a file, then you need to either cat $config_out | sed ... or sed ... < $config_out > $config_out.tmp. If it's a command, though, the first command might have truncated/overwritten whatever it was (shell script or perl script or something?).