tags:

views:

84

answers:

1

I have a text file where some lines have a character at the beginning and some lines don't. I want to print the text file to screen, excluding the lines that don't have a character at the beginning.

Can I do this with grep?

+2  A: 
"excluding the lines that don't have a character at the beginning" 

is same as

"including the lines that have the character at the beginning"

To get all the lines that start with char s you can do:

grep '^s' filename 

Example:

[23:18:03][/tmp]$ cat test
stack
overflow
testing
sample
[23:21:37][/tmp]$ grep '^s' test # To list lines beginning with s
stack
sample
codaddict
similarly, if you had multiple characters that may be at the beginning, you can use a character class:grep '^[adf]' filenamewould match any lines that start with a, d, or f.
TJ Ellis
What if I don't know which characters at the beginning? Is there a way to specify a blank space?
Phenom
To list only those lines that do not have a leading space you can use: `grep '^[^ ]' test`
codaddict