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

What are the ways to use the sed command in linux

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "what are the methods of using the sed command in linux", the explanation in the article is simple and clear, easy to learn and understand, please follow the ideas of Xiaobian slowly, together to study and learn "what are the methods of using the sed command in linux"!

The Linux SED command:

1 First Known SED

The sed editor is called a stream editor and, unlike interactive text editors like vim, manipulates data in a stream according to commands. Does the following:

Read data from input one row at a time (repeat until all rows have been read)

Match data according to editor commands

Modify the data in the data stream as commanded

Output new data to STDOUT

The sed command can be entered from the command line or read from a command file.

sed -e script adds the command specified in script sed -f file adds the command specified in file

1.1 Define sed command on command line

Let's look at a simple example of text substitution:

echo "hi,my name is xxx" |sed 's/xxx/kinsomy/'#modify file sed 's/xxx/kinsomy/' data.txt #execute multiple commands with-e option, semicolon separated sed 's/xxx/kinsomy/; s/** */hhh/' data.txt

Pipe the echo output data into sed, and replace it with the s command, replacing the matching data after the first slash with the data after the second slash.

Note: sed manipulates the data in the text file, only outputs the modified data to STDOUT, but does not modify the data in the file itself.

1.2 Read command from file

Define a series of commands in a file script.sed for easy reuse.

s/*/as/x/bs/-/+# -f option specifies command file sed -f script.sed data.txt

2 sed basis

2.1 substitute tags

echo "hi,my name is xxx"| sed 's/xxx/kinsomy/' replaces only the first match in each row, but not all matches in a row.

echo "hi,my name is xxx, i am xxx" |sed 's/xxx/kinsomy/'#Type hi,my name is kinsomy, i am xxx

At this time, you can use some substitution flag to set the substitution mode. The replacement tag follows the replacement string.

s/pattern/replacement/flags

A number indicating which number of matched data will be replaced.

echo "hi,my name is xxx, i am xxx" |sed 's/xxx/kinsomy/2'#output second xxx replaced with kinsomyhi,my name is xxx, i am kinsomy

g, means replace all matched data

echo "hi,my name is xxx, i am xxx" |sed 's/xxx/kinsomy/g'#output the second xxx is replaced with kinsomyhi,my name is kinsomy, i am kinsomy

p, which means the matched lines will be printed

echo "hi,my name is xxx, i am xxx" |sed 's/xxx/kinsomy/p'#output hi,my name is kinsomy, i am xxxhi,my name is kinsomy, i am xxxw, save the output after replacement to the specified file echo "hi,my name is xxx, i am xxx"| sed 's/xxx/kinsomy/w output.txt'

2.3 row addressing

As mentioned above, the sed command reads the text line by line and performs matching operations on the data until all lines have been traversed. If we want to do operations on specific lines at this time, we must use line addressing. Row addressing takes two forms:

Numeric representation of rows

#Operation single line $ sed '2s/xxx/** */'data.txt #operation line interval [2,4]$ sed '2,4s/xxx/** */'data.txt #start to end [2,endline]$ sed '2,$s/xxx/** */' data.txt

Text mode filter

First find the record for kinsomy in/etc/passwd and replace bash with csh. Text pattern filters can be powerful with regular expressions.

sed '/kinsomy/s/bash/csh/' /etc/passwd

2.4 delete rows

Delete text using the delete command d.

#Delete all text sed 'd' data.txt #Delete single line sed '2d' data.txt #Delete line interval [2,3]sed '2,3d' data.txt #Delete line interval [2,endline]sed '2,$d' data.txt #Delete line matching text aa sed '/aa/d' data.txt #Delete text between two matching texts Text between lines matching 1 and 3 All text deleted sed '/1/,/3/d' data.txt

2.5 Insert, append text

insert command i adds a new line before the specified line

append command a adds a new line after the specified line

#Add echo "Line 2"| sed 'a\pipe quote> Line 1'#insert echo "Line 2"| sed 'i\pipe quote> Line 1'

2.6 modify a row

The c command is used to modify a line of data

#Change the third line of text sed '3c\pipe quote> change line ' data.txt

2.7 conversion command

Command format sed 'y/inchar/outchar'

Convert characters in inchar to characters in outchar one by one

echo "This 1 is a test of 1 try. " |sed 'y/123/456/'#Output This 4 is a test of 4 try. Thank you for reading, the above is the "linux sed command in the use of what" content, after the study of this article, I believe that everyone on linux sed command in the use of what this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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