0

Consider the file test.txt:

#include "foo.h"
#include "bar.h"
#include "baz.h"

using GNU sed version 4.2.1 (on Ubuntu 10.04.4 LTS), I can extract foo.h, bar.h and baz.h with:

SHELL$) sed -n -e 's:^\s*\#include\s*"\(.*\)".*:\1:p' test.txt
foo.h
bar.h
baz.h

using BSD sed (on Mac OS X lion), and modifying the above command, I can extract foo.h, bar.h and baz.h, but with double quotes:

SHELL) sed -n -e 's:^\s*\#include\s*\(.*\).*:\1:p' test.txt
 "foo.h"
 "bar.h"
 "bar.h"

How can to extract names without the quotes with BSD sed? The output of theses commands are empty:

SHELL) sed -n -e 's:^\s*\#include\s*"\(.*\)".*:\1:p' test.txt
SHELL) sed -n -e 's:^\s*\#include\s*\"\(.*\)\".*:\1:p' test.txt
user744629
  • 1,961
  • 1
  • 18
  • 27
  • For a portable workaround, just remove double quotes from input (`tr '"' ' '`) before applying your sed command. – mouviciel May 29 '12 at 07:45

1 Answers1

2

BSD sed (unsurprisingly, really) doesn't support the \s Perlism -- it is interpreted as just a literal s. Try this instead;

 sed -n -e 's!^[[:space:]]*\#include[[:space:]]*"\(.*\)".*!\1!p' test.txt

The character class [[:space:]] should work in all POSIX regex implementations. (Other seds may or may not want backslashes before grouping parentheses.)

tripleee
  • 175,061
  • 34
  • 275
  • 318