Print new line with sed using variable [duplicate] - bash

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

Related

Replace a string with special characters using sed [duplicate]

This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 7 months ago.
I have the below string
sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$
I want replace .*app\.com$|localhost$|127\.0\.0\.1$ with * so that final string looks like below
sec.val.hos.patn=*
I am trying to solve this problem using below sed command on Mac OS
sed -i ' ' 's~\.\*app\\\.com\$\|localhost\$\|127\\\.0\\\.0\\\.1\$~\*~g' file.txt
but unable to get the desired replacement. Can someone please help me to get this working.
I don't know if the pattern is actually more complex than you sample, but seems this should do the trick:
sed 's/\(^.*=\).*$/\1*/' <<< "sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$"
Here we capture everything from the start to =, then we replace the whole thing with capture group 1 plus *.

Overwrite a url config file using Sed [duplicate]

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'

How to remove a line from a file containing certain pattern value in variable? [duplicate]

This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 4 years ago.
I have a file that contains names of directories and some other information, but the names always come first.The file looks like this:
/home/user/Desktop/IS/proj_1/sch/text 4 2018-03-14 07:41:01
/home/user/Desktop/IS/file1.txt 3 2018-03-14 16:50:01
...
I have a variable "name" that contains this for example:
/home/user/Desktop/IS/file1.txt
And I need to delete that one particular line from the file somehow. I've searched many posts and tried using various quotations and ways of expansions, but nothing did the trick. If I type it in directly, it deletes the line without problem, but I'm having a hard time doing it from a variable. This is what I came up with but it still doesn't work.
sed -i '/"$name"/d' $File_name
Try this :
sed -i "\|$name|d" "$File_name"
As you can see, I changed the delimiter for |, you can pick another one depending of your needs from most of ascii characters (not all works)
sed command doesn't allow plain string based search and performs search using only a regex (BRE or ERE). That requires escaping all special regex meta-characters in search pattern.
Better to use a non-regex approach using awk:
name='/home/user/Desktop/IS/file1.txt'
awk -v p="$name" '!index($0, p)' file
/home/user/Desktop/IS/proj_1/sch/text 4 2018-03-14 07:41:01
Whatever given in single quotes wont get expanded.
Try:
sed -i "/$name/d" $File_name
If you have problems with /, escape them properly.
name=$(echo "$name"|sed -e "s/\//\\\\\//g")
sed -i "/$name/d" $File_name

Find variable word and replace it in file using Bash [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 5 years ago.
I have a file and from this file I am trying to find a word and replace it with another word using Bash. I am using sed to do this and please note that the word that I am looking for is an output from a command. So I am trying to find a word, which is the output of a command, and replace it with another word and override the previous word.
This is my code:
File=file.txt
File2=file2.txt
min=$(cat $File2 | grep word);
sed -i 's/$min/max/g' $File
It's not producing any error, but I am unable to find the word in order to replace it. When I manually type the word rather than using the variable "$min" it works just fine. So when I do this, it works:
sed -i 's/min/max/g' $File
but when I do this, it doesn't:
sed -i 's/$min/max/g' $File
I am thinking maybe sed doesn't accept variables as a search string. Any idea how I can achieve this?
thank you.
Use double quotes for the sed expression, this should work:
sed -i "s/$min/max/g" $File

SED command gives unknown option to `s' error [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 7 years ago.
I have a file like this:
1 = zara
2 = gucci
I want to write a bash script which read two variables and decides to change the number in one of lines to a new number. I have this script for this purpose:
pattern="$num[[:space:]]=[[:space:]].*"
sed -ie 's,'"$pattern"','"$num"',\ =\ '"$newBrandName"',' $fileLocation
Variables num, newBrandName and fileLocation have the right value; I have checked this by echo command.
But when I run the script, I get this error:
sed: -e expression #1, char 52: unknown option to `s'
The output of echo on the SED command is this:
sed -ie s,1[[:space:]]=[[:space:]].*,1\ =\ sign, /root/info.info
Can anyone help me with the regex?
The form of s is s/regexp/replacement/. You can't have multiple expressions on it. Try this instead:
sed -i -e '/^${num}[[:space:]]*=[[:space:]]*/s|.*|$num = $newBrandName|" "$fileLocation"
It would find the line matching ^${num}[[:space:]]*=[[:space:]]* and replace everything on it with $num = $newBrandName.
You also should separate -i and -e so e would not be interpreted as the backup suffix for -i.
If think it's because you have an extraneous , left :
pattern="$num[[:space:]]=[[:space:]].*"
sed -ie 's,'"$pattern"','"$num"',\ =\ '"$newBrandName"',' $fileLocation
--------------------------------^
You might consider escaping it, or use another character like #.

Resources