Equivalent to grep in Go or a way to use wildcards in searches? I am trying to search through another commands output ([]bytes) and do a count and retrieve some data

201 views
Skip to first unread message

john.ma...@gmail.com

unread,
Jun 7, 2018, 4:31:02 PM6/7/18
to golang-nuts
My apologies in advance if this comes across scatterbrained.  I am a little fizzled out and am going to take a break on this.

I am building a custom exporter to use with FreeNAS (https://github.com/Maelos/freenas_exporter) and have run into another roadblock.  While the command line on the FreeNAS VM works using 

ipmitool -I lanplus -H ipmiHost -U ipmiUser -f ipmiPWFile sdr elist all | grep -c -i "cpu.*temp"

I have been less successful using the os.exec package like so (or similar to)

commandText := []string{"bash", "-c", "/usr/local/bin/ipmitool -I lanplus -H", ipmiHost, "-U", ipmiUser, "-f", ipmiPWFile, "sdr elist all | grep -c -i \"cpu.*temp\""}
numCPUCmd
:= exec.Command(commandText[0], commandText[1:]...)
numCPUBytes
, err := numCPUCmd.Output()

This is likely for many reasons, but I am more focused on the next step here. I have since looked back and am attempting to use the standard library but am now stuck on how to do the equivalent of the above with, or similar to, the below:

cpuNum := bytes.Count(ipmiOutput, []byte("cpu.([]+)temp"))

Thank you for any guidance. I may go back to using the exec commands and piping one StdOut to another's StdIn.

Justin Israel

unread,
Jun 7, 2018, 4:44:23 PM6/7/18
to john.ma...@gmail.com, golang-nuts


On Fri, Jun 8, 2018, 8:30 AM <john.ma...@gmail.com> wrote:
My apologies in advance if this comes across scatterbrained.  I am a little fizzled out and am going to take a break on this.

I am building a custom exporter to use with FreeNAS (https://github.com/Maelos/freenas_exporter) and have run into another roadblock.  While the command line on the FreeNAS VM works using 

ipmitool -I lanplus -H ipmiHost -U ipmiUser -f ipmiPWFile sdr elist all | grep -c -i "cpu.*temp"

I have been less successful using the os.exec package like so (or similar to)

commandText := []string{"bash", "-c", "/usr/local/bin/ipmitool -I lanplus -H", ipmiHost, "-U", ipmiUser, "-f", ipmiPWFile, "sdr elist all | grep -c -i \"cpu.*temp\""}
numCPUCmd
:= exec.Command(commandText[0], commandText[1:]...)
numCPUBytes
, err := numCPUCmd.Output()

This is likely for many reasons,

When you are using shell commands that need expansion, pipes, etc, and you are passing it to /bin/bash -c, the entire command after the -c needs to be a string. In your example, you start with part of the command as a string and then start passing more items in the slice. Just string format your command first and pass it as the 3rd item in your slice. 

cmd := fmt.Sprintf("/usr/local/bin/ipmitool -I lanplus -H %s -U %s -f %s sdr elist all | grep -c -i 'cpu.*temp'", ipmiHost, ipmiUser, ipmiPWFile) 

but I am more focused on the next step here. I have since looked back and am attempting to use the standard library but am now stuck on how to do the equivalent of the above with, or similar to, the below:

cpuNum := bytes.Count(ipmiOutput, []byte("cpu.([]+)temp"))

Take a look at counting the results of a regular expression match:



Thank you for any guidance. I may go back to using the exec commands and piping one StdOut to another's StdIn.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

john.ma...@gmail.com

unread,
Jun 7, 2018, 4:55:27 PM6/7/18
to golang-nuts
Thank you for the quick reply.  I should have reached out sooner.  I thought I had it working earlier by defining the entire string (no separate variables) and this should work. 
Reply all
Reply to author
Forward
0 new messages