Linux change string in file

Содержание
  1. How to use sed to find and replace text in files in Linux / Unix shell
  2. Find and replace text within a file using sed command
  3. Syntax: sed find and replace text
  4. Examples that use sed to find and replace
  5. sed command problems
  6. How to use sed to match word and perform find and replace
  7. Recap and conclusion – Using sed to find and replace text in given files
  8. How to Replace a String in a File in Bash
  9. Replace String in a File with the `sed` Command
  10. Example 1: Replace File with the ‘sed’ Command
  11. Example 2: Replace File with the ‘sed’ Command with ‘g’ and ‘i’ Flag
  12. Example 3: Replace File with ‘sed’ Command and Matching Digit Pattern
  13. Replace String in a File with `awk` Command
  14. Example 4: Replace File with ‘awk’ Command
  15. Conclusion
  16. About the author
  17. Fahmida Yesmin
  18. How to Find and Replace a String in File Using the sed Command in Linux
  19. What is sed Command
  20. 1) How to Find and Replace the “first” Event of the Pattern on a Line
  21. 2) How to Find and Replace the “Nth” Occurrence of the Pattern on a Line
  22. 3) How to Search and Replace all Instances of the Pattern in a Line
  23. 4) How to Find and Replace the Pattern for all Instances in a Line from the “Nth” Event
  24. 5) Search and Replace the pattern on a specific line number
  25. 6) How to Find and Replace Pattern in a Range of Lines
  26. 7) How to Find and Change the pattern in the Last Line
  27. 8) How to Find and Replace the Pattern with only Right Word in a Line
  28. 9) How to Search and Replaces the pattern with case insensitive
  29. 10) How to Find and Replace a String that Contains the Delimiter Character
  30. 11) How to Find and Replaces Digits with a Given Pattern
  31. 12) How to Find and Replace only two Digit Numbers with Pattern
  32. 13) How to Print only Replaced Lines with the sed Command
  33. 14) How to Run Multiple sed Commands at Once
  34. 15) How to Find and Replace the Entire Line if the Given Pattern Matches
  35. 16) How to Search and Replace lines that Matches a Pattern

How to use sed to find and replace text in files in Linux / Unix shell

Find and replace text within a file using sed command

The procedure to change the text in files under Linux/Unix using sed:

  1. Use Stream EDitor (sed) as follows:
  2. sed -i ‘s/old-text/new-text/g’ input.txt
  3. The s is the substitute command of sed for find and replace
  4. It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.txt
  5. Verify that file has been updated:
  6. more input.txt

Let us see syntax and usage in details.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements sed utility on Linux, macOS or Unix-like OS
Est. reading time 4 minutes

Syntax: sed find and replace text

The syntax is:
sed ‘s/word1/word2/g’ input.file
## *bsd/macos sed syntax#
sed ‘s/word1/word2/g’ input.file > output.file
sed -i ‘s/word1/word2/g’ input.file
sed -i -e ‘s/word1/word2/g’ -e ‘s/xx/yy/g’ input.file
## use + separator instead of / ##
sed -i ‘s+regex+new-text+g’ file.txt
The above replace all occurrences of characters in word1 in the pattern space with the corresponding characters from word2.

Examples that use sed to find and replace

Let us create a text file called hello.txt as follows:
$ cat hello.txt
The is a test file created by nixCrft for demo purpose.
foo is good.
Foo is nice.
I love FOO.

I am going to use s/ for substitute the found expression foo with bar as follows:
sed ‘s/foo/bar/g’ hello.txt
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Please note that the BSD implementation of sed (FreeBSD/MacOS and co) does NOT support case-insensitive matching. You need to install gnu sed. Run the following command on Apple Mac OS:
$ brew install gnu-sed
######################################
### now use gsed command as follows ##
######################################
$ gsed -i ‘s/foo/bar/g I ‘ hello.txt
$ cat hello.txt

sed command problems

Consider the following text file:
$ cat input.txt
http:// is outdate.
Consider using https:// for all your needs.

Find word ‘http://’ and replace with ‘https://www.cyberciti.biz’:
sed ‘s/ http:// / https://www.cyberciti.biz /g’ input.txt
You will get an error that read as follows:

Our syntax is correct but the / delimiter character is also part of word1 and word2 in above example. Sed command allows you to change the delimiter / to something else. So I am going to use +:
sed ‘s+ http:// + https://www.cyberciti.biz +g’ input.txt
Sample outputs:

How to use sed to match word and perform find and replace

In this example only find word ‘love’ and replace it with ‘sick’ if line content a specific string such as FOO:
sed -i -e ‘/FOO/s/love/sick/’ input.txt
Use cat command to verify new changes:
cat input.txt

Recap and conclusion – Using sed to find and replace text in given files

The general syntax is as follows:
## find word1 and replace with word2 using sed ##
sed -i ‘s/word1/word2/g’ input
## you can change the delimiter to keep syntax simple ##
sed -i ‘s+word1+word2+g’ input
sed -i ‘s_word1_word2_g’ input
## you can add I option to GNU sed to case insensitive search ##
sed -i ‘s/word1/word2/gI’ input
sed -i ‘s_word1_word2_gI’ input

See BSD(used on macOS too) sed or GNU sed man page by typing the following command:
man sed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Replace a String in a File in Bash

Sales.txt

Date Amount Area

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

Replace String in a File with the `sed` Command

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

Syntax

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

Output

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

Output

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/\b9\<5\>\b/$&/g’ $1
fi

Output

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′ $1 > temp.txt && mv temp.txt $1
fi

