Shell script to comment and uncomment a line in file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a file where listed all server for exemple lserver :



$ cat lserver
A1
A2
A3


I want to create a shell script to comment any server from lserver
exemple :



$ stopm.sh A2
$ cat lserver
A1
#A2
A3


and uncomment a server from lserver :
exemple



$ startm.sh A2
$ cat lserver
A1
A2
A3


Any suggestion?







share|improve this question





















  • Welcome to U&L, we are not a script writing service, please, edit your question with what you have tried so far.
    – Archemar
    Jul 5 at 11:13






  • 1




    Note don't give script names extensions. This is a filthy habit that has come from MS-dos. It leaks implementation detail. That is it tells the use what language it is written in. This will make it harder if you need to re-write the program, in another language, because all scripts/programs that use it will need to be edited.
    – ctrl-alt-delor
    Jul 5 at 11:18














up vote
1
down vote

favorite












I have a file where listed all server for exemple lserver :



$ cat lserver
A1
A2
A3


I want to create a shell script to comment any server from lserver
exemple :



$ stopm.sh A2
$ cat lserver
A1
#A2
A3


and uncomment a server from lserver :
exemple



$ startm.sh A2
$ cat lserver
A1
A2
A3


Any suggestion?







share|improve this question





















  • Welcome to U&L, we are not a script writing service, please, edit your question with what you have tried so far.
    – Archemar
    Jul 5 at 11:13






  • 1




    Note don't give script names extensions. This is a filthy habit that has come from MS-dos. It leaks implementation detail. That is it tells the use what language it is written in. This will make it harder if you need to re-write the program, in another language, because all scripts/programs that use it will need to be edited.
    – ctrl-alt-delor
    Jul 5 at 11:18












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a file where listed all server for exemple lserver :



$ cat lserver
A1
A2
A3


I want to create a shell script to comment any server from lserver
exemple :



$ stopm.sh A2
$ cat lserver
A1
#A2
A3


and uncomment a server from lserver :
exemple



$ startm.sh A2
$ cat lserver
A1
A2
A3


Any suggestion?







share|improve this question













I have a file where listed all server for exemple lserver :



$ cat lserver
A1
A2
A3


I want to create a shell script to comment any server from lserver
exemple :



$ stopm.sh A2
$ cat lserver
A1
#A2
A3


and uncomment a server from lserver :
exemple



$ startm.sh A2
$ cat lserver
A1
A2
A3


Any suggestion?









share|improve this question












share|improve this question




share|improve this question








edited Jul 5 at 11:34
























asked Jul 5 at 10:49









BOUABANE Raed

62




62











  • Welcome to U&L, we are not a script writing service, please, edit your question with what you have tried so far.
    – Archemar
    Jul 5 at 11:13






  • 1




    Note don't give script names extensions. This is a filthy habit that has come from MS-dos. It leaks implementation detail. That is it tells the use what language it is written in. This will make it harder if you need to re-write the program, in another language, because all scripts/programs that use it will need to be edited.
    – ctrl-alt-delor
    Jul 5 at 11:18
















  • Welcome to U&L, we are not a script writing service, please, edit your question with what you have tried so far.
    – Archemar
    Jul 5 at 11:13






  • 1




    Note don't give script names extensions. This is a filthy habit that has come from MS-dos. It leaks implementation detail. That is it tells the use what language it is written in. This will make it harder if you need to re-write the program, in another language, because all scripts/programs that use it will need to be edited.
    – ctrl-alt-delor
    Jul 5 at 11:18















Welcome to U&L, we are not a script writing service, please, edit your question with what you have tried so far.
– Archemar
Jul 5 at 11:13




Welcome to U&L, we are not a script writing service, please, edit your question with what you have tried so far.
– Archemar
Jul 5 at 11:13




1




1




Note don't give script names extensions. This is a filthy habit that has come from MS-dos. It leaks implementation detail. That is it tells the use what language it is written in. This will make it harder if you need to re-write the program, in another language, because all scripts/programs that use it will need to be edited.
– ctrl-alt-delor
Jul 5 at 11:18




Note don't give script names extensions. This is a filthy habit that has come from MS-dos. It leaks implementation detail. That is it tells the use what language it is written in. This will make it harder if you need to re-write the program, in another language, because all scripts/programs that use it will need to be edited.
– ctrl-alt-delor
Jul 5 at 11:18










3 Answers
3






active

oldest

votes

















up vote
2
down vote













To comment:



server=A2; sed -i "/^$server/ c#$server" file.txt



To uncomment:



server=A2; sed -i "/^#$server/ c$server" file.txt






share|improve this answer

















  • 1




    What dose the c do?
    – ctrl-alt-delor
    Jul 5 at 11:38






  • 1




    c option to change a complete line
    – SivaPrasath
    Jul 5 at 11:39

















up vote
0
down vote













(server=A2; sed -ir -e "s/^$server$/#1/")





share|improve this answer



















  • 1




    I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
    – Kalavan
    Jul 5 at 11:28

















up vote
0
down vote













#!/bin/bash
server="$1"
case $2 in
start)
sed -i "s/^#($server)$/1/" lserver.txt
;;
stop)
sed -i "s/^$server$/#&/" lserver.txt
;;
esac


Save as server_ctrl, then run chmod u+x server_ctrl.




Usage:



 ./server_ctrl A2 start|stop





share|improve this answer























  • looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
    – ilkkachu
    Jul 5 at 11:52










  • Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
    – RoVo
    Jul 5 at 12:07











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453585%2fshell-script-to-comment-and-uncomment-a-line-in-file%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













To comment:



server=A2; sed -i "/^$server/ c#$server" file.txt



To uncomment:



server=A2; sed -i "/^#$server/ c$server" file.txt






share|improve this answer

















  • 1




    What dose the c do?
    – ctrl-alt-delor
    Jul 5 at 11:38






  • 1




    c option to change a complete line
    – SivaPrasath
    Jul 5 at 11:39














up vote
2
down vote













To comment:



server=A2; sed -i "/^$server/ c#$server" file.txt



To uncomment:



server=A2; sed -i "/^#$server/ c$server" file.txt






share|improve this answer

















  • 1




    What dose the c do?
    – ctrl-alt-delor
    Jul 5 at 11:38






  • 1




    c option to change a complete line
    – SivaPrasath
    Jul 5 at 11:39












up vote
2
down vote










up vote
2
down vote









To comment:



server=A2; sed -i "/^$server/ c#$server" file.txt



To uncomment:



server=A2; sed -i "/^#$server/ c$server" file.txt






share|improve this answer













To comment:



server=A2; sed -i "/^$server/ c#$server" file.txt



To uncomment:



server=A2; sed -i "/^#$server/ c$server" file.txt







share|improve this answer













share|improve this answer



share|improve this answer











answered Jul 5 at 11:35









SivaPrasath

3,69811636




3,69811636







  • 1




    What dose the c do?
    – ctrl-alt-delor
    Jul 5 at 11:38






  • 1




    c option to change a complete line
    – SivaPrasath
    Jul 5 at 11:39












  • 1




    What dose the c do?
    – ctrl-alt-delor
    Jul 5 at 11:38






  • 1




    c option to change a complete line
    – SivaPrasath
    Jul 5 at 11:39







1




1




What dose the c do?
– ctrl-alt-delor
Jul 5 at 11:38




What dose the c do?
– ctrl-alt-delor
Jul 5 at 11:38




1




1




c option to change a complete line
– SivaPrasath
Jul 5 at 11:39




c option to change a complete line
– SivaPrasath
Jul 5 at 11:39












up vote
0
down vote













(server=A2; sed -ir -e "s/^$server$/#1/")





share|improve this answer



















  • 1




    I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
    – Kalavan
    Jul 5 at 11:28














up vote
0
down vote













(server=A2; sed -ir -e "s/^$server$/#1/")





share|improve this answer



















  • 1




    I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
    – Kalavan
    Jul 5 at 11:28












up vote
0
down vote










up vote
0
down vote









(server=A2; sed -ir -e "s/^$server$/#1/")





share|improve this answer















(server=A2; sed -ir -e "s/^$server$/#1/")






share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 5 at 11:35


























answered Jul 5 at 11:15









ctrl-alt-delor

8,68331947




8,68331947







  • 1




    I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
    – Kalavan
    Jul 5 at 11:28












  • 1




    I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
    – Kalavan
    Jul 5 at 11:28







1




1




I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
– Kalavan
Jul 5 at 11:28




I would make sure that pattern starts from beginning of line to be sure I will match correct server and won't add another '#' sign there. Same goes to the end of pattern as there should be a difference between A1 and A13, right?
– Kalavan
Jul 5 at 11:28










up vote
0
down vote













#!/bin/bash
server="$1"
case $2 in
start)
sed -i "s/^#($server)$/1/" lserver.txt
;;
stop)
sed -i "s/^$server$/#&/" lserver.txt
;;
esac


Save as server_ctrl, then run chmod u+x server_ctrl.




Usage:



 ./server_ctrl A2 start|stop





share|improve this answer























  • looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
    – ilkkachu
    Jul 5 at 11:52










  • Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
    – RoVo
    Jul 5 at 12:07















up vote
0
down vote













#!/bin/bash
server="$1"
case $2 in
start)
sed -i "s/^#($server)$/1/" lserver.txt
;;
stop)
sed -i "s/^$server$/#&/" lserver.txt
;;
esac


Save as server_ctrl, then run chmod u+x server_ctrl.




Usage:



 ./server_ctrl A2 start|stop





share|improve this answer























  • looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
    – ilkkachu
    Jul 5 at 11:52










  • Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
    – RoVo
    Jul 5 at 12:07













up vote
0
down vote










up vote
0
down vote









#!/bin/bash
server="$1"
case $2 in
start)
sed -i "s/^#($server)$/1/" lserver.txt
;;
stop)
sed -i "s/^$server$/#&/" lserver.txt
;;
esac


Save as server_ctrl, then run chmod u+x server_ctrl.




Usage:



 ./server_ctrl A2 start|stop





share|improve this answer















#!/bin/bash
server="$1"
case $2 in
start)
sed -i "s/^#($server)$/1/" lserver.txt
;;
stop)
sed -i "s/^$server$/#&/" lserver.txt
;;
esac


Save as server_ctrl, then run chmod u+x server_ctrl.




Usage:



 ./server_ctrl A2 start|stop






share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 5 at 12:04


























answered Jul 5 at 11:36









RoVo

83219




83219











  • looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
    – ilkkachu
    Jul 5 at 11:52










  • Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
    – RoVo
    Jul 5 at 12:07

















  • looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
    – ilkkachu
    Jul 5 at 11:52










  • Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
    – RoVo
    Jul 5 at 12:07
















looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
– ilkkachu
Jul 5 at 11:52




looks odd, is that a GNUism? I think the standard way to refer to the matched string is &. Also, you'll match A11 too when looking for A1.
– ilkkachu
Jul 5 at 11:52












Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
– RoVo
Jul 5 at 12:07





Might be GNUish, but I saw in many implementations. For me & looks odd When I have 1 to 9 , why not ...
– RoVo
Jul 5 at 12:07













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453585%2fshell-script-to-comment-and-uncomment-a-line-in-file%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)