Related Tags

community creator
unix
sed

# How to substitute strings in Unix

Eman Kashif

You can substitute strings in Unix with the sed command, a powerful stream editor that can perform a variety of operations on input text files such as insertion, deletion, and substitution.

### Syntax

sed [SCRIPT] [INPUT-FILE]

Let’s look at some examples of substitution that use the sed command. Below is the sample text inputfile.txt that we will be working on.

Hello! My name is John and I am currently doing CS.
CS is a very difficult major, which is why people are afraid of CS.
You need to be good at creating logic.
But I am glad I am doing CS!

### How to substitute a string

To search and replace a string from the input text, you can use the sed command:

$sed 's/CS/computer science/' inputfile.txt The command above parses through the input text, searches for instances of the string CS, and replaces it with computer science. By default, only the first instance of the string in each line is substituted. s specifies that this is a substitution operation, / is the delimiter, 'CS' is the search pattern, and 'computer science' is the replacement string. Hello! My name is John and I am currently doing computer science. computer science is a very difficult major, which is why people are afraid of CS. You need to be good at creating logic. But I am glad I am doing computer science! ### How to substitute a particular occurrence of a string To search and replace a particular instance of a string, we use /n flags to specify that only the $n$th occurrence of the search pattern should be replaced: $sed 's/CS/computer science/2' inputfile.txt

The command above only replaces the second occurrence of 'CS' with 'computer science'.

Hello! My name is John and I am currently doing CS.
computer science is a very difficult major, which is why people are afraid of CS.
You need to be good at creating logic.
But I am glad I am doing CS!

### How to substitute all instances of a string

To search and replace all occurrences of a search pattern, we can use the /g flag:

$sed 's/CS/computer science/g' inputfile.txt The command above searches for all instances of the search pattern and replaces them with the replacement string. Hello! My name is John and I am currently doing computer science. computer science is a very difficult major, which is why people are afraid of computer science. You need to be good at creating logic. But I am glad I am doing computer science! ### How to substitute from $n$th to all instances of a string To substitute from the $n$th to all occurrences of the search pattern, you can use a combination of /n and /g. n specifies from what occurrence substitution should start. $sed 's/CS/computer science/2g' inputfile.txt

The command above searches for and replaces all instances of 'CS', starting from the 2nd one.

Hello! My name is John and I am currently doing CS.
computer science is a very difficult major, which is why people are afraid of computer science.
You need to be good at creating logic.
But I am glad I am doing computer science!

RELATED TAGS

community creator
unix
sed

CONTRIBUTOR

Eman Kashif
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time