Output

Conclusion

This article showed you how to use bash scripts to replace particular strings in a file. The task to replace a string in a file should become easier for you after practicing the above examples.

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.

Источник

How to Find and Replace a String in File Using the sed Command in Linux

When you are working on text files you may need to find and replace a string in the file.

Sed command is mostly used to replace the text in a file.

This can be done using the sed command and awk command in Linux.

In this tutorial, we will show you how to do this using the sed command and then show about the awk command.

What is sed Command

Sed command stands for Stream Editor, It is used to perform basic text manipulation in Linux. It could perform various functions such as search, find, modify, insert or delete files.

Also, it’s performing complex regular expression pattern matching.

It can be used for the following purpose.

  • To find and replace matches with a given format.
  • To find and replace specific lines that match a given format.
  • To find and replace the entire line that matches the given format.
  • To search and replace two different patterns simultaneously.

The fifteen examples listed in this article will help you to master in the sed command.

If you want to remove a line from a file using the Sed command, go to the following article.

Note: Since this is a demonstration article, we use the sed command without the -i option, which removes lines and prints the contents of the file in the Linux terminal.

But if you want to remove lines from the source file in the real environment, use the -i option with the sed command.

Common Syntax for sed to replace a string.

First we need to understand sed syntax to do this. See details about it.

  • sed: It’s a Linux command.
  • -i: It’s one of the option for sed and what it does? By default sed print the results to the standard output. When you add this option with sed then it will edit files in place. A backup of the original file will be created when you add a suffix (For ex, -i.bak
  • s: The s is the substitute command.
  • Search_String: To search a given string or regular expression.
  • Replacement_String: The replacement string.
  • g: Global replacement flag. By default, the sed command replaces the first occurrence of the pattern in each line and it won’t replace the other occurrence in the line. But, all occurrences will be replaced when the replacement flag is provided
  • / Delimiter character.
  • Input_File: The filename that you want to perform the action.

Let us look at some examples of commonly used with sed command to search and convert text in files.

We have created the below file for demonstration purposes.

1) How to Find and Replace the “first” Event of the Pattern on a Line

The below sed command replaces the word unix with linux in the file. This only changes the first instance of the pattern on each line.

2) How to Find and Replace the “Nth” Occurrence of the Pattern on a Line

Use the /1,/2. /n flags to replace the corresponding occurrence of a pattern in a line.

The below sed command replaces the second instance of the “unix” pattern with “linux” in a line.

3) How to Search and Replace all Instances of the Pattern in a Line

The below sed command replaces all instances of the “unix” format with “Linux” on the line because “g” means a global replacement.

4) How to Find and Replace the Pattern for all Instances in a Line from the “Nth” Event

The below sed command replaces all the patterns from the “Nth” instance of a pattern in a line.

5) Search and Replace the pattern on a specific line number

You can able to replace the string on a specific line number. The below sed command replaces the pattern “unix” with “linux” only on the 3rd line.

6) How to Find and Replace Pattern in a Range of Lines

You can specify the range of line numbers to replace the string.

The below sed command replaces the “Unix” pattern with “Linux” with lines 1 through 3.

7) How to Find and Change the pattern in the Last Line

The below sed command allows you to replace the matching string only in the last line.

The below sed command replaces the “Linux” pattern with “Unix” only on the last line.

8) How to Find and Replace the Pattern with only Right Word in a Line

As you might have noticed, the substring “linuxunix” is replaced with “linuxlinux” in the 6th example. If you want to replace only the right matching word, use the word-boundary expression “\b” on both ends of the search string.

9) How to Search and Replaces the pattern with case insensitive

Everyone knows that Linux is case sensitive. To make the pattern match with case insensitive, use the I flag.

10) How to Find and Replace a String that Contains the Delimiter Character

When you search and replace for a string with the delimiter character, we need to use the backslash “\” to escape the slash.

In this example, we are going to replaces the “/bin/bash” with “/usr/bin/fish”.

The above sed command works as expected, but it looks bad. To simplify this, most of the people will use the vertical bar “|”. So, I advise you to go with it.

11) How to Find and Replaces Digits with a Given Pattern

Similarly, digits can be replaced with pattern. The below sed command replaces all digits with “8” “number” pattern.

12) How to Find and Replace only two Digit Numbers with Pattern

If you want to replace the two digit numbers with the pattern, use the sed command below.

13) How to Print only Replaced Lines with the sed Command

If you want to display only the changed lines, use the below sed command.

  • p – It prints the replaced line twice on the terminal.
  • n – It suppresses the duplicate rows generated by the “p” flag.

14) How to Run Multiple sed Commands at Once

The following sed command detect and replaces two different patterns simultaneously.

The below sed command searches for “linuxunix” and “CentOS” pattern, replacing them with “LINUXUNIX” and “RHEL8” at a time.

The following sed command search for two different patterns and replaces them with one string at a time.

The below sed command searches for “linuxunix” and “CentOS” pattern, replacing them with “Fedora30” at a time.

15) How to Find and Replace the Entire Line if the Given Pattern Matches

If the pattern matches, you can use the sed command to replace the entire line with the new line. This can be done using the “C” flag.

16) How to Search and Replace lines that Matches a Pattern

You can specify a pattern for the sed command to fit on a line. In the event of pattern matching, the sed command searches for the string to be replaced.

The below sed command first looks for lines that have the “OS” pattern, then replaces the word “Linux” with “ArchLinux”.

Источник

Читайте также:  Каталина mac os требования
Оцените статью