grep for lines not containing #

I'm trying to grep for lines not containing a #. I'm not getting the syntax right. This is what I have tried so far.

grep -v \\# /home/bob/.config/vlc/vlcrc
grep -v '\\#' /home/bob/.config/vlc/vlcrc
grep -v "\\\\#" /home/bob/.config/vlc/vlcrc

I tried to look at this thread of mine for ideas with no luck.

If your just ignoring the lines with # .. Then try the below..

$ grep -v "#" infile

That didn't work :(.

Jajan's code worked flawless for me. Do you mind posting a helpful output of the error you got? Having 316 posts in this forum, you should be familiar giving detailed answers to solve your problems, thanks.

can you post the output of

 
cat -v /home/bob/.config/vlc/vlcrc
 

My problem is I didn't get an error. I got a ton of blank lines. The file has 4.5k of lines. I'm not sure what the preferred method is to deal with huge files.

Here is 100 of the lines.

# Video filter module (string)
#video-filter=

# Video output filter module (string)
#vout-filter=

# Enable sub-pictures (boolean)
#spu=1

# On Screen Display (boolean)
#osd=1

# Text rendering module (string)
#text-renderer=

# Use subtitle file (string)
#sub-file=

# Autodetect subtitle files (boolean)
#sub-autodetect-file=1

# Subtitle autodetection fuzziness (integer)
#sub-autodetect-fuzzy=3

# Subtitle autodetection paths (string)
#sub-autodetect-path=./Subtitles, ./subtitles

# Force subtitle position (integer)
#sub-margin=0

# Subpictures filter module (string)
#sub-filter=

# Program (integer)
#program=0

# Programs (string)
#programs=

# Audio track (integer)
#audio-track=-1

# Subtitles track (integer)
#sub-track=-1

# Audio language (string)
#audio-language=

# Subtitle language (string)
#sub-language=

# Audio track ID (integer)
#audio-track-id=-1

# Subtitles track ID (integer)
#sub-track-id=-1

# Input repetitions (integer)
#input-repeat=0

# Start time (float)
#start-time=0.000000

# Stop time (float)
#stop-time=0.000000

# Run time (float)
#run-time=0.000000

# Fast seek (boolean)
#input-fast-seek=0

# Playback speed (float)
#rate=1.000000

# Input list (string)
#input-list=

# Input slave (experimental) (string)
#input-slave=

# Bookmarks list for a stream (string)
#bookmarks=

# DVD device (string)
#dvd=/dev/dvd

# VCD device (string)
#vcd=/dev/cdrom

# Audio CD device (string)
#cd-audio=/dev/cdrom

# UDP port (integer)
#server-port=1234

# MTU of the network interface (integer)
#mtu=1400

# Force IPv6 (boolean)
#ipv6=0

Try:

awk 'NF && $1!~/^#/' infile

or

grep -vE '^[[:space:]]*($|#)' infile

--
To only exclude comment lines that have # as the first character (lines that have whitespace before the # are printed), as well as empty lines

awk 'NF && !/^#/' infile

That's better than

Thanks.

I'm no expert with grep or regular expressions in general (started working with them a couple days ago), but wouldn't using this pattern in grep work?

^[^\r\n#]+[\r\n]

I haven't used grep from the terminal, but my text editor can use grep for search, and this matches all lines without # and ignores those with a #.

EDIT:

To match blank lines, change the + to a *

^[^\r\n#]*[\r\n]

Carriage returns (\r) are not part of a regular Unix file and a linefeed (\n) cannot be matched by regular grep, since grep is line based. + is part of an extended regular expression (ERE) so you would need to use grep -E

Ok. That's good to know. I suppose the end of line anchor ($) will work, then.

This seems to work on a tiny test file.

grep -e '^[^#]*$' testhash.txt

EDIT:

This is my file contents:

line1 has some text
line2 is a second line
li#e3 has a hash!
line4 the above line had a hash!
line# has a hash too
line6 - ^ had a hash.. bad line?
#ine7 hashline
line8 lastly the last line
#

line11 single hash and a blank line above

And this is the output:

line1 has some text
line2 is a second line
line4 the above line had a hash!
line6 - ^ had a hash.. bad line?
line8 lastly the last line

line11 single hash and a blank line above

But that would also filter out lines that use the # symbol somewhere on the line either as a partial comment or not as a comment. It also does not filter out empty lines..

To compare:

$ cat infile
# This is a test
echo hello # partial test

echo the line above contains tabs and spaces

echo the line above is empty
  echo "just want to print an #i character" ffsf
$ grep -e '^[^#]*$' infile

echo the line above contains tabs and spaces

echo the line above is empty
$ awk 'NF && $1!~/^#/' infile
echo hello # partial test
echo the line above contains tabs and spaces
echo the line above is empty
  echo "just want to print an #i character" ffsf
$

Ok, as that was my intention, I suppose I was misunderstanding what was desired.