duanhongqiong9460 2016-12-30 18:30
浏览 193

Golang执行命令多个管道

I'm trying to do multiple pipes with 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)

This is what the expected output should look like:

$ 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):

This is what I'm getting:

&{/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>}

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条回答 默认 最新

  • dongtuojuan8998 2016-12-30 18:37
    关注

    The issue was with:

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

    I just needed to remove the single quotes.

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

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数