How to Get Matching String: Linux Sed Command

Here is a way to print a matching string from an input file. The sed also called stream editor and is helpful for stream editing in the shell scripts.

What is the sed command?

It is a “non-interactive” stream-oriented editor that can use to automate editing via shell scripts. This ability to modify an entire data stream (like how the grep command behaves) as if you were inside an editor is not common in modern programming languages.

Purpose of the sed command

The purpose of the sed command is to print matching lines, delete matching lines, and find/replace [sed command find replace] matching strings or regular expressions.

Sample.txt file

1
2
1111
3
1223
4

Method-1: How to get matching string

cat sample.txt |sed -n "/3/p"

The output

3
1223

Method-2: How to get matching string

sed -n "/3/p" sample.txt

The output

3
1223

In both ways, you got the same output. Here the /3/ is looking for string ‘3’, and ‘p’ is for the matching condition. This way, you can use the sed command in shell scripts.

Related

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.