Run awk in golang

I’m trying to execute this code and I only get errors

_, err := exec.Command(“awk”, ‘{gsub(/./,",",$0);print $0}’, file, “| sponge”, file).Output()

can you help me?

Thank you

1 Like

What errors?

A syntax error because of wrong usage of '? Or because awk does not understand | sponge as an argument?

exec.Command() bypasses your shell and directly starts your program, you’ll need to pipe between awk and sponge manually.

Also ' is for runes, not for strings, you need to use either " and escape the inner quotes, or use raw strings surrounded by backticks (`{gsub(/./,",",$0);print $0}`).

1 Like

_, err := exec.Command(“awk”, {gsub(/\./",",$0);print $0}, file, “| sponge”, file).Output()

Error: exit status 2

1 Like

And what does stderr say? It’s probably the unknown argument as explained earlier

1 Like


I do not know how to apply stderr to this case

what I’m trying to achieve is to replace “.” by “,” in a giant file, and the fastest option I have found is to use awk in this way, but I am not able to run it from golang

1 Like

Easiest were to use exec.(*Cmd).CombinedOutput(), at least in your current situation, its the least invasive fix.

output, err := exec.Command(“awk”, `{gsub(/\./",",$0);print $0}`, file, “| sponge”, file).CombinedOutput()

It will probably like this:

# Created by newuser for 5.7.1
awk: cmd. line:1: fatal: cannot open file `| sponge' for reading (No such file or directory)

As you are running a command that is equivalent to the following in bash:

awk '{gsub(/\./",",$0);print $0}' ~/.zshrc "| sponge" ~/.zshrc
1 Like

yes, when executing that code I got that error

1 Like

You need to manually manage the *Cmds lifecycle and manually connect stdout of awk with stdin of sponge using exec.(*Cmd).Run(), exec.(*Cmd).StdoutPine(), and exec.(*Cmd).StdinPipe().

I never actually have done that.

Alternatively, you can start bash and do something like this:

output, err := exec.Command(bash, "-c", fmt.Sprintf(`awk '{gsub(/\./",",$0);print $0}' %[1]s | sponge %[1]s`, file)),Output()

I’d suggest to not throw away stderr though, but use Run() instead and prepare buffers that you attach to stderr/stdout to make sure you can show output in case of an error, as that makes it much easier to debug…

1 Like

Sincerely, thanks for your help.

I have executed:

output, err: = exec.Command (“bash”, “-c”, fmt.Sprintf (awk '{gsub (/\./",",$ 0); print $ 0}'% [1] s | sponge % [1] s, fileAnalysis)). Output ()

and it does not give an error, but it does not work

1 Like

What does, does not work mean? Also please use markdown to properly format your code… Also there are some spaces in the code you pasted, those are not allowed to be there…

1 Like


output, err := exec.Command("bash", "-c", fmt.Sprintf(`awk '{gsub(/\./",",$0);print $0}' %[1]s | sponge %[1]s`, file)).Output()

it does not give an error, but it does not replace points with commas, which is what I want the code to do

1 Like

Check the results of the Sprintf call if it is like you would type it on the command line.

But on a first glance it seems as if some coma has gone missing in one of your posts… I just copied from there.

Debugging the awk is your job I don’t understand it.

1 Like

Might as well get the obvious solution out of the way first:

/tmp % echo "Here. Is. A file." > foo
/tmp % sed -i.bak -e 's/\./,/g' foo
/tmp % cat foo
Here, Is, A file,

But if you must do it in Go and must use awk, @NobbZ was correct about your disappeared comma.

You had:

awk '{gsub(/\./",",$0);print $0}' %[1]s | sponge %[1]s

I found success by adding a comma after the regex /\./:

awk '{gsub(/\./, ",",$0);print $0}' %[1]s | sponge %[1]s
1 Like

Have you considered tr? tr . ,


@epicteto here is how I use awk in my status bar application:

First I set the command as a string to a variable:

homeCmd := "df -Ph .| awk '/d*G/ {print $4}'"

Then when calling the command I had to run it via sh -c (and in my case I capture the output):

homeOut, err := exec.Command("sh", "-c", homeCmd).Output()

Hope this sheds some light on the subject!


Thanks for your advice guys, I got it to work thanks to you


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.