1

Consider the following typescript:

$ freebsd-version 10.0-RELEASE-p5 $ echo ' found' | sed -n '/[[:blank:]]\+/p' $ echo ' found' | grep '[[:blank:]]\+' found 

When I do this on Arch Linux with GNU sed:

$ echo ' found' | sed -n '/[[:blank:]]\+/p' found $ echo ' found' | grep '[[:blank:]]\+' found 

Am I doing it wrong? Why doesn't it work on FreeBSD? How to make it work?

1
  • 2
    Did you read your sed manpage? I'd try removing the backslash Commented Sep 30, 2015 at 20:13

2 Answers 2

4

With BSD sed you have to turn on ERE's with the E option.

echo ' found' | sed -nE '/[[:blank:]]+/p' 
0

tl;dr see fd0's solution

I did look in the man page before asking, but failed to notice the relevant info, since I figured it doesn't differ significantly from that of Linuxes. But,

$ man sed ... -E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats. $ man re_format ... Obsolete (“basic”) regular expressions differ in several respects. ‘|’ is an ordinary character and there is no equivalent for its functional‐ ity. ‘+’ and ‘?’ are ordinary characters, and their functionality can be expressed using bounds (‘{1,}’ or ‘{0,1}’ respectively). Also note that ‘x+’ in modern REs is equivalent to ‘xx*’. The delimiters for bounds are ‘\{’ and ‘\}’, with ‘{’ and ‘}’ by themselves ordinary characters. 

And I did try to use {1,}, I just forgot to escape curly brackets. So surely fd0's solution is generally the best way possible. But others would be:

$ echo ' found' | sed -n '/[[:blank:]]\{1,\}/p' found $ echo ' found' | sed -n '/[[:blank:]][[:blank:]]*/p' found 

However, not sure what they meant by

Also note that ‘x+’ in modern REs is equivalent to ‘xx*’

Meaning, what it was like in nonmodern ones.

Also, grep's manpage didn't mention re_format entry, which apparently means it has its own implementation of regular expressions. For that matter, it was GNU grep, as opposed to sed.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.