Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

egrep and tab

41 views
Skip to first unread message

James

unread,
Sep 6, 2016, 1:21:40 PM9/6/16
to
Need your wisdom. I wish to grep lines which begin with a space or a tab.
What is the best way (using egrep)?
Is there a way to grep non-words?

TIA
James

Janis Papanagnou

unread,
Sep 6, 2016, 1:30:06 PM9/6/16
to
On 06.09.2016 19:21, James wrote:
> Need your wisdom. I wish to grep lines which begin with a space or a tab.
> What is the best way (using egrep)?

grep '^[[:space:]]'


Janis

Geoff Clare

unread,
Sep 7, 2016, 8:41:06 AM9/7/16
to
Janis Papanagnou wrote:

> On 06.09.2016 19:21, James wrote:
>> Need your wisdom. I wish to grep lines which begin with a space or a tab.
>> What is the best way (using egrep)?
>
> grep '^[[:space:]]'

That will match all whitespace characters, not just space and tab.

Which might be what the OP wants, but to match just space and tab:

grep '^[[:blank:]]'

or:

grep '^[ ]'

(where there is a space and an actual tab between the brackets; in
shells which do filename completion when you press tab you can usually
get a literal tab by preceding it with Ctrl-V).

--
Geoff Clare <net...@gclare.org.uk>

Janis Papanagnou

unread,
Sep 7, 2016, 9:21:26 AM9/7/16
to
On 07.09.2016 14:28, Geoff Clare wrote:
> Janis Papanagnou wrote:
>
>> On 06.09.2016 19:21, James wrote:
>>> Need your wisdom. I wish to grep lines which begin with a space or a tab.
>>> What is the best way (using egrep)?
>>
>> grep '^[[:space:]]'
>
> That will match all whitespace characters, not just space and tab.

I've ruled out \n and \r because grep is operating only on line content.
But you are right, [[:blank:]] is preferable - if only to not run into
unexpected conditions, say with contained \v, or \r on a Unix'y system.

Janis

lawren...@gmail.com

unread,
Sep 15, 2016, 12:39:56 AM9/15/16
to
On Thursday, September 8, 2016 at 12:41:06 AM UTC+12, Geoff Clare wrote:
> grep '^[ ]'
>
> (where there is a space and an actual tab between the brackets ...

Bash allows $'^[ \t]', which is nice.

Janis Papanagnou

unread,
Sep 15, 2016, 3:21:19 AM9/15/16
to
Not only Bash. Ksh had this featured documented already more than two
decades ago as "ANSI C Strings". Bash borrowed many of Ksh's features.
Zsh has it as well. But you should be aware that it's non-standard.

Janis

0 new messages