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

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/how-to-use-sed-to-find-and-replace-text-in-files-or-standard-input/
PINTEREST
PINTEREST
INSTAGRAM

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.

  • 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

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.

LEARN MORE  Python Regular Expression Operations - Regex

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

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

Replace In Place

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

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

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.

LEARN MORE  Awk Print Tutorial With Examples

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.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/how-to-use-sed-to-find-and-replace-text-in-files-or-standard-input/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.