0

I've been given someone else's shell script (bash) to go through and one of the lines is as follows:

sed -i "s@_RUNDIR_@$RUNDIRECTORY/runSettings.txt@" start.m

Of course, $RUNDIRECTORY is specified in the file, but RUNDIR isnt specified anywhere. runSettings.txt is a file I have access to and I think the goal here is to replace certain parts of a path with $RUNDIRECTORY. From a quick search on the syntax of sed -i, I'm very confused what the s and @ do in the string.

Can someone help me out with a very basic example?

2
  • That set command replaces the literal string RUNDIR with something else, so RUNDIR is not supposed to be set. @ is just separating the arguments to sed. 'man sed' is your friend. Commented May 28, 2014 at 21:48
  • This isn't an answer specific to your question, but Mac's version of 'sed' varies from the GNU version. I'd suggest installing the GNU version, if at all possible, via Homebrew or Macports. Then you could run the same commands on Mac or Linux (and get more help from people who can cater to both questions). Commented May 28, 2014 at 21:49

2 Answers 2

1

The -i flag means that it modifies your file: usually, sed will only output its results to stdout. You COULD pipe the result into your file, or use that replace inline flag instead.

The s/[search]/replace]/ format starts with s which means that you want to do a search. You can use most any character in place of /, so in the example you posted, the @ character was used instead: s@[search]@replace@ That is good for when either the [search] or the [replace] terms contain a / character so you don't have to do any fancy \/ escaping.

Sign up to request clarification or add additional context in comments.

Comments

0

RUNDIR is not a variable. It is part of the text that the author expected to be present in some of the lines in start.m.

s means that the first occurrence on any line of the first pattern (_RUNDIR_) should be replaced with the value of the second pattern ($RUNDIRECTORY/runSettings.txt).

The @ is an arbitrary character meant to delineate pattern 1 and pattern 2.

Type man sed in the Terminal for more information.

If the intent is to substitute the literal text $RUNDIRECTORY into the file -- rather than to put in whatever might be the value of that shell variable in your current environment -- then you will want to use single quotes instead of double quotes:

sed -i 's@_RUNDIR_@$RUNDIRECTORY/runSettings.txt@' start.m 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.