This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 1 year ago.
I have test.txt file and I want to add my variable to specific line after the word. But my sed command not work how can I do this?
test.txt file is:
Hello:
Green:
Yellow:
My variable is
echo $variable
http://111.111.111.11
My bash command is:
sed '/Green:/r/dev/stdin' test.txt <<<"$variable"
My output after run the bash command:
Hello:
Green:
http://111.111.111.11
Yellow:
My desire output:
Hello:
Green:http://111.111.111.11
Yellow:
You can place your variable directly into the substitution:
sed "s~\(Green:\)~\1$variable~" test.txt
This will match the string Green: and capture it into group \1, you can then append $variable to the captured group in the substitution. Note I have used ~ as a separator as your substitution string contains multiple slashes / which cause problems if you also use / as the separator.
Related
This question already has answers here:
How do I replace single quotes with another character in sed?
(6 answers)
Closed 5 months ago.
Trying to put ' before each line of text and ' at the end of each line of text.
I have been using sed 's/^/1/' file.txt to replace to begging of each line and sed 's/$/0/' file.txt to replace the end of each line.
What I am trying to make work is sed 's/^/'/' and sed 's/$/'/'
This would format my file to make each line reach as a command, when applied to a separate script.
echo abc | sed "s/.*/'&'/"
Output:
'abc'
From man sed:
The replacement may contain the special character & to refer to that portion of the pattern space which
matched
This question already has answers here:
How to escape the ampersand character while using sed
(3 answers)
Closed 2 years ago.
My config file looks like:
KEY1=VALUE1
URL=https://drive.google.com/uc?export=download&id=myhash
KEY3=VALUE3
I'm trying to use sed to replace the URL value with another one. I got to the following:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download&id=mynewhash#g' file.txt
But that doesn't seem to work, as I'm getting:
URL=https://drive.google.com/uc?export=downloadURL=https://drive.google.com/uc?export=download&id=mynewhash=myhash
What am I missing? Thanks
& is a special character in the replacement string provided to the s command of sed. It represents the string that matches the entire regex used to search (URL=.* in your example).
In order to represent itself it needs to be escaped with \:
sed -i.bak 's#URL=.*#URL=https://drive.google.com/uc?export=download\&id=mynewhash#g' file.txt
Type man sed in your terminal to read its documentation or read the documentation of sed online.
This question already has answers here:
Remove everything after the first / including the first / for each line
(3 answers)
Closed 8 years ago.
I'm trying to use command line to find and replace some text. I have a file with a few million lines that are similar to this:
Something-Here/Grafton-WV</loc>
More-Information/Claremore-OK</loc>
This-Is-It/Seminole-OK</loc>
Your-Company/Lunenburg-MA</loc>
What I need to do is remove the slash and everything after it. I've done wildcard find/replace before but I'm not sure what command would need to be used to start at the slash and continue until the end of the line.
Here's what the output should be:
Something-Here
More-Information
This-Is-It
Your-Company
The following one-liner could work for you:
perl -pe 's{/.*}{}' file.txt
Explanation:
Switches:
-p: Creates a while(<>){...; print} loop for each “line” in your input file.
-e: Tells perl to execute the code on command line.
Code:
s{/.*}{}: Remove all characters after the first forward slash from the line
This is usually done with sed:
sed 's|/.*||' file.txt > newfile.txt
This question already has answers here:
Shell variables in sed script [duplicate]
(5 answers)
Closed 8 years ago.
I want to delete words into a line. For example:
I want to delete one word in this line
And I want to delete 'one' to obtain:
I want to delete word in this line
By passing the word through a variable. So far I have got:
WORD=one ; sed -n 's/"$WORD"//g' file.txt > newfile.txt
But, it doesn't do anything. Why not? And how can I make it work?
WORD=one ; sed -e "s/$WORD//g" file.txt > newfile.txt
the key moment is variable expansion. You have to be careful though because shell variable expansion may be sometimes not what you want. In hard cases you have to do something like this:
EXPANDVAR=one; NOEXPANDVAR=another; sed -e 's/'"$EXPANDVAR"'$NOEXPANDVAR//g' file.txt > newfile.txt
In this case sed will replace (remove) pattern one$NOEXPANDVAR , literally.
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
Changing the delimiter slash (/) to pipe (|) in the substitute command of sed works like below
echo hello | sed 's|hello|world|'
How can I change the delimiter slash (/) to pipe (|) in the sed insert command below?
echo hello | sed '/hello/i world'
I'm not sure what is intended by the command you mentioned:
echo hello | sed '/hello/i world'
However, I presume that you want to perform certain action on lines matching the pattern hello. Lets say you wanted to change the lines matching the pattern hello to world. In order to accomplish that, you can say:
$ echo -e "something\nhello" | sed '\|hello|{s|.*|world|}'
something
world
In order to match lines using a regexp, the following forms can be used:
/regexp/
\%regexp%
where % may be replaced by any other single character (note the preceding \ in the second case).
The manual provides more details on this.
The answer to the question asked is:
echo hello | sed '\|hello|i world'
That is how you would prepend a line before a line matching a path, and avoid Leaning Toothpick Syndrome with the escapes.