Golang exec.Command多个管道

时间:2016-12-30 18:30:22

标签: bash go pipe ctags

我正在尝试使用Go执行多个管道:

ctags := exec.Command("ctags", "-x", "--c-types=f", "./tmp/"+fileName)
grep  := exec.Command("grep", "member")
awk   := exec.Command("awk", "'{$1=$2=$3=$4=\"\"; print $0}'")
grep.Stdin, _ = ctags.StdoutPipe()
awk.Stdin, _ = grep.StdoutPipe()
awk.Stdout = os.Stdout
_ = grep.Start()
_ = awk.Start()
_ = ctags.Run()
_ = grep.Wait()
_ = awk.Wait()

fmt.Println(awk)

这是预期输出的样子:

$ ctags -x --c-types=f ./tmp/genetic_algorithm.py | grep member | awk '{$1=$2=$3=$4=""; print $0}'
    def __init__(self, vector, fitness_function):
    def __init__(self, population_size=10, crossover_points=[.3, .6],
    def apply_crossover(self, genotype_1, genotype_2):
    def apply_mutation(self, child_genotype):
    def calc_population_fitness(self, individuals):
    def evolve(self):
    def final_genotype(self):
    def fitness(self):
    def generate_offspring(self, selected):
    def genotype(self, indiv):
    def get_accuracy(nn_hidden_structure=[10]):
    def parse_bitstring(self, genotype):
    def run(self):
    def select_for_crossover(self):
    def slice_vector(self, vector):
    def validate_vector(self, vector, max_nodes=100):
    def vector_from_bitstring(self, genotype):

这就是我得到的:

&{/usr/bin/awk [awk '{$1=$2=$3=$4=""; print $0}'] []  0xc4204ce020 0xc42007e008 <nil> [] <nil> 0xc4201fa900 exit status 2 <nil> <nil> true [0xc4204ce020 0xc42007e008 0xc4204ce048] [0xc4204ce048] [] [] 0xc420070360 <nil>}

我在Go中找到了几个管道命令的例子,但是大多数例子只管道两个命令。这个例子是https://gist.github.com/dagoof/1477401,但是当我根据这个逻辑安排我的代码时,我遇到了同样的问题。我管道正确吗?该命令只是在该Python文件上执行繁琐的ctags,然后grep任何指示方法的行,然后只忽略grepped输出的前四列。

1 个答案:

答案 0 :(得分:1)

问题在于:

awk   := exec.Command("awk", "'{$1=$2=$3=$4=\"\"; print $0}'")

我只需删除单引号。

awk   := exec.Command("awk", "{$1=$2=$3=$4=\"\"; print $0}")