Linux find and replace text in multiple files

Содержание
  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. Linux: how to find and replace text in multiple files
  9. xargs
  10. Fine tuning 1: how to exclude directories while searching
  11. Fine tuning 2: regular expressions
  12. Glass Onion Blog
  13. Cheat sheets, post-its and random notes from the desk of a programmer
  14. Linux Tip: Search & Replace text in multiple files recursively
  15. Share this:
  16. Like this:
  17. Related
  18. 4 thoughts on “ Linux Tip: Search & Replace text in multiple files recursively ”
  19. How do I find and replace text in multiple files in Linux?
  20. How to Find and Replace a String in File Using the sed Command in Linux
  21. What is sed Command
  22. 1) How to Find and Replace the “first” Event of the Pattern on a Line
  23. 2) How to Find and Replace the “Nth” Occurrence of the Pattern on a Line
  24. 3) How to Search and Replace all Instances of the Pattern in a Line
  25. 4) How to Find and Replace the Pattern for all Instances in a Line from the “Nth” Event
  26. 5) Search and Replace the pattern on a specific line number
  27. 6) How to Find and Replace Pattern in a Range of Lines
  28. 7) How to Find and Change the pattern in the Last Line
  29. 8) How to Find and Replace the Pattern with only Right Word in a Line
  30. 9) How to Search and Replaces the pattern with case insensitive
  31. 10) How to Find and Replace a String that Contains the Delimiter Character
  32. 11) How to Find and Replaces Digits with a Given Pattern
  33. 12) How to Find and Replace only two Digit Numbers with Pattern
  34. 13) How to Print only Replaced Lines with the sed Command
  35. 14) How to Run Multiple sed Commands at Once
  36. 15) How to Find and Replace the Entire Line if the Given Pattern Matches
  37. 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

Источник

Linux: how to find and replace text in multiple files

Harness the power of grep and sed.

Often times I need to search and replace a string of text across multiple files in my Linux box. After a bit of research I’ve come up with a nice solution. Assuming that you want to search for the string search through multiple files and replace it with replace , this is the one-liner:

Let me now dissect it and take a quick look at the different tools in use.

grep is a utility for searching for strings through multiple text files. Here I’m invoking it with the following parameters:

  • R — perform a recursive search, also across symbolic links;
  • i — case-insensitive search
  • I — skip binary files. We are working with text, afer all;
  • l — print results as a simple list of file names. This is needed for the next command.

The output of grep is then piped to .

xargs

This is a little command-line utility that takes what receives in input and passes it as argument to another program. So in this example the output of grep is passed to the next command sed as its argument.

sed is a glorious Unix utility that transforms text. In the current snippet I’m using it to replace text with the following parameters:

  • i — replace in file. Remove it for a dry run mode;
  • s/search/replace/g — this is the substitution command. The s stands for substitute (i.e. replace), the g instructs the command to replace all occurrences.

Fine tuning 1: how to exclude directories while searching

You can add the —exclude-dir= parameter to grep if you want to skip a specific directory while searching for files. For example, say you want to skip the tests/ directory:

Exclude multiple directories by wrapping them into curly braces, like so:

Fine tuning 2: regular expressions

Both grep and sed support regular expressions, so you can search with grep given a specific pattern and then replace the text with sed given another one. Take a look at the grep manual and the sed manual for more information.

Источник

Glass Onion Blog

Cheat sheets, post-its and random notes from the desk of a programmer

Linux Tip: Search & Replace text in multiple files recursively

Here is how to find and replace text across multiple files recursively.

The following command will recursively find all files with extension .html starting with the current folder. In each file, the text apple will be replaced with orange.

Share this:

Like this:

4 thoughts on “ Linux Tip: Search & Replace text in multiple files recursively ”

You want to be sure that you take the time that you need to understand how you are going to receive.
The majority of the advantages which one gains while exploiting
these buildings. For once, given steel building used for sale that they are tough.
Arena buildings have always been a popular means
for creating a space from which to run your business.

I like the helpful information you provide in your
articles. I will bookmark your weblog and check again here
frequently. I am quite sure I will learn many new stuff
right here! Best of luck for the next!

Do you mind if I quote a couple of your articles as long as I provide
credit and sources back to your website? My website is in the
exact same niche as yours and my visitors would really benefit from
a lot of the information you present here. Please let me
know if this okay with you. Appreciate it!

Acutallly, sed can edit multple files all by itself; use the option -is on linux or -i on darwin. Unfortunately, not recursive and no way to filter the list of files.

Источник

How do I find and replace text in multiple files in Linux?

Click to read full detail here. People also ask, how do you replace a word in multiple files in Linux?

In the current snippet I’m using it to replace text with the following parameters:

  1. i — replace in file. Remove it for a dry run mode;
  2. s/search/replace/g — this is the substitution command. The s stands for substitute (i.e. replace), the g instructs the command to replace all occurrences.

Also, how do I find and replace in Unix vi EDitor? Searching and Replacing in vi

  1. vi hairyspider. For starters, access vi and a specific file.
  2. /spider. Enter command mode, then type / followed by the text you’re looking for.
  3. to find the first occurrence of the term. Type n to find the next one.

Beside above, how do I find and replace text in multiple files?

Remove all the files you don’t want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you’ll find an option to Replace All in All Opened Documents.

What does $# mean in shell script?

$# Stores the number of command-line arguments that were passed to the shell program. $? So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments. For example, $1 is the first argument and so on.

Источник

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 “6” “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”.

Источник

Читайте также:  Вход без пароля windows 10 нет галочки
Оцените статью