ajoo Posted February 19, 2017 Share Posted February 19, 2017 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. Quote Link to comment Share on other sites More sharing options...
kicken Posted February 19, 2017 Share Posted February 19, 2017 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 19, 2017 Share Posted February 19, 2017 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. Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 20, 2017 Author Share Posted February 20, 2017 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 20, 2017 Share Posted February 20, 2017 @ 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@*:~# Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 21, 2017 Author Share Posted February 21, 2017 (edited) 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 February 21, 2017 by ajoo Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted February 21, 2017 Solution Share Posted February 21, 2017 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:~$ Quote Link to comment Share on other sites More sharing options...
ajoo Posted February 21, 2017 Author Share Posted February 21, 2017 hmmm ! that's exact what I said or at least meant to say. In any case you are absolutely right. Those were my observations too. Thanks again ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.