Sed Append Line - linux

Does sed have a command to append a line after a matched line? I tried searching around but was a bit confused with the results.
Basiclly I want it to match
#address=/doubleclick.net/127.0.0.1
And add a line below it like
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress
Is this possible?

You can use the a(append) command in sed:
$ sed -i '/^#address=\/doubleclick.net\/127.0.0.1$/a\
> address=/anotherurl/ipaddress' file.txt
Or you can use s(substitute) command:
$ sed -i 's#^#address=/doubleclick.net/127.0.0.1$#&\naddress=/anotherurl/ipaddress#' file.txt
Note: $ and > are bash prompt.

This might work for you:
echo "#address=/doubleclick.net/127.0.0.1"|
sed '\|#address=/doubleclick.net/127.0.0.1|a\address=/anotherurl/ipaddress'
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress
You can use any delimiter you like in an address by prepending a \ i.e. \|...| for the substitute command the \ is not necessary.
If you want a blank line then some text following the match, use:
echo "#address=/doubleclick.net/127.0.0.1"|
sed '\|#address=/doubleclick.net/127.0.0.1|a\\naddress=/anotherurl/ipaddress'
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress

Related

sed removes one string in one file

I'm trying to remove one string in one file by using:
sed -i -e '/example/' test.txt
But I've got the following error:
sed: -e expression #1, char 6: missing command
What is it missing and why?
Thanks!
/example/ is an address which tells sed where to run commands - on a line containing the string example. You didn't specify any commands.
This is a commnand that replaces the string example with an empty string:
's/example//'
try:
sed '/example/d' test.txt
Explanation as suggested by #Nathan Tuggy
sed will search for the given string and will 'D'elete it
user#host:~$ cat test.txt
one
two
three
user#host:~$ sed '/two/d' test.txt
one
three

I am having trouble with Sed

I am trying to use the sed command to replace this line:
charmm.c36a4.20140107.newcali4.fixhcali.grange.b
with:
charmm.20140911.c36a4.3rd.ghost2.model3rd
When I use:
sed -i '/s/firstline/secondline/g'
It doesn't work. I think the periods are messing it up. How do I get around this?
sed uses regular expressions, so . matches any character. If you want to only match the . character itself, tell sed to look for \.
so to change the first line into the second line:
sed -e 's/charmm\.c36a4.20140107\.newcali4\.fixhcali\.grange\.b/charmm.20140911.c36a4.3rd.ghost2.model3rd/g' < filetochange >newfile
Here, I added "g" so it does it globally, ie, if there are several instances on the same line, all will be changed. If you remove the "g", it will only change the first occurence on each line.
It reads from filetochange and writes to newfile
If you do :
sed -i -e 's/charmm\.c36a4.20140107\.newcali4\.fixhcali\.grange\.b/charmm.20140911.c36a4.3rd.ghost2.model3rd/g' filetochange
it will directly do the change in "filetochange" ... but please be careful, a badly written sed -i could mess up the file and make it unusable
The s command follows this syntax:
s/pattern/replacement/
You need to drop the / in front of the sed command.

Sed replace ">" to "/>" bash

I'm newbie and I would like to replace "special" caracters with sed. I have an xml file that it is not well formed and at the end of any data row it finish with ">" I need to scrap it and to do it I need to change ">" with "/>". But when I try:
sed -i s/>//>/g FILE
returns => -bash: //: Is a directory
same with:
sed -i s/>/\/>/g FILE
also with
sed -i s,>,\>,g FILE
Man page doesn't solve this problem.
Does anyone face this issue?
You should escape the slash character if you use it a separator:
sed 's/>/\/>/g'
Or use another character e.g.:
sed 's_>_/>_g'
Note that this will replace all matches in the file, not just the last one
If you want to match only > not preceded by /, you can use this:
sed -r 's_[^/]>_/>_g' file
Would sed -i 's/>$//>/g' FILE work for you?
$ echo "<blah> <blah>" | sed 's/>$/\/>/g'
<blah> <blah/>

Sed Command get text between two string while excluding the two strings using Terminal UNIX

So I have a sed command that looks like this:
sed -n "/DXImageTransform.Microsoft.AlphaImageLoader(src='/,/',sizingMethod='crop');/p"
/Users/ME/Documents/weather/yahooWeather.html > /Users/ME/Documents/weather/out.txt
And it correctly gets the HTML that I want out of the file, however it still has the strings that I used to search for it (i.e. "DXImageTransform.Microsoft.AlphaImageLoader(src='" & "',sizingMethod='crop');" on the beginning and the end of the file. I'd like to remove those two strings. How can I modify my command to do this?
This should work:
sed -n -e "1,/START/d" -e "/END/,$d" -e p file.html
I separated 3 commands there using the -e options but you could put them all together separated by ;.
And this is what they do:
1,/START/d -- deletes a range: from the first line of the file until the line matching the pattern /START/
/END/,$d -- deletes a range: from the line matching the pattern until the end of the file
p -- print the line (these are lines not matched by previous patterns)
UPDATE
If the pattern is on the first line, the above won't work. With GNU sed, you can fix that like this:
sed -n -e "0,/START/d" -e "/END/,$d" -e p file.html
Unfortunately this won't work with BSD sed.

sed -e 's/^M/d' not working

A very common problem, but I am unable to work around it with sed.
I have a script file ( a batch of commands) say myfile.txt to be executed at once to create a list. Now when I am executing a batch operation my command line interface clearly shows its unable to parse the command as a line feed ^M is adding up at end of each line.
I thought sed to be the best way to go about it.I tried:
sed -e 's/^M/d' myfile.txt > myfile1.txt
mv myfile1.txt myfile.txt
It didn't work. I also tried this and it didn't work:
sed -e 's/^M//g' myfile.txt > myfile1.txt
mv myfile1.txt myfile.txt
Then I thought may be sed is taking it as a M character in the beginning of line, and hence no result. So I tried:
sed -e 's/\^M//g' myfile.txt > myfile1.txt
mv myfile1.txt myfile.txt
But no change. Is there a basic mistake I am doing ? Kindly advise as I am bad at sed.
I found a resolution though which was to open the file in vi editor and in command mode execute this:
:set fileformat=unix
:w
But I want it in sed as well.
^M is not literally ^M. Replace ^M with \r. You can use the same representation for tr; these two commands both remove carriage returns:
tr -d '\r' < input.txt > output.txt
sed -e 's/\r//g' input.txt > output.txt
sed -e 's/^M/d' myfile.txt
Has the following meaning [the same for /\^M/ ]: If the first letter of the line is M, then remove the line, else print it and pass to next.. And you have to insert 2 separators /old/new/ in s[earch command].
This may help you.
Late, but here for posterity: sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix
The gist, which answers the above question: to get ^M type CTRL+V followed by CTRL+M i.e. don’t just type the carat symbol and a capital M. It will not work

Resources