cryptomanic
cryptomanic

Reputation: 6306

Why sed command is unable to substitute matched string?

My file name 'old' contains '123 abc'. When I use the below sed command in linux terminal:

 sed 's:[0-9]+:& &:' <old \> new 

new file contains same data as old that is '123 abc'. But output should be '123 123 abc'. I don,t know where i am wrong. Can anyone help me please......

Upvotes: 0

Views: 61

Answers (2)

Chris Maes
Chris Maes

Reputation: 37752

Two small remarks:

  • you shold use a backslash before the + sign
  • no need to use a backslash before the > sign to pipe the output:

    sed 's:[0-9]\+:& &:' old > new

(and a minor change; but not necessary: no need for the < before the input file; sed knows you will give it a file.)

Upvotes: 1

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

Change your command to:

sed 's:[0-9]\+:& &:' old > new

OR

sed -r 's:[0-9]+:& &:' old > new 

-r : Use extended regular expressions (in our case, for + to work as expected)

Upvotes: 1

Related Questions