LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-15-2014, 06:57 PM   #1
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Rep: Reputation: Disabled
using egrep to search a file


I, for the life of me cannot figure out how to write an egrep command to display NONEMPTY lines WITH BLANK SPACES in a file.

I have egrep -n '^ $' file

This command displays lines with one blank space, but how do I display lines with any number of blank spaces? (blanks spaces only, no letters, numbers etc...)
 
Old 04-15-2014, 07:40 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,440

Rep: Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508Reputation: 1508
Code:
egrep -n '^ +$' file
   # or
egrep -n '^  *$' file
 
1 members found this post helpful.
Old 04-16-2014, 09:50 AM   #3
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
Code:
egrep -n '^ +$' file
   # or
egrep -n '^  *$' file
Thank you!

So now what expression would you use to find blank space trailing a sentence?
 
Old 04-16-2014, 09:54 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,121

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
i guess that would be any line that ends with a space:
Code:
[schneidz@hyper ~]$ grep " "$ hello-world.c
 printf("hello world");
 
Old 04-16-2014, 10:01 AM   #5
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
i guess that would be any line that ends with a space:
Code:
[schneidz@hyper ~]$ grep " "$ hello-world.c
 printf("hello world");
ahh see, I didn't know you could put $ outside of the parenthesis, so how would you grep to find both leading and trailing blanks together? I know how to do just leading, and no just trailing, just not both together, as when I combine the regular expressions it doesn't output the write lines.
 
Old 04-16-2014, 10:04 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,121

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
i think this would be interpreted as any line that begins with a space, contains 0 or more of any character, then ends with a space:
Code:
[schneidz@hyper ~]$ grep ^" .* "$ hello-world.c
 printf("hello world");
 
1 members found this post helpful.
Old 04-16-2014, 10:11 AM   #7
michaelgg13
LQ Newbie
 
Registered: Apr 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
i think this would be interpreted as any line that begins with a space, contains 0 or more of any character, then ends with a space:
Code:
[schneidz@hyper ~]$ grep ^" .* "$ hello-world.c
 printf("hello world");
Thank you, but this missed my 2 lines that have only one blank space and nothing else, but it picks up my line with a bunch of blank spaces.
 
Old 04-16-2014, 11:12 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,121

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
i dont think a single regex would do so maybe this will work:
Code:
[schneidz@hyper ~]$ egrep "(^ .* $| )" hello-world.c
#include <stdio.h>
 
 printf("hello world");
 
1 members found this post helpful.
  


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
egrep read file? type8code0 Solaris / OpenSolaris 6 03-22-2011 01:23 AM
nice egrep 300 search words in one go? shalomajay LinuxQuestions.org Member Intro 2 03-01-2006 05:31 PM
nice egrep 300 search words in one go? shalomajay Programming 1 03-01-2006 03:21 PM
How to 'apt-cache search' & 'apt-file search' by distribution? davidas Debian 3 04-19-2004 01:56 PM


All times are GMT -5. The time now is 03:50 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration