Shell script to comment and uncomment a line in file

Clash 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?
bash shell-script shell
add a comment |Â
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?
bash shell-script shell
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
add a comment |Â
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?
bash shell-script shell
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?
bash shell-script shell
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
add a comment |Â
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
add a comment |Â
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
1
What dose thecdo?
â ctrl-alt-delor
Jul 5 at 11:38
1
coption to change a complete line
â SivaPrasath
Jul 5 at 11:39
add a comment |Â
up vote
0
down vote
(server=A2; sed -ir -e "s/^$server$/#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
add a comment |Â
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
looks odd, is that a GNUism? I think the standard way to refer to the matched string is&. Also, you'll matchA11too when looking forA1.
â ilkkachu
Jul 5 at 11:52
Might be GNUish, but I sawin many implementations. For me&looks odd When I have1to9, why not...
â RoVo
Jul 5 at 12:07
add a comment |Â
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
1
What dose thecdo?
â ctrl-alt-delor
Jul 5 at 11:38
1
coption to change a complete line
â SivaPrasath
Jul 5 at 11:39
add a comment |Â
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
1
What dose thecdo?
â ctrl-alt-delor
Jul 5 at 11:38
1
coption to change a complete line
â SivaPrasath
Jul 5 at 11:39
add a comment |Â
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
To comment:
server=A2; sed -i "/^$server/ c#$server" file.txt
To uncomment:
server=A2; sed -i "/^#$server/ c$server" file.txt
answered Jul 5 at 11:35
SivaPrasath
3,69811636
3,69811636
1
What dose thecdo?
â ctrl-alt-delor
Jul 5 at 11:38
1
coption to change a complete line
â SivaPrasath
Jul 5 at 11:39
add a comment |Â
1
What dose thecdo?
â ctrl-alt-delor
Jul 5 at 11:38
1
coption 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
add a comment |Â
up vote
0
down vote
(server=A2; sed -ir -e "s/^$server$/#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
add a comment |Â
up vote
0
down vote
(server=A2; sed -ir -e "s/^$server$/#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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
(server=A2; sed -ir -e "s/^$server$/#1/")
(server=A2; sed -ir -e "s/^$server$/#1/")
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
add a comment |Â
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
add a comment |Â
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
looks odd, is that a GNUism? I think the standard way to refer to the matched string is&. Also, you'll matchA11too when looking forA1.
â ilkkachu
Jul 5 at 11:52
Might be GNUish, but I sawin many implementations. For me&looks odd When I have1to9, why not...
â RoVo
Jul 5 at 12:07
add a comment |Â
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
looks odd, is that a GNUism? I think the standard way to refer to the matched string is&. Also, you'll matchA11too when looking forA1.
â ilkkachu
Jul 5 at 11:52
Might be GNUish, but I sawin many implementations. For me&looks odd When I have1to9, why not...
â RoVo
Jul 5 at 12:07
add a comment |Â
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
#!/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
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 matchA11too when looking forA1.
â ilkkachu
Jul 5 at 11:52
Might be GNUish, but I sawin many implementations. For me&looks odd When I have1to9, why not...
â RoVo
Jul 5 at 12:07
add a comment |Â
looks odd, is that a GNUism? I think the standard way to refer to the matched string is&. Also, you'll matchA11too when looking forA1.
â ilkkachu
Jul 5 at 11:52
Might be GNUish, but I sawin many implementations. For me&looks odd When I have1to9, 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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