1

I've got a command that I run that outputs to stdio that I need to redirect to an existing file, replacing the word replaceme with the two lines output by my command.

The command mycommand outputs like this:
somedata=this
somedata=that

I'm able to get the output into an environment variable:

export TWOLINES=$(mycommand)

When I echo it back out it comes out as a single line

echo $TWOLINES

Returns:
somedata1=this somedata2=that

I need to get this content into my file as two lines

Attempting to do:

sed -e "s,replaceme,${TWOLINES}," -i file

Returns: unterminated 's' command

Thoughts or other tools besides sed that could assist?

Derek
  • 313
  • 4
  • 11
  • 1
    Use quotes: `echo "$TWOLINES"`. – Walter A Mar 28 '18 at 21:01
  • 1
    [BashFAQ #21](https://mywiki.wooledge.org/BashFAQ/021) provides tools that -- unlike `sed` -- work with completely arbitrary source and replacement strings; no problem with newlines, no need for a sigil variable (`,` in your example) that can't be present in the contents. See the `gsub_literal` function for which source is provided in said FAQ for one such example. – Charles Duffy Mar 28 '18 at 21:03
  • BTW, in general, you shouldn't use an environment variable unless you know why you need one (instead of using a regular shell variable). The more environment variables you have defined, and the longer their contents, the shorter maximum command line length you can use when starting other commands. – Charles Duffy Mar 28 '18 at 21:03

0 Answers0