awk | Replace String in a File

In Linux, “awk” is a utility to perform various operations on the text files such as displaying the file record, replacing any string in a file, etc. The working of this command is just like the programming language, which doesn’t need any compiler and gives access to variables, logical operators, and string functions.

This post will demonstrate replacing a string in a file using the “awk” command.

How to Replace String in a File Using awk Command?

Using the awk command, the replacement of any string in a file can be done. If you want to know the basics of the “awk” command, check out our article on the awk command.

Example 1: Replacing a String With Another String

In this example, a string will be replaced with another string in a file named “linuxfoss.txt” with the help of the if statement and the awk command. Before that, have a look at the content of the file on which the “awk” utility will be applied:

$ cat linuxfoss.txt

The following “awk” command is utilized to replace the string named “Author” with “technical author”:

$ awk '{if($2=="Author") {$2="technical author"} print $0}' linuxfoss.txt

The command is described as follows:

  • $2 represents the second location (Column) of every line in the file so it will find the “Author” word in each line.
  • Once the “Author” string is found, then, if the statement executes and the value of $2 will be replaced with the “technical author”.
  • Here, $0 means printing the entire line.

The execution of the command is as follows:

All Strings named “Author” have been changed to “technical author”.

Example 2: Replacing String Enclosed in Specific Character

Sometimes, users want to change the string between the specific character (the parentheses are considered here). The file’s content is provided below, which will be used in this example:

$ cat itslinuxfoss.txt

Let’s say we want to change the “Author” word to “Henry Author”, which is under the parentheses. So a normal string-changing command cannot be used in that case, and we need to use the “gsub” global substitutions.

Our delimiter (end of section file) is the parenthesis, so we must define it in our command.

$ awk -vRS=")" '/Author/{ gsub(/Author/,"Henry Author"); }1' ORS=")" itslinuxfoss.txt

The command is described as follows:

  • First “vRS” representing the variable current record separator which means our string is ending before the closing parenthesis “)”.
  • In the second part of the command, we have used global substitutions to search for specific strings. Once it finds any closing parenthesis, it will match the word with the given input “Author” if it matches, then it will be replaced with the “Henry Author”.
  • Here, “ORS” is the output record separator.

Below is the execution of this command:

“Henry” string has been to the “Henry Author”, as shown in the above image.

Bonus Tip: Replace String in a File Using sed Utility

There is an alternate command, “sed”, which can replace any string in a file. There is only a syntax change between the commands. Consider the following output of the file while understanding the concept of the “sed” command:

$ cat linuxfoss.tx

To replace a string in a file using the “sed” command is obtained as follows:

$ sed -i 's/Henry/Joseph/' linuxfoss.txt

After executing the above command, the content of the file will be changed from “Henry” to “Joseph” which can be seen in the below image:

$ cat linuxfoss.txt

Note: If you want to know more about “sed”, check out our article on sed command.

Conclusion

In any file of Linux, a string can be replaced using the awk command with “$” variables or the global substitutions (gsub). This utility will match the string and replace it with the given one. This post has demonstrated the replacement method of any string in a file using the awk command.