I'm trying to write a program to automatically assemble and run sed commands. I'm using the following code snippet to generate commands:
switch command {
case "=", "d":
return fmt.Sprintf("'/%s/ %s'", regex, command)
case "c", "a", "i":
return fmt.Sprintf("'/%s/ %s\\\n%s'", regex, command, phrase)
case "s", "y":
return fmt.Sprintf("'%s/%s/%s/'", command, regex, phrase)
default:
return ""
}
Then I use the following code snippet to run the full command:
fmt.Println("Running command: sed", args)
program := exec.Command(programName, args...)
output, err := program.CombinedOutput()
fmt.Printf("%s", output)
if err != nil {
fmt.Println(err)
}
Which results in the following output (Generating 100s of commands, this is just 1):
Running command: sed [-e '/([0a-z][a-z0-9]*,)+/ c\
abc said he""llo!!!\n 1 ' -e '/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)/ a\
0,0,0,156, aac' -e '/(s?)a.b,a\nb/ d' -f number_lines.txt -f eliminate_punctuation.txt -f delete_leading_trailing_whitespace.txt -f delete_last_ten_lines.txt -f eliminate_blanks.txt -f number_non_blank_lines.txt -f reverse_lines.txt -f strip.txt input1.txt input2.txt input3.txt]
sed: 1: " '/([0a-z][a-z0-9]*,)+/ ...": invalid command code '
exit status 1
That's weird, but what's weirder is if I run the generated command by hand (Without the square brackets of course), it works just fine! What's going on here?