1

I'm writing a shell script to modify a file and I have a line something like this in it:

sed s/here \(.*\n\)/gone \1/g

Unfortunately, the search seems to match the longest string (i.e., it goes all the way to the last \n -- thus giving me just one replacement) but I want it to match only up to the first \n it finds (so I can get replacements on every line).

Is this possible?

Thanks for your help!

user825067
  • 53
  • 1
  • 5
  • 1
    Please provide example file to process with your command, expected and actual output. – vbo Jan 22 '14 at 19:20
  • Unfortunately I'm away from that computer now for the next 6 hours. But the basic idea is this: if str = "dadbcbcbc" and I search in str using: a.*b it seems to match adbcbcb, but I want it to match just adb. – user825067 Jan 22 '14 at 20:00

1 Answers1

0

Looks like you want the feature called non-greedy (or lazy) match. Unfortunately sed does not provide such feature. To emulate it you need to search for anything except separator match until separator match. Like this:

s/here \([^\n]*\n\)/gone \1/g
Community
  • 1
  • 1
vbo
  • 13,583
  • 1
  • 25
  • 33
  • Great answer. Thanks! [^\n]* looks perfect. I'll test it tonight and no news from me means good news. :) – user825067 Jan 22 '14 at 22:07
  • @user825067 Did you try my suggestion? – vbo Jan 23 '14 at 17:05
  • Yes and it worked fine. Thank you very much. (No news was meant to mean that everything was fine.) But greedy matching seems to pose an interesting question: suppose str = "aabcdbc" and I search str for "a.*b.*d". Is this search going to fail because a.*b matches abcdb? – user825067 Jan 23 '14 at 20:44
  • Try this out: `echo 'aabcdbc' | sed 's/a.*b.*d//'`. Looks like regex engine goes backwards if it can't match after too greedy submatch. BTW if my answer was helpful consider accepting it by clicking to the huge check mark on the left. – vbo Jan 23 '14 at 21:23
  • Interesting. That's pretty smart! – user825067 Jan 24 '14 at 01:01