Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Using [[:space:]] looks a little non-elegant and, quite frankly, Microsoft-ish for sed match patters. I was trying to extract "Last" out of "First Last" and either of the following worked:

echo "First Last" | sed s/First //
echo "First Last" | sed s/First\s//

What did work is

echo "First Last" | sed s/First[[:space:]]//

I was wondering:

  1. Is there is a more minimalistic notation to match a whitespace in sed?
  2. Does [[:space:]] work on all Linux distros?
share|improve this question
2  
"Microsoft-ish"? That's a classic BRE character class, it's as POSIX as it gets. –  terdon Feb 6 at 17:36
    
i just find \s to be more minimalist –  amphibient Feb 6 at 17:38
    
That's specific to PCREs, you won't find it anywhere else. –  terdon Feb 6 at 17:42
    
it works with Java, Perl and Python RegEx patterns. –  amphibient Feb 6 at 17:43
2  
By the way, the problem with echo "First Last" | sed s/First // is that you are passing two arguments to sed. You need to quote the argument: echo "First Last" | sed "s/First //" or echo "First Last" | sed s/First\ // –  rici Feb 6 at 18:11
show 2 more comments

1 Answer

up vote 5 down vote accepted

Yes, [:space:] should be recognized by all sed editions, it is part of Basic Regular Expressions as defined by POSIX.

The \s notation is from Perl Compatible Regular Extensions which are implemented in many programs (grep with -P for example) and languages (perl, php, java, javascript, python...). Neither one of these regex syntaxes has anything to do with Microsoft!

If you want PCRE syntax, why not use Perl? Both of these work:

echo "First Last" | perl -pe 's/First\s//'
echo "First Last" | perl -pe 's/First *//'

The -p flag means "print every line" after performing whatever script was passed with -e on it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.