2

I have this text in this file:

test.php

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'original',
      'username' => 'root',
      'password' => 'root',
      'host' => 'localhost'
    ),
  ),
);

In terminal run this line to replace 'original' with 'new'

sed -i 's/original/new/g' test.php

UPDATE: The error message is:

sed: 1: "test.php": undefined label 'est.php'

What is the problem?

UPDATE 2:

If I just run: ( I removed '-i')

sed 's/original/new/g' test.php

I see the file text modified in the terminal. But then the file is not saved.

chefnelone
  • 189
  • 1
  • 4
  • 16
  • It's rather unlikely to get the error message you've mentioned by issuing `sed -i 's/original/new/g' test.php`. What command did you use? – devnull Jan 20 '14 at 07:33
  • possible duplicate of [why "extra characters after command" error shown for the sed command line shown?](http://stackoverflow.com/questions/17924737/why-extra-characters-after-command-error-shown-for-the-sed-command-line-shown) – tripleee Jan 20 '14 at 07:39
  • I found that problem is the '-i'. If I run just 'sed 's/original/new/g' test.php' (I removed '-i') it works but the file is not saved. – chefnelone Jan 20 '14 at 12:05
  • BTW: why the downvote...? – chefnelone Jan 20 '14 at 12:05
  • @devnull I just copy/paste the commands. Pretty sure. – chefnelone Jan 20 '14 at 12:06
  • @devnull Sorry you were right. The error message is this one: sed: 1: "test.php": undefined label 'est.php' – chefnelone Jan 20 '14 at 12:18
  • @chefnelone You haven't still showed the actual problem code. It seems that the linked duplicate question would resolve the issue you're facing. – devnull Jan 20 '14 at 12:21
  • @devnull I updated my question with the right error message. I tried using other delimiter as said in the other question link but it didn't work. – chefnelone Jan 20 '14 at 12:23
  • @chefnelone Are the pattern & replacement strings variables? – devnull Jan 20 '14 at 12:24
  • @devnull no, they are just text – chefnelone Jan 20 '14 at 12:26

1 Answers1

23

On BSDish platforms (including Mac OSX), the -i option requires an argument.

sed -i '' 's/original/new/g' test.php

Notice the empty argument ''.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • If you don't ever expect multiple matches on a single line, the `/g` flag is superfluous. – tripleee Dec 11 '18 at 05:23
  • Related: options for doing `sed -i` portably: https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux – tripleee Dec 13 '18 at 16:12