gpt4 book ai didi

bash - sed: 1: "'/正则表达式/.. .": invalid command code '

转载 作者:数据小太阳 更新时间:2023-10-29 03:14:37 28 4
gpt4 key购买 nike

我正在尝试编写一个程序来自动组装和运行 sed 命令。我正在使用以下代码片段生成命令:

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 ""
}

然后我使用以下代码片段运行完整命令:

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)
}

这会导致以下输出(生成 100 条命令,这只是 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

这很奇怪,但更奇怪的是,如果我手动运行生成的命令(当然没有方括号),它工作得很好!这是怎么回事?

最佳答案

Shell 去除引号字符,exec.Command 不去除。所以可能 sed 正在通过命令传递给 '。尝试形成不带单引号的命令:

switch command {
// remove single quotes from strings
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 ""
}

关于bash - sed: 1: "'/正则表达式/.. .": invalid command code ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40122581/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com