- 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
- linux: how to replace text in file from command line
- using sed
- using awk
- using perl
- using ex mode of vim
- Find and replace text within a file using commands
- 7 Answers 7
- Python
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.
Источник
This post and this website contains affiliate links. See my disclosure about affiliate links.
linux: how to replace text in file from command line
Linux has several different utilities that can perform string or text manipulation. Depending on your environment and the tools that are available, you should be able to easily parse, find and replace any string in a text file. Almost every single text editor comes with a find and replace feature with a UI, so we are going to only explore how it can be done from the command line.
Some of the commonly used utilities are sed, awk, gawk, perl and vim. We will look at several examples of how you could use them in different scenarios. All of these also come with several different command line options.
using sed
sed or stream editor is probably the most well known of all the text manipulation utilities. It provides the most flexibility and ease when it comes to replacing text in large texts. The most simple example of using sed to replace text is:
the -i command line option specifies that the substitution should be done in place. This will modify the text and save back to the original file. The last command line argument is the name of the file.
sed follows a regular expression pattern for substitution that is used by many programs and utilities. In the example above, the s in the command string specifies that it is a substitute command expression. The first string after the forward slash is the string that will be replaced with the new text.
- s : the substitute command
- oldtext : a regular expression that match the text to be replaced
- newtext : the string that will replace the matched string
- g: global or replace all matched instances of the expression.
The sed command can be piped as well. That means you pipe the output of sed through other command or pipe the output of other command through to sed.
using awk
awk and gawk are powerful text processing utilities that almost are programming languages in its own way. We will just use it for simple text replacement but the program is capable of much much more. Being a much more powerful tool, the syntax of awk can be a little challenging than that of sed, but then that is subjective.
Without going into how awk works, we will look at how two functions in awk can perform this substitution. The two functions are sub and gsub. There are both identical for most part but sub will only replace the first occurrence of a string while gsub will replace occurrences of the string. We will use gsub for the reminder of this post.
awk can take input files as arguments as well. If you have the latest versions of awk then you can do in-place editing just like sed. Otherwise you will need to direct the output to another file.
using perl
Perl is a programming language that can be run from the command line interface and can be used for text processing. However, Perl is a general purpose language rather than a text processing language. But simple text substitutions can be done in Perl from the command line or from within scripts.
- -p : this specifies that it needs to loop over the input files
- -e : this allows you to write the one line of script
- -i : this specifies that it is an inline edit
Perl is much more powerful than this, but we are only dealing with simple command line substitutions in this post.
using ex mode of vim
Almost all Linux distros should have the vi editor installed. This means you will also have the ex mode that will allow you to perform text substitution just as you can from within the vi editor.
- -s : suppress any interactive user feedback
- -c : the command follows this
- % : perform command on all lines
- s : substitution command
- g : replace all occurences
- x : write changes back to file
We have not dealt with regular expressions that can be used to match several strings at the same time. In all of the examples above, you can very well use regular expressions instead of the string oldtext to match against multiple strings that can be substituted.
These are just some of the ways you can perform a text replacement from command line in Linux. There are several others which involve piping the output using different commands. It all depends on what exactly you want to search for and replace it with.
Источник
Find and replace text within a file using commands
How can I find and replace specific words in a text file using command line?
7 Answers 7
- sed = Stream EDitor
- -i = in-place (i.e. save back to the original file)
The command string:
- s = the substitute command
- original = a regular expression describing the word to replace (or just the word itself)
- new = the text to replace it with
- g = global (i.e. replace all and not just the first occurrence)
file.txt = the file name
There’s multitude of ways to achieve it. Depending on the complexity of what one tries to achieve with string replacement, and depending on tools with which user is familiar, some methods may be preferred more than others.
In this answer I am using simple input.txt file, which you can use to test all examples provided here. The file contents:
Bash isn’t really meant for text processing, but simple substitutions can be done via parameter expansion , in particular here we can use simple structure $
This small script doesn’t do in-place replacement, meaning that you would have to save new text to new file, and get rid of the old file, or mv new.txt old.txt
Side note: if you’re curious about why while IFS= read -r ; do . done is used, it’s basically shell’s way of reading file line by line. See this for reference.
AWK, being a text processing utility, is quite appropriate for such task. It can do simple replacements and much more advanced ones based on regular expressions. It provides two functions: sub() and gsub() . The first one only replaces only the first occurrence, while the second — replaces occurrences in whole string. For instance, if we have string one potato two potato , this would be the result:
AWK can take an input file as argument, so doing same things with input.txt , would be easy:
Depending on the version of AWK you have, it may or may not have in-place editing, hence the usual practice is save and replace new text. For instance something like this:
Sed is a line editor. It also uses regular expressions, but for simple substitutions it’s sufficient to do:
What’s good about this tool is that it has in-place editing, which you can enable with -i flag.
Perl is another tool which is often used for text processing, but it’s a general purpose language, and is used in networking, system administration, desktop apps, and many other places. It borrowed a lot of concepts/features from other languages such as C,sed,awk, and others. Simple substitution can be done as so:
Like sed, perl also has the -i flag.
Python
This language is very versatile and is also used in a wide variety of applications. It has a lot of functions for working with strings, among which is replace() , so if you have variable like var=»Hello World» , you could do var.replace(«Hello»,»Good Morning»)
Simple way to read file and replace string in it would be as so:
With Python, however, you also need to output to new file , which you can also do from within the script itself. For instance, here’s a simple one:
Источник