0

I want to create a pattern for sed, which will find out 'type="" For this I tried to use the pattern

type=".*\?"

echo 'aa type="none" stretchChildren="first"' | sed s/'type=".*\?"'/hello/

Above is the sed command which prints

aa hello

Which means it selects 'type="none" stretchChildren="first"' for 'type=".*\?"'

Now below is the grep command using same pattern on same string

 echo 'aa type="none" stretchChildren="first"' | grep -oP 'type=".*?"'

It gives output

type="none"

Don't know what I am missing in sed pattern

Can some one help me out here Output of sed should be

aa hello stretchChildren="first"

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • 2
    Which platform are you on? You're making non-portable assumptions about dialects of `grep` and `sed`. It looks as though you're probably on Linux or using GNU `sed` and `grep`, but it would be as well to say so. – Jonathan Leffler Jun 21 '16 at 05:32
  • 2
    Why not use a simply pattern: `type="[^"]*"` which will work the same in `sed` and `grep` without extra options and does what you seem to want. – Jonathan Leffler Jun 21 '16 at 05:37

1 Answers1

4

sed doesn't have non-greedy pattern matching, so using *? or *\? won't work.

If you want to have the same output as grep then use a grouping without the " - [^"]+ instead of ".*?":

sed -r 's/type="[^"]+"/hello/'

[, ] is a group of characters, ^ is a negation, so [^"] means any character that is not a ".

For OSX use -E instead of -r. (-E also works on latest GNU sed, but it is not documented in --help nor in man sed so I don't recommend it)

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115