Golang exec.Command 多个管道

huangapple go评论104阅读模式
英文:

Golang exec.Command multiple pipes

问题

我正在尝试使用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文件上执行exuberant ctags,然后grep任何指示方法的行,然后仅忽略grepped输出的前四列。

英文:

I'm trying to do multiple pipes with Go:

ctags := exec.Command(&quot;ctags&quot;, &quot;-x&quot;, &quot;--c-types=f&quot;, &quot;./tmp/&quot;+fileName)
grep  := exec.Command(&quot;grep&quot;, &quot;member&quot;)
awk   := exec.Command(&quot;awk&quot;, &quot;&#39;{$1=$2=$3=$4=\&quot;\&quot;; print $0}&#39;&quot;)
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)

This is what the expected output should look like:

$ ctags -x --c-types=f ./tmp/genetic_algorithm.py | grep member | awk &#39;{$1=$2=$3=$4=&quot;&quot;; print $0}&#39;
    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):

This is what I'm getting:

&amp;{/usr/bin/awk [awk &#39;{$1=$2=$3=$4=&quot;&quot;; print $0}&#39;] []  0xc4204ce020 0xc42007e008 &lt;nil&gt; [] &lt;nil&gt; 0xc4201fa900 exit status 2 &lt;nil&gt; &lt;nil&gt; true [0xc4204ce020 0xc42007e008 0xc4204ce048] [0xc4204ce048] [] [] 0xc420070360 &lt;nil&gt;}

I've found several examples of piping commands with Go, but most of them only pipe two commands. There's this example https://gist.github.com/dagoof/1477401, but when I arranged my code according to that logic, I got the same issue. Am I piping correctly? The command is just supposed to execute exuberant ctags on that Python file and then grep any rows indicating method and then only ignore the first four columns of the grepped output.

答案1

得分: 2

问题出在:

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

我只需要移除单引号。

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

The issue was with:

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

I just needed to remove the single quotes.

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

huangapple
  • 本文由 发表于 2016年12月31日 02:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/41400960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定