Another.Chemist
Another.Chemist

Reputation: 2559

Why SED delete first character after replacement pattern

I was wondering what is wrong with:

b="inst.;inst" ; echo $b | sed -e 's/\.;[^ ]/\n /' inst nst

Expected Output

inst
inst

Thank too much in advance for any clue.

Upvotes: 0

Views: 39

Answers (3)

giantnrs
giantnrs

Reputation: 1

Or just change your command to b="inst.;inst" ; echo $b | sed -e 's/.;/\n/' replace .; with a newline

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80921

Your pattern matches three characters. A period, a semi-colon, and some non-space character. You then replace all three of those characters with two characters (newline and space).

So your pattern matches .;i and you replace that with \n.

You need to capture and re-insert that non-space character.

Use \([^ ]\) in the pattern and \n \1 as the replacement.

Upvotes: 4

Hasturkun
Hasturkun

Reputation: 36402

You are replacing the first character of inst, capture and preserve it instead, i.e.

sed -e 's/\.;\([^ ]\)/\n \1/'

Upvotes: 2

Related Questions