The Three Musketeers of Linux Commands – Using sed for Search and Append

Introduction to sed

sed is short for Stream Editor, also known as a stream editor. sed works like an assembly line in a workshop, where the characters to be processed are the raw materials on the assembly line, and after processing by sed, they become the finished goods.
sed is mainly used in the following scenarios:
1. Searching and filtering file content
2. Replacing file content
3. Checking if input is purely English or purely numeric in scripts

2. Using the sed Command

sed [options] [sed-commands] [input-file]
sed   [options]       [sed commands]          [input file]

Execution process:

1. sed reads a line from a file or pipe, processes it, and outputs it.
2. It then reads the next line, processes it, and outputs it.

Common options:

-n Suppresses the default output of sed, often used with the p command

-e Allows multiple sed commands to be executed in one line

-r Uses extended regular expressions; by default, sed only recognizes basic regular expressions

-i Modifies the file content directly instead of outputting to the terminal. If the -i option is not used, sed only modifies data in memory and does not affect the file on disk.

Common commands:

a Append text after the specified line

c Replace the specified line

d Delete the specified line

i Insert text before the specified line

p Print content, usually used with -n

s Substitute s#text to be replaced#replacement#g

Special symbols:

! Apply command to all lines except the specified line

= Print the current line number

; Allows multiple sed commands to be executed in one line

sed Practice – Search

Searching with sed mainly uses the combination of -n and p, where -n suppresses default output and p indicates to display only matching lines.Test text:

cat > girs.txt << EOF

I am oldzhang !

I teach linux.

testtt0aaaasssabcc788ayyytt

#I like swimming, football, basketball, video games

I like ChaShao TaiQiu

#my blog is https://www.xxx110.com/alaska/

my site is https://www.abcdef12.com/u/ee1c7fcea5b0

#my-qq is 873195417.

my_phone is 1532000233111112.

EOF

Single Line Search

Search for the second line

The Three Musketeers of Linux Commands - Using sed for Search and Append

Multi-Line Search

Query from the second line to the third line

The Three Musketeers of Linux Commands - Using sed for Search and Append

Keyword Search

Search for lines containing the keyword Cha

### Search by keyword range

Start searching for lines containing the keyword Cha from the third line

The Three Musketeers of Linux Commands - Using sed for Search and Append

Search for all lines between oldzhang and Cha

sed Practice – Append

Appending uses the a and i commands, which add text before or after the specified line, respectively.

Single Line Append

Add a new line “good good study day day up” after the second line

The Three Musketeers of Linux Commands - Using sed for Search and Append

Parameter a is for appending after. i is for inserting before.

Multi-Line Append

Add two lines of text “good good study day day up; you can you up” after the second line, with a newline in between as shown in the image below.The Three Musketeers of Linux Commands - Using sed for Search and Append

sed Practice – Replace

Explanation of the replace parameters

The replacement function of sed is very powerful and is the most commonly used feature of sed.

By default, sed does not actually modify the file; to write the modified content back to the file, the -i parameter must be added.

Command syntax:

sed ‘[address range|pattern range] s#[text to be replaced]#[replacement text]#[flags]’ [input file]

Command explanation:

1. [address range|pattern range]: Optional; if not specified, sed will perform the replacement on all lines.

2. “s” indicates the substitute command.

3. The string to be replaced can be a regular expression.

4. The replacement string must be a specific content.

5. Flags: Optional.

– Global flag g

– Numeric flag (1, 2, 3…)

– Print flag p

– Write flag w

– Ignore case flag i

– Execute command flag e

Flags can be combined as needed.

Stay tuned for the next article!

Leave a Comment