sed: Using a variable in sed - bash

I am writing a script that changes the paths in a file. This is what I want to do. If I have a file that has the string "/path/to/incorrect/location", I want to change it to "/path/to/correct/location". I have tried using sed to do this by doing this...
sed -i "s/$badpath/$goodpath/g" file
Doing this though, does absolutely nothing and I cannot for the life of me figure it out. I do need to use the variable for the bad path, but the good path can be written out (since that one never changes). How would one do this?
MORE INFORMATION:
I am using GNU sed, so the -i flag is valid.

Seems to work for me, after fixing the delimiters:
$ cat sample
/path/to/incorrect/location
$ badpath="/path/to/incorrect/location"
$ goodpath="/path/to/correct/location"
$ sed -i "s|$badpath|$goodpath|" sample
$ cat sample
/path/to/correct/location

Related

Use a shell script to replace text with pwd

file.txt
...
<LOCAL_PATH_TO_REPO>/src/java/example.java
...
^A longer file but this pretty much explains what I am trying to do.
script.sh
dir=$(pwd)
# replace <LOCAL_PATH_TO_REPO> with dir
I tried using the sed command but it did not work for some reason. Any ideas on how to do this?
Your error means you have backslashes in the variable text.
The simplest solution is to change the delimiter to the one that does not occur in the variable text.
If there are no commas use a comma:
sed -i "s,LOCAL_PATH_TO_REPO,$PWD," file.yml
The -i flag introduces changes into the input file (works for GNU sed).

Prepend, copy/paste, and append using sed?

I have a file full of IDs which I need to use to build a list of URLs as part of a bash file.
ids.txt is as follows:
s_Foo
p_Bar
s1_Blah
e_Yah
The URLs will always end in a filename that contains the ID, in its own path.
I've looked around for how to prepend and append using sed, but cannot figure out to do the duplicating copy/paste part (\1) using that tool. The ID can be anything, so pattern matching seems hard. Duplication of everything before the line break seems more sensible? I don't know.
How do I create something like this as urls.txt using sed or awk? Is it possible?
https://link.domain.com/list/s_Foo/s_Foo_meta.xml
https://link.domain.com/list/p_Bar/p_Bar_meta.xml
https://link.domain.com/list/s1_Blah/s1_Blah_meta.xml
https://link.domain.com/list/e_Yah/e_Yah_meta.xml
$ sed 's#.*#https://link.domain.com/list/&/&_meta.xml#' ids.txt
https://link.domain.com/list/s_Foo/s_Foo_meta.xml
https://link.domain.com/list/p_Bar/p_Bar_meta.xml
https://link.domain.com/list/s1_Blah/s1_Blah_meta.xml
https://link.domain.com/list/e_Yah/e_Yah_meta.xml
$ awk '{sub(/.*/,"https://link.domain.com/list/&/&_meta.xml")}1' ids.txt
https://link.domain.com/list/s_Foo/s_Foo_meta.xml
https://link.domain.com/list/p_Bar/p_Bar_meta.xml
https://link.domain.com/list/s1_Blah/s1_Blah_meta.xml
https://link.domain.com/list/e_Yah/e_Yah_meta.xml
try gnu sed:
sed -E 's/\S+/https://link.domain.com/list/&/&_meta.xml' ids.txt >urls.txt

Unable to modify file as part of entry point command

My Dockerfile's entry point CMD executes a shell script to modify a local file based on an environment variable before executing my application (Flask). The shell script is like so:
cat static/login.html | sed "s/some_match/some_substitute/g" > static/login.html
However, I am finding that the resulting file is zero bytes. Any ideas what might be going on?
Thanks.
I can reproduce your problem, but not explain it. Maybe other answers follow.
But here is a solution which fixes the problem:
Use a different file name for the output than for the input.
cat input.txt | sed "s/a/b/g" > input2.txt
Alternatively, use the -i.bak option.
sed -i.bak "s/b/a/g" input.txt
I can only speculate about what exactly is going on:
Maybe the output of your pipe is opened for writing (non-appending) before the input is read.

Mac OS X remove line from multiple files

I'm attempting to remove a line from several hundred files. The following does exactly what I need but, it doesn't save changes (as expected).
$ grep -v meow src/files
I've seen that appending > to the end of a given command will specify where the output buffer should save but, does this work for multiple files?
So I'd like to know if there's an elegant way to mass edit via the terminal. All of the examples I've come across using awk or sed only provide solutions for editing one file at a time.
One way to do this is using the following Perl one-liner:
perl -i.bak -n -e 'print unless /meow/' src/files
This should do in-place editing of multiple files. The originals are saved in .bak files.
Another way to do it is to do a similar operation with sed:
sed -i .bak '/meow/d' src/files/*
Perl got its -i option from sed, after all. Note that to use no backup file, you need an explicit empty extension with at least some versions of sed:
sed -i '' '/meow/d' src/files/*

How to replace a line in sed?

I have a configure file which has a line ServerIP= in it. Now I want to use find this line and add a new IP address to it, i.e. replace it with ServerIP=192.168.0.101, what is the command like?
You can employ a find-and-replace command to do this:
sed -e 's/\(^ServerIP=\)/\1192.168.0.101/g' your_file
Are we doing this all over the file or only in one spot? The command above should replace it everywhere. You will have to send the output somewhere. I never edit in place with sed because I make too many mistakes.
One tricky thing is this part, \1192.168.0.101, which actually can be broken down like this:
\1 --> the thing we captured
192.168.0.101 --> the thing we are placing IMMEDIATELY after the thing we captured
Also, you may have other lines that look a little different. But, in the future, look up "sed capture and replace".
This one would work whether there's an existing value in ServerIP or not:
sed -i 's#\([[:blank:]]*ServerIP=\)[[:digit:].]*#\1192.168.0.101#' file
I also suggest that you try to learn using CLI editors like VIM or Nano instead.
try:
sed -i 's/^ *ServerIP=/&192.168.0.101/' file
I would do:
sed -i 's/^ServerIP=$/ServerIP=192.168.0.101/' file.config

Resources