Sed an entire line with only the begin of the line - bash

I have a config file with this line:
foo.bar = http://localhost/foo
I want to replace this line with a new url. For example:
foo.bar = http://new_url/bar
But I don't know the original url, I only know the name of the parameter.
I tried this:
sed "s!^foo.bar.*!foo.bar = http://new_url/bar!" test.ini
But it return an error

Use single quotes to prevent shell expansion:
sed -e 's!^foo.bar.*!foo.bar = http://new_url/bar!'

In bash double-quotes and ! don't go well when placed inside ( see HISTORY EXPANSION in man bash), change the separator to # (or) use single-quotes
sed 's#foo.bar.*#foo.bar = http://new_url/bar#' file
foo.bar = http://new_url/bar

Related

pass dynamic value in the sed command in unix inside IT block ruby

output.txt:
select * from table startTime.... (very big query)
abc.rb:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/$time/g' ./db/output.txt`
end
expected output----
output.txt:
select * from table 6537290102....
In output.txt, the value should be 6537290102 please someone help. thanks in advance
If I hardcode the time value as shown below in sed command then it is working fine whereas the passing the dynamic value "time" is not working
replacestarttime = sed -i -e 's/startTime/6537290102/g' ./db/output.txt
Replace single quotes with double quotes
`sed -i -e "s/startTime/$time/g" ./db/output.txt`
Interpolate Your String in Ruby
Right now, you're expecting sed to access a shell variable named $time. This variable doesn't exist, and wouldn't work as-is even if it did.
To fix this, you should to interpolate your Ruby variable into the string passed to sed. You can do this as follows:
it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/#{time}/g' ./db/output.txt`
end

Dynamically replace string value in configuration file

I'm building a shell script to install apache-airflow and update some configurations for local development.
I would like to replace the value of property dags_folder. How can I replace it using shell script?
Here is what I've tried:
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_EXTRA=" "
else
SED_EXTRA=""
fi
sed -i${SED_EXTRA}'' "s,dags_folder = ,dags_folder = banana,g" ~/airflow/airflow.cfg
It's almost working but I want to replace entire value. Is possible?
Don't use sed at all. Use ed, which won't require OS-specific changes to the invocation.
You want to match the entire line, not just the name = prefix, so add .* to the regular expression.
ed ~/airflow/airflow.cfg <<EOF
s,dags_folder = .*,dags_folder = banana,g
wq
EOF
Match up until the end of the line with .* regex
sed -i${SED_EXTRA}'' 's,dags_folder = .*,dags_folder = banana,g' ~/airflow/airflow.cfg
try this one if I understand your issue correctly :)
var1="dags_folder ="
var2="dags_folder = banana"
sed -i 's/'"$var1"'/'"$var2"'/g' path to your cfg file

How do I replace text using a variable in a shell script

I have a variable with a bunch of data.
text = "ABCDEFGHIJK"
file = garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf
What I would like to do is replace the ] charachter in file with text. I've tried using sed but I keep getting odd errors.
output should be:
//iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
Just need to escape the ] character with a \ in regex:
text="ABCDEFGHIJK"
sed "s/\(.*\)\]\(.*\)/\1$text\2/" file > file.changed
or, for in-place editing:
sed -i "s/\(.*\)\]\(.*\)/\1$text\2/" file
Test:
sed "s/\(.*\)\]\(.*\)/\1$text\2/" <<< "iiuhdsfiuhdsihf]sdiuhdfoidsoijsf"
# output => iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
There is always the bash way that should work in your osx:
filevar=$(cat file)
echo "${filevar/]/$text}" #to replace first occurence
OR
echo "${filevar//]/$text}" #to replace all occurences
In my bash i don't even have to escape ].
By the way, the simple sed does not work?
$ a="AA"
$ echo "garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf" |sed "s/]/$a/g"
garbage.txt //iiuhdsfiuhdsihfAAsdiuhdfoidsoijsf

Getting bad flag in substitute command '/' for sed replacement

I am facing one issue while using replace in my sed command. I've a file named a.txt, I want to replace a single line with some other line but I am getting above mentioned error.
Code I want to replace:
DOMAIN_LOCAL = "http://127.0.0.1:3000";
I want it to replace with any other ip lets say something like below:-
DOMAIN_LOCAL = "http://127.1.1.2:3000";
What I've tried:-
ip=http://127.1.1.2:3000
sed "s/DOMAIN_LOCAL = .*$/DOMAIN_LOCAL = "$ip";/g" a.txt
But it is giving me following error. Can somebody help here.
sed: 1: "s/DOMAIN_LOCAL = .*$/DO ...": bad flag in substitute command: '/'
You will need to use another delimiter. And escape the $ or use single quotes:
% ip="http://127.1.1.2:3000"
% sed 's~DOMAIN_LOCAL = .*$~DOMAIN_LOCAL = "'"$ip"'";~' a.txt
DOMAIN_LOCAL = http://127.1.1.2:3000;
When you use / as a delimiter in the substitute command it will be terminated at the first slash in http://:
sed 's/DOMAIN_LOCAL = .*$/DOMAIN_LOCAL = http://127.1.1.2:3000;/'
# ^ here
Breakdown:
sed 's~DOMAIN_LOCAL = .*$~DOMAIN_LOCAL = "'"$ip"'";~' a.txt
# │ ││└ Use double quotes to avoid word splitting and
# │ ││ globbing on $ip
# │ │└ Exit single quotes
# │ └ Literal double quotes
# └ Using single quotes to avoid having to escape special characters
You may need another delimiter instead of "/", for example "%".It worked for me.
use # replace / to avoid this issue
$ sed 's#find#replace#' file

How do I insert a newline/linebreak after a line using sed

It took me a while to figure out how to do this, so posting in case anyone else is looking for the same.
For adding a newline after a pattern, you can also say:
sed '/pattern/{G;}' filename
Quoting GNU sed manual:
G
Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.
EDIT:
Incidentally, this happens to be covered in sed one liners:
# insert a blank line below every line which matches "regex"
sed '/regex/G'
This sed command:
sed -i '' '/pid = run/ a\
\
' file.txt
Finds the line with: pid = run
file.txt before
; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
; Error log file
and adds a linebreak after that line inside file.txt
file.txt after
; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
; Error log file
Or if you want to add text and a linebreak:
sed -i '/pid = run/ a\
new line of text\
' file.txt
file.txt after
; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
new line of text
; Error log file
A simple substitution works well:
sed 's/pattern.*$/&\n/'
Example :
$ printf "Hi\nBye\n" | sed 's/H.*$/&\nJohn/'
Hi
John
Bye
To be standard compliant, replace \n by backslash newline :
$ printf "Hi\nBye\n" | sed 's/H.*$/&\
> John/'
Hi
John
Bye
sed '/pattern/a\\r' file name
It will add a return after the pattern while g will replace the pattern with a blank line.
If a new line (blank) has to be added at end of the file use this:
sed '$a\\r' file name
Another possibility, e.g. if You don't have an empty hold register, could be:
sed '/pattern/{p;s/.*//}' file
Explanation:
/pattern/{...} = apply sequence of commands, if line with pattern found,
p = print the current line,
; = separator between commands,
s/.*// = replace anything with nothing in the pattern register,
then automatically print the empty pattern register as additional line)
The easiest option -->
sed 'i\
' filename

Resources