This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sed: delete using a different delimiter
I can substitute line in file like this
sed "s|$PATH_WITH_SLASH||" file
but I cannot delete it
sed "|$PATH_WITH_SLASH|d" file
The problem is that | character cannot be used for deletion. Why?
If you use other character as address delimiter, you need to use backslash before the first char. So
/address/
or
\|address|
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:
Combining two sed commands
(2 answers)
Closed 6 months ago.
sed "s/[][,']//g"
I used this, but then I have empty lines getting returned. I know that you can use:
sed '/^[[:space:]]*$/d'
Delete empty lines using sed
However, trying sed "s/[][[:space:]][,']//g" didn't work as well as other stuff I have tried.
sed is a scripting language; combine the two commands to get the effect you seek.
sed "s/[][,']//g;"'/^[[:space:]]*$/d'
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:
Reverse input order with sed
(6 answers)
Closed 4 years ago.
For example, we have:
This is the song that doesn`t end
What sed command will turn it into this?
end doesn`t that song the is This
I've found only how to reverse lines in a file (a.k.a. tac):
sed -n '1!G;h;$p'
This might work for you (GNU sed):
sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file
Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.
Could you please try following and let me know if this helps you.
awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}' Input_file
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 7 years ago.
I have a stumbling block with sed replace (Linux shell)
I need to replace
</test>
to
</test1>
tried
sed -i 's/<\/test>/</test1>/g'
and similar variants -but still no luck...so thanks for any hint to try
Try this:
echo '</test>' | sed 's|</test>|</test1>|'
For what you tried, you need to escape the slash in the replacement string:
sed -i 's/<\/test>/<\/test1>/g'
Or change the regex boundary marker character:
sed -i 's%</test>%</test1>%g'