Simplest way to comment/uncomment certain lines using command line

Clash Royale CLAN TAG#URR8PPP
up vote
30
down vote
favorite
Is there a way to comment/uncomment a shell/config/ruby script using command line?
for example:
$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf
this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).
shell-script text-processing
add a comment |Â
up vote
30
down vote
favorite
Is there a way to comment/uncomment a shell/config/ruby script using command line?
for example:
$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf
this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).
shell-script text-processing
1
There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this usingsed,perl, etc. Specifically the A by terdon!
â slmâ¦
May 9 '14 at 3:24
add a comment |Â
up vote
30
down vote
favorite
up vote
30
down vote
favorite
Is there a way to comment/uncomment a shell/config/ruby script using command line?
for example:
$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf
this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).
shell-script text-processing
Is there a way to comment/uncomment a shell/config/ruby script using command line?
for example:
$ comment 14-18 bla.conf
$ uncomment 14-18 bla.conf
this would add or remove # sign on bla.conf on line 14 to 18. Normally I use sed, but I must know the contents of those lines and then do a find-replace operation, and that would give a wrong result when the there are more than one needle (and we're only want to replace the N-th one).
shell-script text-processing
shell-script text-processing
edited May 9 '14 at 18:02
Braiam
22.8k1972133
22.8k1972133
asked May 9 '14 at 3:01
Kokizzu
2,17663358
2,17663358
1
There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this usingsed,perl, etc. Specifically the A by terdon!
â slmâ¦
May 9 '14 at 3:24
add a comment |Â
1
There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this usingsed,perl, etc. Specifically the A by terdon!
â slmâ¦
May 9 '14 at 3:24
1
1
There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using
sed, perl, etc. Specifically the A by terdon!â slmâ¦
May 9 '14 at 3:24
There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using
sed, perl, etc. Specifically the A by terdon!â slmâ¦
May 9 '14 at 3:24
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
37
down vote
accepted
To comment lines 2 through 4 of bla.conf:
sed -i '2,4 s/^/#/' bla.conf
To make the command that you wanted, just put the above into a shell script called comment:
#!/bin/sh
sed -i "$1"' s/^/#/' "$2"
This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:
comment 2,4 bla.conf
An uncomment command can be created analogously.
Advanced feature
sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:
comment '/foo/,/bar/' bla.conf
BSD (OSX) Systems
With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:
sed -i '' '2,4 s/^/#/' bla.conf
And, replace the command in the script with:
sed -i '' "$1"' s/^/#/' "$2"
1
Are the double'after the-ioption intentional? Didn't need them.
â raphinesse
Mar 26 '15 at 9:50
add a comment |Â
up vote
5
down vote
With GNU sed (to replace the files in place with the -i option):
sed -i '14,18 s/^/#/' bla.conf
sed -i '14,18 s/^##*//' bla.conf
add a comment |Â
up vote
1
down vote
You can use Vim in Ex mode:
ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
ex -sc 'g/^s*#/s/#//' -cx bla.conf
gglobal regexssubstitutexsave and close
add a comment |Â
up vote
0
down vote
You can create a bash_file with functions to reuse it in your projects
#!/bin/bash
# your target file
CONFIG=./config.txt
# comment target
comment()
sed -i '' "s/^$1/#$1/" $CONFIG
# comment target
uncomment()
echo $1
sed -i '' "s/^#$1/$1/" $CONFIG
# Use it so:
uncomment enable_uart
comment arm_freq
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
37
down vote
accepted
To comment lines 2 through 4 of bla.conf:
sed -i '2,4 s/^/#/' bla.conf
To make the command that you wanted, just put the above into a shell script called comment:
#!/bin/sh
sed -i "$1"' s/^/#/' "$2"
This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:
comment 2,4 bla.conf
An uncomment command can be created analogously.
Advanced feature
sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:
comment '/foo/,/bar/' bla.conf
BSD (OSX) Systems
With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:
sed -i '' '2,4 s/^/#/' bla.conf
And, replace the command in the script with:
sed -i '' "$1"' s/^/#/' "$2"
1
Are the double'after the-ioption intentional? Didn't need them.
â raphinesse
Mar 26 '15 at 9:50
add a comment |Â
up vote
37
down vote
accepted
To comment lines 2 through 4 of bla.conf:
sed -i '2,4 s/^/#/' bla.conf
To make the command that you wanted, just put the above into a shell script called comment:
#!/bin/sh
sed -i "$1"' s/^/#/' "$2"
This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:
comment 2,4 bla.conf
An uncomment command can be created analogously.
Advanced feature
sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:
comment '/foo/,/bar/' bla.conf
BSD (OSX) Systems
With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:
sed -i '' '2,4 s/^/#/' bla.conf
And, replace the command in the script with:
sed -i '' "$1"' s/^/#/' "$2"
1
Are the double'after the-ioption intentional? Didn't need them.
â raphinesse
Mar 26 '15 at 9:50
add a comment |Â
up vote
37
down vote
accepted
up vote
37
down vote
accepted
To comment lines 2 through 4 of bla.conf:
sed -i '2,4 s/^/#/' bla.conf
To make the command that you wanted, just put the above into a shell script called comment:
#!/bin/sh
sed -i "$1"' s/^/#/' "$2"
This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:
comment 2,4 bla.conf
An uncomment command can be created analogously.
Advanced feature
sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:
comment '/foo/,/bar/' bla.conf
BSD (OSX) Systems
With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:
sed -i '' '2,4 s/^/#/' bla.conf
And, replace the command in the script with:
sed -i '' "$1"' s/^/#/' "$2"
To comment lines 2 through 4 of bla.conf:
sed -i '2,4 s/^/#/' bla.conf
To make the command that you wanted, just put the above into a shell script called comment:
#!/bin/sh
sed -i "$1"' s/^/#/' "$2"
This script is used the same as yours with the exception that the first and last lines are to be separated by a comma rather than a dash. For example:
comment 2,4 bla.conf
An uncomment command can be created analogously.
Advanced feature
sed's line selection is quite powerful. In addition to specifying first and last lines by number, it is also possible to specify them by a regex. So, if you want to command all lines from the one containing foo to the one containing bar, use:
comment '/foo/,/bar/' bla.conf
BSD (OSX) Systems
With BSD sed, the -i option needs an argument even if it is just an empty string. Thus, for example, replace the top command above with:
sed -i '' '2,4 s/^/#/' bla.conf
And, replace the command in the script with:
sed -i '' "$1"' s/^/#/' "$2"
edited Dec 9 '15 at 20:18
answered May 9 '14 at 3:04
John1024
44.9k4101117
44.9k4101117
1
Are the double'after the-ioption intentional? Didn't need them.
â raphinesse
Mar 26 '15 at 9:50
add a comment |Â
1
Are the double'after the-ioption intentional? Didn't need them.
â raphinesse
Mar 26 '15 at 9:50
1
1
Are the double
' after the -i option intentional? Didn't need them.â raphinesse
Mar 26 '15 at 9:50
Are the double
' after the -i option intentional? Didn't need them.â raphinesse
Mar 26 '15 at 9:50
add a comment |Â
up vote
5
down vote
With GNU sed (to replace the files in place with the -i option):
sed -i '14,18 s/^/#/' bla.conf
sed -i '14,18 s/^##*//' bla.conf
add a comment |Â
up vote
5
down vote
With GNU sed (to replace the files in place with the -i option):
sed -i '14,18 s/^/#/' bla.conf
sed -i '14,18 s/^##*//' bla.conf
add a comment |Â
up vote
5
down vote
up vote
5
down vote
With GNU sed (to replace the files in place with the -i option):
sed -i '14,18 s/^/#/' bla.conf
sed -i '14,18 s/^##*//' bla.conf
With GNU sed (to replace the files in place with the -i option):
sed -i '14,18 s/^/#/' bla.conf
sed -i '14,18 s/^##*//' bla.conf
answered May 9 '14 at 3:04
Gilles
517k12210311558
517k12210311558
add a comment |Â
add a comment |Â
up vote
1
down vote
You can use Vim in Ex mode:
ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
ex -sc 'g/^s*#/s/#//' -cx bla.conf
gglobal regexssubstitutexsave and close
add a comment |Â
up vote
1
down vote
You can use Vim in Ex mode:
ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
ex -sc 'g/^s*#/s/#//' -cx bla.conf
gglobal regexssubstitutexsave and close
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use Vim in Ex mode:
ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
ex -sc 'g/^s*#/s/#//' -cx bla.conf
gglobal regexssubstitutexsave and close
You can use Vim in Ex mode:
ex -sc 'g/^s*[^#]/s/^/#/' -cx bla.conf
ex -sc 'g/^s*#/s/#//' -cx bla.conf
gglobal regexssubstitutexsave and close
answered Apr 17 '16 at 5:40
Steven Penny
2,41221635
2,41221635
add a comment |Â
add a comment |Â
up vote
0
down vote
You can create a bash_file with functions to reuse it in your projects
#!/bin/bash
# your target file
CONFIG=./config.txt
# comment target
comment()
sed -i '' "s/^$1/#$1/" $CONFIG
# comment target
uncomment()
echo $1
sed -i '' "s/^#$1/$1/" $CONFIG
# Use it so:
uncomment enable_uart
comment arm_freq
add a comment |Â
up vote
0
down vote
You can create a bash_file with functions to reuse it in your projects
#!/bin/bash
# your target file
CONFIG=./config.txt
# comment target
comment()
sed -i '' "s/^$1/#$1/" $CONFIG
# comment target
uncomment()
echo $1
sed -i '' "s/^#$1/$1/" $CONFIG
# Use it so:
uncomment enable_uart
comment arm_freq
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can create a bash_file with functions to reuse it in your projects
#!/bin/bash
# your target file
CONFIG=./config.txt
# comment target
comment()
sed -i '' "s/^$1/#$1/" $CONFIG
# comment target
uncomment()
echo $1
sed -i '' "s/^#$1/$1/" $CONFIG
# Use it so:
uncomment enable_uart
comment arm_freq
You can create a bash_file with functions to reuse it in your projects
#!/bin/bash
# your target file
CONFIG=./config.txt
# comment target
comment()
sed -i '' "s/^$1/#$1/" $CONFIG
# comment target
uncomment()
echo $1
sed -i '' "s/^#$1/$1/" $CONFIG
# Use it so:
uncomment enable_uart
comment arm_freq
answered 1 min ago
Jan
1031
1031
add a comment |Â
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%2f128593%2fsimplest-way-to-comment-uncomment-certain-lines-using-command-line%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
1
There is an A to this Q titled: Uncommenting multiple lines of code, specified by line numbers, using vi or vim which shows many methods for doing this using
sed,perl, etc. Specifically the A by terdon!â slmâ¦
May 9 '14 at 3:24