Pipe multiple commands in golang

1,024 views
Skip to first unread message

thatipelli santhosh

unread,
Aug 12, 2020, 11:41:39 AM8/12/20
to golang-nuts
Hi all,

I have requirement to execute a command like cmd : strings dynamic_file_path | grep regex

I don't know what is the best way to do this like any well known library or standard way.

Can you please help here?

Marcin Romaszewicz

unread,
Aug 12, 2020, 11:55:56 AM8/12/20
to thatipelli santhosh, golang-nuts
You have two options here.

One, is you execute a shell, and just tell the shell to run your command, eg bash -c "strings dynamic_file_path | grep regex".

The other option is that you exec strings and grep independently, and connect the two of them using an os.pipe, but it's a lot more work to do this and get exiting working properly - you must close the pipe when either side has an error, etc.



--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAN1Sb7y2DdZJP30cfxRKr%3DFKgFsc1Kp%2Bdzqe0pd3qAt4JDg6LQ%40mail.gmail.com.

Tamás Gulácsi

unread,
Aug 12, 2020, 2:11:35 PM8/12/20
to golang-nuts
cmdStrings := exec.Command("strings", dynamicFilePath)
cmdRegex := exec.Command("grep", "regex")
cmdRegex.Stdin = cmdStrings.Stdout
if err := cmdRegex.Start(); ...
if err := cmdStrings.Run(); ...
if err := cmdRegex.Wait();...


Or use http://labix.org/pipe
Reply all
Reply to author
Forward
0 new messages