How To Use Sed To Find and Replace Text In Files or Standard Input – POFTUT

How To Use Sed To Find and Replace Text In Files or Standard Input


Sed or Stream Editor provides a lot of useful and dynamic functions. One of the most popular use case for sed is search and replace text in standard input output and text files. In this tutorial we will look various use cases of find and replace in sed.

Substitute Command

Sed have different verbs or commands to operate on given files or standard input lines. Substitute or s is one of the most popular sed command.

Syntax

Syntax of sed substitute command is like below.

sed 'PATTERNs/OLD/NEW/FLAGS'  FILENAME
  • PATTERN is used to specify the behaviour of substitute command like one at three.
  • s is the substitute command
  • OLD is the term we are searching for and it may be a regex
  • NEW is the word we will set
  • FLAGS are used to set settings about sed
  • FILENAME is optional if we provide text from standard input

Substitute Word Once

We can change given term into the word we specify with the simple s/ / syntax. In this example we will change the linux into poftut  in file sites.txt . The output will be printed to the standard output and no change will occur in sites.txt

$ sed 's/linux/poftut' sites.txt

Substitute All Words

In previous example we have changes only single match. If we need to change all occurrences of given term we should use g flag. In this example we will change all occurrences of linux to poftut in file sites.txt and print to the terminal.

$ sed 's/linux/poftut/g' sites.txt

Substitute Only 2nd Occurrence of Word

Another useful substitute use case is changing given period matches. In this example we will change every occurrence of 2 the linux with poftut in file sites.txt and print output to shell. We will put 2 as flag which means change 2nd occurences

$ sed 's/linux/poftut/g' sites.txt

Write Output To A File

Upto now we have print output to the shell by default. We can also write changes into new file. We will redirect output to the file named sites2.txt

$ sed 's/linux/poftut/g' sites.txt > sites2.txt

Replace In Place

We can also replace given search. This will write changes to the given source file. We will use -i option.

$ sed -i 's/linux/poftut/g' sites.txt

Find And Replace In Multiple Files

Up to now we have only used single file. We may need to find and replace in multiple files. We can specify the files with glob * according to their names or extensions. In this example we will made replace all file names starts with sites and ends with .txt

$ sed -i 's/linux/poftut/g' sites*.txt

Substitute Only If The Line Matches with Given Pattern

We can set some conditions for the match and replace. We can look other patterns in the current line for find and replace. In this example we will change lines those have number 1 where find linux term and set poftut in file sites.txt and print to console.

$ sed '/1/s/linux/poftut/g' sites.txt

Convert DOS Newlines Carriage Return/Line Feed To Unix Format

We can convert Windows line ends with the following sed script. As we know windows line ends provides Carriage Return and Line Feed.

$ sed 's/.$//' sites.txt

LEARN MORE  How To Use Regex (Regular Expression) with Grep?

Leave a Comment