- 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
- Syntax: sed find and replace text
- Examples that use sed to find and replace
- sed command problems
- How to use sed to match word and perform find and replace
- Recap and conclusion – Using sed to find and replace text in given files
- Linux: how to find and replace text in multiple files
- xargs
- Fine tuning 1: how to exclude directories while searching
- Fine tuning 2: regular expressions
- Rushi’s Ramblings
- Linux / Unix: Find And Remove Files With One Command On Fly
- Find And Remove Files With One Command On Fly
- Examples of find command
- Conclusion
- How to Find and Replace a String in File Using the sed Command in Linux
- What is sed Command
- 1) How to Find and Replace the “first” Event of the Pattern on a Line
- 2) How to Find and Replace the “Nth” Occurrence of the Pattern on a Line
- 3) How to Search and Replace all Instances of the Pattern in a Line
- 4) How to Find and Replace the Pattern for all Instances in a Line from the “Nth” Event
- 5) Search and Replace the pattern on a specific line number
- 6) How to Find and Replace Pattern in a Range of Lines
- 7) How to Find and Change the pattern in the Last Line
- 8) How to Find and Replace the Pattern with only Right Word in a Line
- 9) How to Search and Replaces the pattern with case insensitive
- 10) How to Find and Replace a String that Contains the Delimiter Character
- 11) How to Find and Replaces Digits with a Given Pattern
- 12) How to Find and Replace only two Digit Numbers with Pattern
- 13) How to Print only Replaced Lines with the sed Command
- 14) How to Run Multiple sed Commands at Once
- 15) How to Find and Replace the Entire Line if the Given Pattern Matches
- 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:
- Use Stream EDitor (sed) as follows:
- sed -i ‘s/old-text/new-text/g’ input.txt
- The s is the substitute command of sed for find and replace
- It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.txt
- Verify that file has been updated:
- 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.
Источник
Rushi’s Ramblings
I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.
find . -name «*.php» -print | xargs sed -i ‘s/foo/bar/g’
It looks a bit complicated but its quite simple. There are three components to the command:
- find . -name «*.php» -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:
- xargs – This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed - sed -i ‘s/foo/bar/g’ – aka Stream Editor is a tool which should be in every sys admin’s toolkit. In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.
This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.
Источник
Linux / Unix: Find And Remove Files With One Command On Fly
However, the rm command does not support search criteria. For example, find all “*.bak” files and delete them. For such necessities, you need to use the find command to search for files in a directory and remove them on the fly. You can combine find and rm command together. This page explains how to find and remove files with one command on fly.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | find command |
Est. reading time | 4 minutes |
Find And Remove Files With One Command On Fly
The basic find command syntax is as follows:
find dir-name criteria action
Where,
- dir-name : – Defines the working directory such as look into /tmp/
- criteria : Use to select files such as “*.sh”
- action : The find action (what-to-do on file) such as delete the file.
You want to remove multiple files such as ‘*.jpg’ or ‘*.sh’ with one command find, try:
find . -name «FILE-TO-FIND» -exec rm -rf <> \;
OR
find /dir/to/search/ -type f -name «FILE-TO-FIND-Regex» -exec rm -f <> \;
The only difference between above two syntax is that the first command remove directories as well where second command only removes files. Where, options are as follows:
- 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 ➔
- -name «FILE-TO-FIND» : File pattern.
- -exec rm -rf <> \; : Delete all files matched by file pattern.
- -type f : Only match files and do not include directory names.
- -type d : Only match dirs and do not include files names.
Modern version of find command has -delete option too. Instead of using the -exec rm -rf <> \; , use the -delete to delete all matched files. We can also explicitly pass the -depth option to the find to process each directory’s contents before the directory itself. It is also possible to use the -maxdepth option to control descend at most levels of directories below the starting-points. For example, -maxdepth 0 means only apply the tests and actions to the starting-points themselves. Similary, we can pass the -mindepth to the find. It means do not apply any tests or actions at levels less than levels (a non-negative integer). For exampe, -mindepth 1 means process all files except the starting-points. So here is a simplied syntax:
find /dir/to/search/ -type f -name «FILES-TO-FIND» -delete
find /dir/to/search/ -type f -name «FILES-TO-FIND» -depth -delete
find /dir/to/search/ -maxdepth 2 -type f -name «FILES-TO-FIND» -depth -delete
Examples of find command
Find all files having .bak (*.bak) extension in the current directory and remove them:
find . -type f -name «*.bak» -exec rm -f <> \;
OR
find . -type f -name «*.bak» -delete
Find all core files in the / (root) directory and remove them (be careful with this command):
# find / -name core -exec rm -f <> \;
### OR ###
# find / -name core -delete
Find all *.bak files in the current directory and removes them with confirmation from user:
$ find . -type f -name «*.bak» -exec rm -i <> \;
Sample outputs:
The -delete always works better because it doesn’t have to spawn an external process such as rm for every matched file. However, the -delete option may not available on all find versions. Hence, we can use the xargs command as follows too:
find . -type f -name «*.err» -print0 | xargs -I <> -0 rm -v «<>»
Where the find command option is as follows:
- -print0 – Force find command to print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.
And the xargs command options are as follows:
- -I <> : Replace occurrences of <> in the initial-arguments with names read from standard input. We pass <> as arg to the rm command.
- -0 : Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
- rm -v «<>« : Run rm command on matched files.
Conclusion
Read the following man pages using the man command:
man find
man xargs
man rm
For detailed information on find command please see finding/locating files with find command part # 1, Part # 2.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
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 “1” “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”.
Источник