grep uncommented and empty lines in linux
I use below command to filter the file for # and empty lines.
But, how can we grep uncommented and blank lines in linux with single grep.
[root@localhost ~]# cat test | grep -v ^# | grep -v ^$
linux grep
add a comment |
I use below command to filter the file for # and empty lines.
But, how can we grep uncommented and blank lines in linux with single grep.
[root@localhost ~]# cat test | grep -v ^# | grep -v ^$
linux grep
add a comment |
I use below command to filter the file for # and empty lines.
But, how can we grep uncommented and blank lines in linux with single grep.
[root@localhost ~]# cat test | grep -v ^# | grep -v ^$
linux grep
I use below command to filter the file for # and empty lines.
But, how can we grep uncommented and blank lines in linux with single grep.
[root@localhost ~]# cat test | grep -v ^# | grep -v ^$
linux grep
linux grep
edited Dec 11 at 18:18
asked Dec 11 at 11:45
Ritesh Vishwakarma
163
163
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
For simple cases, my go-to pattern for this is:
$ egrep '^[^#]'
This pattern matches lines which begin with some character OTHER than a pound sign.
If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).
If those cases are important to you, this pattern is better:
$ egrep '^[[:blank:]]*[^[:blank:]#]'
For example:
$ cat test
# comment
# spaces then comment
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$ egrep '^[[:blank:]]*[^[:blank:]#]' test
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$
add a comment |
IIUC, you want to show non-blank and non-commented lines. You can do that with -e
using a single grep
command:
grep -v -e "^#" -e "^$" test
For example, if your test
looks like this:
#a
uncommented line
#comment below blank line
output will be:
$ grep -v -e "^#" -e "^$" test
uncommented line
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
add a comment |
consider the file:
valid config line 1
# Comment
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
If you consider the cases:
- Lines starting with #
- Blank lines
You can use cat file | grep -v '^$|^#'
or cat file | grep -v '^($|#)'
And you will get something like:
valid config line 1
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:
cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'
Obtaining:
valid config line 1
valid config line 2
Blank line between
These two
One space in the line between
These two
Explanation
[[:space:]]*$
matches 0 or more spaces before the end of the line
^(a|b)
matches lines starting witha
orb
, using#
asa
and[[:space:]]*$
asb
will match all lines starting with#
, blank lines, and lines that only have spaces.
/match/d
deletes all the matching lines
;
separates sed commands
s/a/b/g
replacesa
withb
globally. Using#.*
asa
and an emptyb
will remove all the matching comments after a line.
Hope it helps. Regards
add a comment |
Got it!
[root@localhost ~]# cat file | grep -v ^'$|#'
3
Hi Ritesh, watch out! if you have a line likeconfig line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should usegrep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1382606%2fgrep-uncommented-and-empty-lines-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
For simple cases, my go-to pattern for this is:
$ egrep '^[^#]'
This pattern matches lines which begin with some character OTHER than a pound sign.
If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).
If those cases are important to you, this pattern is better:
$ egrep '^[[:blank:]]*[^[:blank:]#]'
For example:
$ cat test
# comment
# spaces then comment
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$ egrep '^[[:blank:]]*[^[:blank:]#]' test
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$
add a comment |
For simple cases, my go-to pattern for this is:
$ egrep '^[^#]'
This pattern matches lines which begin with some character OTHER than a pound sign.
If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).
If those cases are important to you, this pattern is better:
$ egrep '^[[:blank:]]*[^[:blank:]#]'
For example:
$ cat test
# comment
# spaces then comment
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$ egrep '^[[:blank:]]*[^[:blank:]#]' test
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$
add a comment |
For simple cases, my go-to pattern for this is:
$ egrep '^[^#]'
This pattern matches lines which begin with some character OTHER than a pound sign.
If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).
If those cases are important to you, this pattern is better:
$ egrep '^[[:blank:]]*[^[:blank:]#]'
For example:
$ cat test
# comment
# spaces then comment
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$ egrep '^[[:blank:]]*[^[:blank:]#]' test
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$
For simple cases, my go-to pattern for this is:
$ egrep '^[^#]'
This pattern matches lines which begin with some character OTHER than a pound sign.
If you extend the definition of 'blank' line to include a line entirely of whitespace, then the pattern fails, as it will match such a line. The pattern also fails if you allow arbitrary whitespace before the pound sign in a comment line (as Apache, bash, and others do).
If those cases are important to you, this pattern is better:
$ egrep '^[[:blank:]]*[^[:blank:]#]'
For example:
$ cat test
# comment
# spaces then comment
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$ egrep '^[[:blank:]]*[^[:blank:]#]' test
config # then comment
before empty line
after empty line
space only on next line
tab only on next line
$
answered Dec 11 at 21:04
Jim L.
1413
1413
add a comment |
add a comment |
IIUC, you want to show non-blank and non-commented lines. You can do that with -e
using a single grep
command:
grep -v -e "^#" -e "^$" test
For example, if your test
looks like this:
#a
uncommented line
#comment below blank line
output will be:
$ grep -v -e "^#" -e "^$" test
uncommented line
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
add a comment |
IIUC, you want to show non-blank and non-commented lines. You can do that with -e
using a single grep
command:
grep -v -e "^#" -e "^$" test
For example, if your test
looks like this:
#a
uncommented line
#comment below blank line
output will be:
$ grep -v -e "^#" -e "^$" test
uncommented line
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
add a comment |
IIUC, you want to show non-blank and non-commented lines. You can do that with -e
using a single grep
command:
grep -v -e "^#" -e "^$" test
For example, if your test
looks like this:
#a
uncommented line
#comment below blank line
output will be:
$ grep -v -e "^#" -e "^$" test
uncommented line
IIUC, you want to show non-blank and non-commented lines. You can do that with -e
using a single grep
command:
grep -v -e "^#" -e "^$" test
For example, if your test
looks like this:
#a
uncommented line
#comment below blank line
output will be:
$ grep -v -e "^#" -e "^$" test
uncommented line
answered Dec 11 at 11:53
Arkadiusz Drabczyk
1,571711
1,571711
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
add a comment |
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
thanks for your response, but i am looking to do it in single go. like below(though it doesn't work) cat test | grep -v ^[$,#]
– Ritesh Vishwakarma
Dec 11 at 11:56
add a comment |
consider the file:
valid config line 1
# Comment
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
If you consider the cases:
- Lines starting with #
- Blank lines
You can use cat file | grep -v '^$|^#'
or cat file | grep -v '^($|#)'
And you will get something like:
valid config line 1
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:
cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'
Obtaining:
valid config line 1
valid config line 2
Blank line between
These two
One space in the line between
These two
Explanation
[[:space:]]*$
matches 0 or more spaces before the end of the line
^(a|b)
matches lines starting witha
orb
, using#
asa
and[[:space:]]*$
asb
will match all lines starting with#
, blank lines, and lines that only have spaces.
/match/d
deletes all the matching lines
;
separates sed commands
s/a/b/g
replacesa
withb
globally. Using#.*
asa
and an emptyb
will remove all the matching comments after a line.
Hope it helps. Regards
add a comment |
consider the file:
valid config line 1
# Comment
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
If you consider the cases:
- Lines starting with #
- Blank lines
You can use cat file | grep -v '^$|^#'
or cat file | grep -v '^($|#)'
And you will get something like:
valid config line 1
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:
cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'
Obtaining:
valid config line 1
valid config line 2
Blank line between
These two
One space in the line between
These two
Explanation
[[:space:]]*$
matches 0 or more spaces before the end of the line
^(a|b)
matches lines starting witha
orb
, using#
asa
and[[:space:]]*$
asb
will match all lines starting with#
, blank lines, and lines that only have spaces.
/match/d
deletes all the matching lines
;
separates sed commands
s/a/b/g
replacesa
withb
globally. Using#.*
asa
and an emptyb
will remove all the matching comments after a line.
Hope it helps. Regards
add a comment |
consider the file:
valid config line 1
# Comment
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
If you consider the cases:
- Lines starting with #
- Blank lines
You can use cat file | grep -v '^$|^#'
or cat file | grep -v '^($|#)'
And you will get something like:
valid config line 1
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:
cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'
Obtaining:
valid config line 1
valid config line 2
Blank line between
These two
One space in the line between
These two
Explanation
[[:space:]]*$
matches 0 or more spaces before the end of the line
^(a|b)
matches lines starting witha
orb
, using#
asa
and[[:space:]]*$
asb
will match all lines starting with#
, blank lines, and lines that only have spaces.
/match/d
deletes all the matching lines
;
separates sed commands
s/a/b/g
replacesa
withb
globally. Using#.*
asa
and an emptyb
will remove all the matching comments after a line.
Hope it helps. Regards
consider the file:
valid config line 1
# Comment
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
If you consider the cases:
- Lines starting with #
- Blank lines
You can use cat file | grep -v '^$|^#'
or cat file | grep -v '^($|#)'
And you will get something like:
valid config line 1
valid config line 2 # Comment
Blank line between
These two
One space in the line between
These two
However, I would consider also lines that start with a config line and then have an inline comment (supported in several config files), lines that are not blank but have only spaces, for this in a single command, I would use sed:
cat file | sed '/^(#|[[:space:]]*$)/d;s/#.*//g'
Obtaining:
valid config line 1
valid config line 2
Blank line between
These two
One space in the line between
These two
Explanation
[[:space:]]*$
matches 0 or more spaces before the end of the line
^(a|b)
matches lines starting witha
orb
, using#
asa
and[[:space:]]*$
asb
will match all lines starting with#
, blank lines, and lines that only have spaces.
/match/d
deletes all the matching lines
;
separates sed commands
s/a/b/g
replacesa
withb
globally. Using#.*
asa
and an emptyb
will remove all the matching comments after a line.
Hope it helps. Regards
answered Dec 11 at 17:51
Jorge Valentini
30229
30229
add a comment |
add a comment |
Got it!
[root@localhost ~]# cat file | grep -v ^'$|#'
3
Hi Ritesh, watch out! if you have a line likeconfig line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should usegrep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
add a comment |
Got it!
[root@localhost ~]# cat file | grep -v ^'$|#'
3
Hi Ritesh, watch out! if you have a line likeconfig line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should usegrep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
add a comment |
Got it!
[root@localhost ~]# cat file | grep -v ^'$|#'
Got it!
[root@localhost ~]# cat file | grep -v ^'$|#'
answered Dec 11 at 12:03
Ritesh Vishwakarma
163
163
3
Hi Ritesh, watch out! if you have a line likeconfig line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should usegrep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
add a comment |
3
Hi Ritesh, watch out! if you have a line likeconfig line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should usegrep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
3
3
Hi Ritesh, watch out! if you have a line like
config line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
Hi Ritesh, watch out! if you have a line like
config line # Comment
you will be taking it out with this. If all of your comments are starting with # as the first char you should use grep -v '^$|^#'
– Jorge Valentini
Dec 11 at 16:32
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1382606%2fgrep-uncommented-and-empty-lines-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown