Q: I have a large configuration file with a lot of comments and blank lines. When I cat the file I can only see the last seven lines. There are only about sixteen lines that are not commented out or blank. How can I use grep (or awk/sed) to print the file to the screen without the blank lines?
A: This is very simple using grep and a regex.
For an example let’s use the following text (LINES NUMBERED FOR CLARITY):
1 This is a file
2 # just for testing
3 With a comment
4
5
6
7 Then some blank lines
8
9 # Then some more comments
10
11 And some more blank lines…
12
13
14
15 Then the end of the file
The first way to do it is basically grep every "character" using the "." regex. A period in regular expressions matches any single character.
$ grep . testfile.txt
This is a file
# just for testing
With a comment
Then some blank lines
# Then some more comments
And some more blank lines…
Then the end of the file
In the example above, we basically told grep to search for ANY character. This will NOT work if the lines are not truly blank (if they contain spaces, tabs or carriage returns).
If you want to remove the comments as well:
$ grep . testfile.txt | grep -v "^#"
This is a file
With a comment
Then some blank lines
And some more blank lines…
Then the end of the file
Here is another way using a different regex.
$ grep -v ^$ testfile.txt
This is a file
# just for testing
With a comment
Then some blank lines
# Then some more comments
And some more blank lines…
Then the end of the file
The caret symbol ^ means the beginning of the line and the dollar sign $ means the end of the line. So this regular expressions means match all lines that have nothing on them since ^ is the beginning of the line and $ is the end of the line and there is nothing in between them.
The -v switch tells grep to skip or don’t print the lines that match the following regex.
We can also use this to remove the comments. Using the ^# regex we can tell grep to skip or not print the lines starting with a #. Remember caret ^ is the beginning of a line, so ^# means match any lines starting with a pound sign.
$ grep -v '^$|^#' testfile.txt
This is a file
With a comment
Then some blank lines
And some more blank lines…
Then the end of the file
There is always more than one way to skin a cat. That saying is definitely true for Linux.
Anyone can feel free to leave a comment and tell us how you would do it.
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
2 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
grep -v ^# testfile.txt | grep -v ^$ testfile.txt
Howdy just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Safari.
I'm not sure if this is a formatting issue or something to
do with internet browser compatibility but I thought I'd post to let you know.
The layout look great though! Hope you get the problem fixed soon. Thanks