Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: July 22, 2024
Managing and manipulating text files on Linux is a common responsibility for system administrators. One such task may involve replacing a string on a specific line number across multiple files. This can be useful in modifying configuration files, updating scripts, or processing data.
In this tutorial, we’ll explore different methods of replacing a string on the nth line in multiple text files using the sed, awk, and perl commands in the Linux terminal.
sed is a text processing tool used to parse and transform text. Additionally, we can use it to perform basic text manipulation such as searching, replacing, and deleting lines on an input stream. In this case, we’ll use it to replace a string on a specific line in multiple files. Furthermore, we’ll use sed in combination with the find command.
Before we begin, let’s check the syntax we’ll use:
$ sed -i 'Ns/old_string/new_string/' filename
Now, let’s use sed to replace a string on the nth line of multiple text files:
$ find . -type f -name '*.txt' -exec sed -i '2s/yahoo/gmail/' {} +
Let’s examine the above command:
Above, we replace the string yahoo with gmail on the second line of each file. Moreover, only the first occurrence of the string yahoo is replaced.
To replace all occurrences of yahoo on the second line, we use the g flag:
$ find . -type f -name '*.txt' -exec sed -i '2s/yahoo/gmail/g' {} +
Here, we add the g flag to the sed command, enabling us to replace multiple occurrences of the string yahoo with gmail on the second line.
Furthermore, we can use multiple sed commands to replace multiple strings on the same line by separating the commands with a semicolon:
$ find . -type f -name "*.txt" -exec sed -i '2s/Email/EmailAddress/; 2s/yahoo/gmail/' {} +
The above command replaces multiple strings on the second line of each file. To explain, 2s/Email/EmailAddress/ replaces the string Email with EmailAddress while 2s/yahoo/gmail/ replaces the string yahoo with gmail.
awk is a command line tool used to process text and extract data. Unlike sed, awk is more flexible, especially when we need to perform complex pattern matching and replacement.
First, let’s check the syntax we’ll use:
$ awk '{ if (NR == N) sub(/old_string/, "new_string"); print }' filename > temp && mv temp filename
Let’s break down the above syntax:
To clarify, since awk doesn’t modify files in place, we need to redirect the output to a temporary file and then move it back.
To demonstrate, let’s replace a specific string on the 6th line:
$ find . -type f -name "*.txt" -exec sh -c 'for file; do awk "{ if (NR == 6) sub(/DOB/, \"Date_Of_Birth\"); print }" "$file" > temp && mv temp "$file"; done' sh {} +
Let’s understand the above command:
Above, we replace the string DOB with Date_Of_Birth on the 6th line of each file.
By default, the sub-function replaces only the first occurrence of the string in a line. So, to replace all occurrences of the string in the nth line, we use the gsub function:
$ find . -type f -name "*.txt" -exec sh -c 'for file; do awk "{ if (NR == 2) gsub(/gmail/, \"yahoo\"); print }" "$file" > temp && mv temp "$file"; done' sh {} +
Here, we replace all occurrences of the string gmail with yahoo on the second line of each file.
In addition, we can replace multiple strings on the same line:
$ find . -type f -name "*.txt" -exec sh -c 'for file; do awk "{ if (NR == 2) { gsub(/EmailAddress/, \"Email\"); gsub(/yahoo/, \"gmail\"); } print }" "$file" > temp && mv temp "$file"; done' sh {} +
Above, we use multiple awk commands separated by a semicolon to replace multiple strings on the same line on each file.
Furthermore, we can perform different actions based on multiple conditions within the same command using awk. For instance, let’s replace specific strings on different lines in multiple files:
$ find . -type f -name "*.txt" -exec sh -c 'for file; do awk "NR == 2 { gsub(/yahoo/, \"gmail\") } NR == 7 { gsub(/AdmissionNumber/, \"AdmnNo\") } { print }" "$file" > temp && mv temp "$file"; done' sh {} +
Let’s explain the above command:
To explain, the above command replaces specific strings in multiple files on the second and seventh lines.
perl is a command line tool used to extract information from text files. Additionally, we can use it to perform complex text manipulations. Here, we’ll use it to replace a string on the nth line.
Firstly, let’s check the syntax we’ll use:
$ perl -i -pe 's/old_string/new_string/ if $. == N' filename
Let’s understand the above syntax:
Now, let’s replace a string on the nth line of multiple files.
To illustrate, we’ll replace a string on the first line of each file. We’ll use perl in combination with the find command:
$ find . -type f -name "*.txt" -exec perl -i -pe 's/Name/FullName/ if $. == 1' {} \;
In the example above, find locates all text files in the current directory and its subdirectories. We then use perl to replace the first occurrence of the string Name in the first line of each file with FullName.
Furthermore, we can replace all occurrences of Name, using the g flag:
$ find . -type f -name "*.txt" -exec perl -i -pe 's/Name/FullName/g if $. == 1' {} \;
The above command replaces all occurrences of the string Name in the first line of each file.
Additionally, we can perform multiple string substitutions on the same line by chaining the substitution commands:
$ find . -type f -name "*.txt" -exec perl -i -pe 's/Email/EmailAddress/g if $. == 2; s/gmail/yahoo/g if $. == 2' {} \;
Above, we replace Email with EmailAddress and gmail with yahoo on the second line of each file.
In this article, we discussed replacing a string on the nth line of multiple files in Linux. We utilized the sed, awk, and perl commands. Further, we combined these commands with the find command to search for text files in the specified directory and its subdirectories.