Linux, Print all lines in a file, NOT starting with
Solution 1
Use the -v
option of grep
to negate the condition:
grep -v '^#' file
Solution 2
You can use the !
operator:
awk '!/^ *#/ { print; }'
This negates the result of the match. I also included lines that start with spaces and then #, but you can tailor the regex how you like.
Solution 3
You could use grep to exclude all lines that begin with #
using the -v
option
grep -v '^#' filename
If you're a fan of sed
:
sed '/^#/d' filename
Solution 4
This would also leave out lines with whitespace before the #
:
awk '$1!~/^#/' file
or
grep -v '^[[:blank:]]*#' file
Solution 5
Here is the grep PCRE way,
grep -P '^(?!#)' file
Dasoren
By Day: IT Support tech By Night: System Admin Also do a bit of coding.
Updated on August 29, 2020Comments
-
Dasoren almost 4 years
I would like to print the contents of a file, but all lines starting with # I want to ignore. I was trying some stuff with grep and awk, but it kept printing the whole file, or just printed the lines starting with #. I you could give me a push in the right way, or a grep/awk command that would print anyline in the file that does not start with #.
-
cmevoli over 11 yearsTwo good examples. But, in the sed example, you don't need the
.*
part. -
Tuxdude over 11 years@cmevoli - yes point taken, since the presence of the # at the beginning of the line is enough for the delete command :)
-
Sigur about 6 yearsWhat about if there are leading blank spaces at beginning of line, before the
#
? -
choroba about 6 years@Sigur:
'^[[:space:]]*#'