LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2010, 01:41 AM   #1
threeonethree
LQ Newbie
 
Registered: Oct 2010
Posts: 24

Rep: Reputation: 0
Regular expression to match lines in a file that do not start with # or a blank space


i have a file like this

# comments
#comments
#comments
bla bla
# comments
#bal bla
bla bla
blank space
blank space
bla bla
end of file

i want to grep lines which do not start with # or a blank space. like

bla bla
bla bla

how do i do this?

i tried grep --invert-match '^#' which gives lines not starting with # but gives me blank lines too

i tried grep --invert-match '^#|^ ' which will give lines not starting with # OR not starting with blank ( which means any line including ones starting with #

what is the correct answer? thanks
 
Old 12-25-2010, 01:51 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
grep -E -v '(^#)|(^ )' file.txt
 
Old 12-25-2010, 02:14 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
I wasn't sure if 'blank space' meant a line with at least a single space on it or an empty line, so for the second option you could use:
Code:
egrep -v '^(#|$)' file
If you want the spaces option too you can add:
Code:
egrep -v '^(#| |$)' file
 
Old 12-25-2010, 03:00 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hi grail Nice refinement, putting the ^ before the list of expressions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed regular expression match everything up to a certain character bhepdogg Programming 3 05-28-2009 02:59 PM
PERL:how to find a blank line in a file--regular expression littletransformer Programming 4 03-27-2008 06:55 PM
Regular expression to match 4 or more alpha characters sixerjman Programming 15 11-27-2006 12:03 AM
Don't match a regular expression dakensta Programming 7 09-21-2006 03:48 AM
perl regular expression a char match richikiki Programming 8 07-19-2006 03:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration