LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-25-2009, 06:10 AM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,022

Rep: Reputation: 173Reputation: 173
grep - remove lines which only contain whitespace.


I'm having some trouble getting grep to remove lines from a file which only contain white space characters.

The attached file contains lines which are commented out by having a # at the start, lines which are empty and lines which contain either just some spaces or tab characters. I want to use grep to remove all the aforementioned types of line and end up with lines 2 3 6 13.

I thought that this would work:

grep -ve ^# lines.txt | grep -ve ^$ | grep -ve '^\s*$'

But it doesn't. The first and second greps do what I expect. The first one removes the commented out lines, the second removes the empty lines. The last grep, which I think should remove lines containing only whitespace characters doesn't remove anything and I can't figure out what the correct regexp should be. Anyone know?
Attached Files
File Type: txt lines.txt (211 Bytes, 5 views)
 
Old 04-25-2009, 06:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

egrep -v "^[ ]*$|^#" lines.txt

There is a space and a tab between the square brackets. A tab can be 'cerated' by: ctrl-v then tab

Hope this helps.
 
Old 04-25-2009, 07:02 AM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,022

Original Poster
Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by druuna View Post
egrep -v "^[ ]*$|^#" lines.txt
Whilst I appreciate the suggestion, I really don't like that as a solution because of the invisibility of the tab character. It would be far too easy to forget that it's there, or to lose it in a copy/paste. I tried using

Code:
egrep -v "^[ \t]*$|^#" lines.txt
But that doesn't work.
 
Old 04-25-2009, 07:26 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi again,

I don't get the objection:

1) If you use this as a oneliner you type as you go and the oneliner is forgotten after you use it (getting it back from your history is a possibility).

2) If this is used in a script: A comment above the command should solve the forgetting part.

Anyway, here's another solution (but more typing... ):

egrep -v "^[[:space:]]*$|^#" lines.txt
 
Old 04-25-2009, 09:41 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,022

Original Poster
Rep: Reputation: 173Reputation: 173
That's great, thanks a lot.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove whitespace in file and directory names using bash TheFutonEng Programming 14 02-26-2012 12:35 PM
remove multiple files with whitespace coldy78 Linux - General 1 12-10-2007 02:23 AM
remove whitespace at end of file FunkyRes Programming 2 10-05-2004 12:31 AM
Grep lines gsibble Linux - General 1 04-12-2004 01:30 AM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM


All times are GMT -5. The time now is 05:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration