Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell programming the sed of the three Musketeers

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

sed tools

SED Profile:

sed is a powerful and simple text parsing tool that reads text, edits the text content (deletes, replaces, adds, moves, etc.) according to specified conditions, and finally outputs all lines or only some lines processed. sed can also implement fairly complex text processing operations without interaction, and is widely used in Shell scripts to complete various automated processing tasks.

sed workflow:

Read: sed reads a line from an input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space).

Execution: By default, all sed commands are executed sequentially in the pattern space, and the sed command is executed sequentially on all lines unless a line address is specified.

Display: Send modified content to output stream. After sending data again, the pattern space will be cleared.

sed command format:

sed [options] 'action' parameter

Common sed command options:

-e or--expression=: Indicates that the input text file is processed with a specified command or script.

-f or--file=: Indicates that the input text file is processed with the specified script file.

-h or--help: Displays help.

-n, --quiet, or silent: Indicates that only processed results are displayed.

-i: Edit text files directly.

Common operations:

a: Add, add a line below the current line.

c: Replace, replaces the selected row with the specified content.

d: Delete, delete the selected row.

i: Insert, inserts a specified line above the selected line.

p: Print, if the specified line is specified at the same time, it means printing the specified line; if the line is not specified, it means printing all the contents; if there are non-printed characters, it is output in ASCII code. It is usually used with the "-n" option.

s: Replace, replace the specified character.

Y: Character conversion.

Examples of sed command usage:

1), output qualified text (p)

sed -n 'p' test1.txt //Output everything, equivalent to cat test1.txt

sed -n '3,5p' test1.txt //Output 3~5 lines

sed -n 'p;n' test1.txt //Output all odd lines,n means read the next line of data

sed -n 'n;p' test1.txt //Output all even lines,n means read the next line of data

sed -n '1 ~ 5 {n;p}' test1.txt //Output even lines between lines 1 and 5 (lines 2/4/6)

sed -n '/the/p' test. txt//Output lines containing the

sed -n '4,/the/p' test. txt//Output line 4 to the first line containing the

sed -n '/the/=' test. txt//outputs the line number containing the, equal sign (=) is used to output the line number

sed -n '/\/p' test. txt//Output lines containing the word wood,\represents word boundaries

2)Delete eligible text (d)

nl test1.txt |sed '3,5d' //Delete lines 3 - 5

nl test.txt |sed '/cross/d' //delete rows containing cross

sed '/^[a-z]/d' test. txt//Delete lines that begin with lowercase letters

sed '/.$/ d'test. txt//delete with ". "End of line

3) Replace eligible text

sed 's/the/THE/' test1.txt //Replace the first the in each line with THE

sed 's/l/L/2' test1.txt //Replace the 2nd l in each line with L

sed 's/the/THE/g' test1.txt //Replace all the the in the file with THE

sed 's/^/#/' test1.txt //Insert #at the beginning of each line

sed '/the/s/^/?/ ' test1.txt //insert at the beginning of each line containing the? no.

sed 's/$/EOF/' test1.txt //Insert the string EOF at the end of each line

4) Transfer eligible text

sed '/the/{H;d};$G' test1.txt //Move the line containing the to the end of the file

sed '1 ~ 5 {H;d};13G' test1.txt //Transfer lines 1 - 5 to line 13

sed '3aNew' test1.txt //Insert a new line after line 3, which reads New

sed '/the/aNew' test1.txt //Insert a new line after each line containing the, with the content New

sed '3aNew1\nNew2' test1.txt //Insert multiple lines after line 3,\n in the middle indicates newline

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report