Reputation: 147
Trying to utilize the following command in scala bash file (sys.process._ has been imported):
val writeToLine5 = "sed -i '5a some text' to.file".!
the following error emerges:
> "sed: -e expression #1, char 1: unknown command: `'';
The command itself works perfect in command line.
Have tried also:
"""sed -i "5a adding some text to" file.text""".!;
"sed -i \'5a adding some text to\' file.text".!;
Is there any scala shell scripting specialist here? Thank you!
PS: have asked on askubuntu.com. They have suggested to ask here.
Upvotes: 2
Views: 278
Reputation: 51271
The interpretation of the '
character is done by the shell, not by sed
itself, so it's usually easiest to ask the shell to do it for you.
import sys.process._
val writeToLine5 = Seq("sh", "-c", "sed -i '5a some text' to.file").!
But you can do the interpretation yourself.
val writeToLine5 = Seq("sed", "-i", "5a some text", "to.file").!
You could also use a Regex pattern to interpret the internal quotations, but it's error prone and I really don't recommend it.
val cmd = "sed -i '5a some text' to.file"
val res = cmd.split(""" +(?=([^'"]*['"][^'"]*['"])*[^'"]*$)""") //split on non-quoted spaces
.map(_.replaceAll("['\"]","")) //remove all the internal quote marks
.toSeq.!
Upvotes: 4