LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using egrep to search a file (https://www.linuxquestions.org/questions/linux-newbie-8/using-egrep-to-search-a-file-4175501876/)

michaelgg13 04-15-2014 06:57 PM

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...)

rknichols 04-15-2014 07:40 PM

Code:

egrep -n '^ +$' file
  # or
egrep -n '^  *$' file


michaelgg13 04-16-2014 09:50 AM

Quote:

Originally Posted by rknichols (Post 5153534)
Code:

egrep -n '^ +$' file
  # or
egrep -n '^  *$' file


Thank you!

So now what expression would you use to find blank space trailing a sentence?

schneidz 04-16-2014 09:54 AM

i guess that would be any line that ends with a space:
Code:

[schneidz@hyper ~]$ grep " "$ hello-world.c
 printf("hello world");


michaelgg13 04-16-2014 10:01 AM

Quote:

Originally Posted by schneidz (Post 5153909)
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.

schneidz 04-16-2014 10:04 AM

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");


michaelgg13 04-16-2014 10:11 AM

Quote:

Originally Posted by schneidz (Post 5153914)
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.

schneidz 04-16-2014 11:12 AM

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");



All times are GMT -5. The time now is 12:47 AM.