How to use 'sed' and 'awk' in Golang? [SOLVED]


GO, GOLANG Solutions

In this tutorial we will cover different examples using sed and awk commands from shell into golang programming language. We have already covered exec.Command and context package usage and examples in our previous articles to run shell commands How to execute shell commands in Golang?

So we will re-use these modules in this tutorial to also execute awk and sed commands.

 

Using sed in golang

Example-1: Perform a simple search and replace

Here is a simple example to perform search and replace operation in golang using sed:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("sed", "-i", "s/oldstring/newstring/g", "file.txt")
    out, err := cmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
}

 

Example-2: Using sed to delete a range of lines from a file

Let's perform another operation to delete a range of lines from a file:

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("sed", "-i", "2,5d", "file.txt")
	out, err := cmd.Output()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(out))
}

 

Example-3: Use sed to delete all lines containing specific string

Here is another example to perform in file sed operation by searching for all lines with specific string and then deleting those lines.

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("sed", "-i", "/string/d", "file.txt")
    out, err := cmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
}

 

Example-4: Use sed to replace string with regex pattern match

Let's use a complex sed operation perform regex match. Here we use sed to replace a string only in lines that match a specific pattern:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("sed", "-i", "/^[0-9]/s/oldstring/newstring/g", "file.txt")
    out, err := cmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
}

This command will replace the string "oldstring" with "newstring" only in lines that start with a number in the file "file.txt".

 

Using awk in golang

Let's looks at some more examples using awk in golang

 

Example-1: Performing simple awk operation

Here is a simple example which uses default field separator to print 2nd column of the input file:

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("awk", "{print $2}", "file.txt")
	out, err := cmd.Output()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(out))
}

 

Example-2: Using awk to print sum of values

Let's take our example to another level and try to perform some math operation inside golang. This command will print the sum of the values in the second column of the file "file.txt".

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("awk", "{sum += $2} END {print sum}", "file.txt")
	out, err := cmd.Output()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(out))
}

 

Example-3: Use awk to with custom field separator

In awk, you can use the -F option to specify a field separator, which is used to split the input into fields. The default field separator is whitespace, but you can change it to any character or regular expression.

Here's an example of how you might use the -F option with the awk command to change the field separator to a comma:

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("awk", "-F", ",", "{print $2}", "file.txt")
	out, err := cmd.Output()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(out))
}

This command will print the second field of each line in the file "file.txt", with the fields being separated by a comma.

 

Summary

We have covered multiple scenarios and examples using sed and awk language in golang programming language. We can perform both simple and complex operations using these tool inside the golang code.

 

Further Reading

Calling "sed" from exec.Command - Stack Overflow
Replace a line in text file Golang - Stack Overflow

 

 

Views: 41
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment