Asked  7 Months ago    Answers:  5   Viewed   85 times

I've successfully used the following sed command to search/replace text in Linux:

sed -i 's/old_link/new_link/g' *

However, when I try it on my Mac OS X, I get:

"command c expects followed by text"

I thought my Mac runs a normal BASH shell. What's up?

EDIT:

According to @High Performance, this is due to Mac sed being of a different (BSD) flavor, so my question would therefore be how do I replicate this command in BSD sed?

EDIT:

Here is an actual example that causes this:

sed -i 's/hello/gbye/g' *

 Answers

56

If you use the -i option you need to provide an extension for your backups.

If you have:

File1.txt
File2.cfg

The command (note the lack of space between -i and '' and the -e to make it work on new versions of Mac and on GNU):

sed -i'.original' -e 's/old_link/new_link/g' *

Create 2 backup files like:

File1.txt.original
File2.cfg.original

There is no portable way to avoid making backup files because it is impossible to find a mix of sed commands that works on all cases:

  • sed -i -e ... - does not work on OS X as it creates -e backups
  • sed -i'' -e ... - does not work on OS X 10.6 but works on 10.9+
  • sed -i '' -e ... - not working on GNU

Note Given that there isn't a sed command working on all platforms, you can try to use another command to achieve the same result.

E.g., perl -i -pe's/old_link/new_link/g' *

Tuesday, June 1, 2021
 
rblarsen
answered 7 Months ago
35

Instead of the "click" event, use "change", as in

$("#productOptions").change(function(){
    var id=$(this).attr("value");

    if (id != "") {
        $("#productAddToCart").attr("href","addtocart.php?code="+id);
    }
});
Friday, August 6, 2021
 
tombuarts
answered 4 Months ago
16

According to the man page you should specify a zero length extension on macOS

sed -i '' -e 's/noreply@(.*).example.com/noreply@example.com/' 
Sunday, August 8, 2021
 
DaveRandom
answered 4 Months ago
93

As seen in SuperUser's How to match whitespace in sed?:

For POSIX compliance, use the character class [[:space:]] instead of s, since the latter is a GNU sed extension

So you are probably running a non-GNU sed version, hence s not working to you.

You have two solutions:

  • To use (space) and t together, like you were doing.
  • To use [[:space:]].
Tuesday, August 17, 2021
 
superfell
answered 4 Months ago
38

The -E flag means to use extended regular expressions. You should just use -e, as on Linux. The sed in Mac OS X is based on BSD sed, so doesn't have the GNU extensions.

After copying your sample input:

[~ 507] pbpaste | sed -e 's,^[[:space:]]*psd(.*);,,'
apa
bepa

depa psd(epa);
Saturday, September 18, 2021
 
jenny
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share