Linux awk replace string

How can I do a recursive find/replace of a string with awk or sed?

oh my god this is exactly what I just did. But it worked and doesn’t seem to have done any harm. Whats the worst that could happen?

Quick tip for all the people using sed: It will add trailing newlines to your files. If you don’t want them, first do a find-replace that won’t match anything, and commit that to git. Then do the real one. Then rebase interactively and delete the first one.

You can exclude a directory, such as git, from the results by using -path ./.git -prune -o in find . -path ./.git -prune -o -type f -name ‘*matchThisText*’ -print0 before piping to xargs

37 Answers 37

find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g' 

-print0 tells find to print each of the results separated by a null character, rather than a new line. In the unlikely event that your directory has files with newlines in the names, this still lets xargs work on the correct filenames.

\( -type d -name .git -prune \) is an expression which completely skips over all directories named .git . You could easily expand it, if you use SVN or have other folders you want to preserve — just match against more names. It’s roughly equivalent to -not -path .git , but more efficient, because rather than checking every file in the directory, it skips it entirely. The -o after it is required because of how -prune actually works.

For more information, see man find .

Источник

How to Replace a String in a File in Bash

As a programmer, you might need to work with different types of files to store data temporarily or permanently. Sometimes, you may need to replace part of the file or modify the particular content of the file. To replace content in a file, you must search for the particular file string. The ‘sed’ command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The ‘awk’ command can also be used to replace the string in a file. This tutorial will show you how to replace any string value from a file using a bash script.

Читайте также:  What is software package in linux

Sample Data for Bash String Replacement

A text file named Sales.txt with the following content is created to show the replacement operations.
Sales.txt

01/01/2020 60000 Dhaka
10/02/2020 76000 Rajshahi
21/03/2020 54000 Khulna
15/04/2020 78000 Chandpur
17/05/2020 45000 Bogra
02/06/2020 67000 Comilla

General Syntax for Sed: Replace String in a File

The basic syntax of the `sed` command for replacing the particular string in a file is given below.

Every part of the above syntax is explained below.

‘-i’ option is used to modify the content of the original file with the replacement string if the search string exists in the file.
‘s’ indicates the substitute command.
‘search_string’ contains the string value that will be searched in the file for replacement.
‘replace_string’ contains the string value that will be used to replace the content of the file that matches the ‘search_string’ value.
‘filename’ contains the filename where the search and replace will be applied.

Example 1: Replace File with the ‘sed’ Command

In the following script, the search-and-replace text will be taken from the user. If the search string exists in ‘Sales.txt’, then it will be replaced by the replacement string. Here, a case-sensitive search will be performed.

# Assign the filename
filename = «Sales.txt»

# Take the search string
read -p «Enter the search string: » search

# Take the replace string
read -p «Enter the replace string: » replace

if [ [ $search ! = «» && $replace ! = «» ] ] ; then
sed -i «s/ $search / $replace /» $filename
fi

Example 2: Replace File with the ‘sed’ Command with ‘g’ and ‘i’ Flag

The following script will work like the previous example, but the search string will be searched globally for the ‘g’ flag, and the case-insensitive search will be done for the ‘i’ flag.

# Take the search string
read -p «Enter the search string: » search

# Take the replace string
read -p «Enter the replace string: » replace

if [ [ $search ! = «» && $replace ! = «» ] ] ; then
sed -i «s/ $search / $replace /gi» $1
fi

Example 3: Replace File with `sed` Command and Matching Digit Pattern

The following script will search for all numerical content in a file and will replace the content by adding the ‘$’ symbol at the beginning of the numbers.

# Check the command line argument value exists or not
if [ $1 ! = «» ] ; then
# Search all string containing digits and add $
sed -i ‘s/\b1\\b/$&/g’ $1
fi

Replace String in a File with `awk` Command

The ‘awk’ command is another way to replace the string in a file, but this command cannot update the original file directly like the ‘sed’ command.

Example 4: Replace File with `awk` Command

The following script will store the updated content in the temp.txt file that will be renamed by the original file.

# Check the command line argument value exists or not
if [ $1 ! = «» ] ; then
# Search all string based on date
awk ‘ ‘ $1 > temp.txt && mv temp.txt $1
fi

Читайте также:  Hp proliant linux tools

In the above example `awk` command is used to search for a string “02/06/2020”, which is the last date in our sample file. The matched string will be replaced by a new string “12/06/2020”. The string resulting whether substituted or not is printed on each line.

Example 5: Replace string in file with Pure Bash Scripting

In this example we will do the same job with a Bash while loop and a bash string replacement command. We will write the output to a tmp file and replace the original at the end of the script. See the code below:

# Replace all instances on line of Khulna with Dhaka
echo $

mv / tmp / Sales.o.txt Sales.txt

You can see the echo command will do a replacement in the line variable, as the script reads each line in the text file, and replace all instances of Khulna with the new string Dhaka. The while loop reads from the input file Sales.txt and writes to the output file /tmp/Sales.o.txt. Finally the temporary output file replaces the original file with the mv command.

Example 6: Replace string in file with Perl one liner

This task is actually super simple with Perl. Perl is an old, less popular language but extremely powerful. This can be done with a line command in the bash command line:

-pi.bak tells perl to process the file in place and also create a backup file with the .bak extension. The replacement will be all instances of Dhaka for Bogra as the new text. Here you can see the easy output of this command:

Conclusion

This article showed you how to use bash scripts to replace strings in a file with search and replace on the command line in bash. There are many ways to do this including with commands sed, awk, perl; you can use native bash scripting with loops and regex and there are more ways even not discussed here. Bash and linux have many ways to do the same job.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

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:

Читайте также:  Linux mint процесс установки

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

$ awk ' 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:

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:

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:

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.

Источник

Оцените статью
Adblock
detector