Jump to content

linux command sed


ajoo
Go to solution Solved by kicken,

Recommended Posts

Hi all !

 

Can someone please explain what the following command does:-

sudo sed -i s,scotchbox.local,$DOMAIN,g /etc/apache2/sites-available/$DOMAIN.conf
sudo sed -i s,/var/www/public,/var/www/$DOMAIN/public,g /etc/apache2/sites-available/$DOMAIN.conf

I can across these commands in here.

 

I have tried them out in the terminal as a modified example :- sudo sed -i, s,www,the,g test.txt.

where test.txt has 2 lines of some text containing the word 'the' a couple of times. The command seems to do nothing. There is no change in test.txt.

 

I cannot get any example of sed -i, s on google. In fact i cannot get a similar example of sed anywhere on google. But then i believe sed is extremely versatile and has tons of usages. 

 

Grateful for any help.

Thanks all.

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

man sed

 

 

-i[sUFFIX], --in-place[=SUFFIX]

 

edit files in place (makes backup if extension supplied). The default operation mode is to break symbolic and hard links. This can be changed with --follow-symlinks and --copy.

s/regexp/replacement/

 

Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.

So basically it modifies the given files to make a couple replacements.

Link to comment
Share on other sites

I have tried them out in the terminal as a modified example :- sudo sed -i, s,www,the,g test.txt.

where test.txt has 2 lines of some text containing the word 'the' a couple of times. The command seems to do nothing. There is no change in test.txt.

Try putting "www" in it instead.
Link to comment
Share on other sites

Hi kicken and requinix, 

 

Thanks for the reply.

 

@kicken : Yes it was me clear that it was trying to make a replacement of sorts but the command seems not to do anything. 

@ requinix : No this - (with "www") - does not work either. 

 

Thanks.

Link to comment
Share on other sites

@ requinix : No this - (with "www") - does not work either.

root@*:~# cat test.txt
[quote name="ajoo" post="1543020" timestamp="1487487301"]
I have tried them out in the terminal as a modified example :- sudo sed -i, s,www,the,g test.txt.
where test.txt has 2 lines of some text containing the word 'the' a couple of times.The command seems to do nothing.
There is no change in test.txt.
[/quote]
Try putting "www" in it instead.

root@*:~# sed -i s,www,the,g test.txt
root@*:~# cat test.txt
[quote name="ajoo" post="1543020" timestamp="1487487301"]
I have tried them out in the terminal as a modified example :- sudo sed -i, s,the,the,g test.txt.
where test.txt has 2 lines of some text containing the word 'the' a couple of times.The command seems to do nothing.
There is no change in test.txt.
[/quote]
Try putting "the" in it instead.

root@*:~#
Link to comment
Share on other sites

Hi requininx, 

 

In all cases it simply makes an exact copy of the given file. I am using "test.txt" and it creates a "test.txt," file after the command is run but makes no string substitutions.

 

Maybe that's what the -i is there for.

 

 

Ok I got it after some more tries. This works :

sudo sed -i, s,the,abc,g test.txt.

It saves the original file as a copy while actually modifying the orignal file with the string substitutions..

 

Thanks.

Edited by ajoo
Link to comment
Share on other sites

  • Solution

The test.txt, file is not supposed to change, that's the backup in case you messed up. The original test.txt is the one that gets changed.

 

kicken@web1:~$ echo 'Visit www.example.com today!' > test.txt
kicken@web1:~$ sed -i, s,www,the,g test.txt
kicken@web1:~$ cat test.txt
Visit the.example.com today!
kicken@web1:~$ cat test.txt,
Visit www.example.com today!
kicken@web1:~$
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.