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

How to understand the Linux sed command

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

Share

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

This article is about how to understand the Linux sed command, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Detailed explanation of Linux sed command

The Linux sed command uses scripts to process text files.

Sed can process and edit text files according to the instructions of the script.

Sed is mainly used to edit one or more files automatically, simplify the repeated operation of files, write conversion programs, and so on.

Syntax:

Sed [- hnV] [- e] [- f] [text file]

Parameter description:

-e or-- expression= processes the input text file with the script specified in the option.

-f or-- file= processes the input text file with the script file specified in the option.

-h or-- help displays help.

-n or-- quiet or-- silent displays only the results after script processing.

-V or-- version displays version information.

Action description:

A: added, a can be followed by strings, and these strings will appear on the new line (the current next line) ~

C: instead, c can be followed by strings, which can replace the lines between N1 and N2!

D: delete, because it is deleted, so there is usually no knock after d.

I: insert, I can be followed by strings, and these strings will appear on the new line (the current previous line)

P: print, that is, the selected data is printed out. Usually p runs with the parameter sed-n ~

S: replace, you can directly carry out the work of replacement! Usually the action of this s can be matched with the formal representation! For example, 1Magazine 20s Universe, OldCompact newscop g is it!

Metacharacter set:

^ specify the start of the line

$specify the end of the line

. Matches a non-newline character

* match zero or more characters

[] matches any character within the specified character

[^] matches any character that is not within the specified character

.. Save matched characters

& s/super/YY&yy/ super becomes YYsuperyy & save search characters to replace other characters

\

< 词首定位符 /\ 词尾定位符 /my\>

/ match lines that contain words ending in my

X\ {m\} m consecutive x / 9\ {5\} / matches rows containing 5 consecutive 9s

X\ {m,\} at least m x / 9\ {5,\} / match lines containing at least 5 consecutive 9s

X\ {mdirection n\} at least m, but not more than n x / 9\ {5pm 7\} / matching contains 5 to 7 consecutive 9s

Example:

Add a line after the fourth line of the testfile file and output the result to standard output, and enter the following command at the command line prompt:

Sed-e 4a\ hellowolrd testfile

View the contents of the testfile:

[root@127-0-0-1 yoon] # cat testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

Good good study!

Linux test

After using the sed command, the output is as follows:

[root@127-0-0-1 yoon] # sed-e 4a\ helloworld testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

Good good study!

Helloworld

Linux test

[root@127-0-0-1 yoon] # sed-e'4a\ hello world' testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

Good good study!

Hello world

Linux test

Addition and deletion of behavior units

Delete

List the contents of the testfile and print the line number while deleting lines 4-5

[root@127-0-0-1 yoon] # nl testfile.txt

1HELLO LINUX!

2Linux is a free unix-type opterating system.

3This is a linux testfile!

4good good study!

5hello world

6Linux test

[root@127-0-0-1 yoon] # nl testfile.txt | sed '4meme 5d'

1HELLO LINUX!

2Linux is a free unix-type opterating system.

3This is a linux testfile!

6Linux test

The action of sed is to delete 4-5 rows because 4-5 rows have been deleted, so the data displayed does not have 4-5 rows. In addition, sed-e should have been issued, and it is OK without-e. Colleagues note that the actions followed by sed must be expanded with''two single quotation marks.

Delete only the second line:

[root@127-0-0-1 yoon] # nl testfile.txt | sed '2d'

1HELLO LINUX!

3This is a linux testfile!

4good good study!

5hello world

6Linux test

Delete the third line to the last line:

[root@127-0-0-1 yoon] # nl testfile.txt | sed'3 yoon ($represents the last line)

1HELLO LINUX!

2Linux is a free unix-type opterating system.

Add

Add the word drink milk after the second line (that is, the third line):

[root@127-0-0-1 yoon] # nl testfile.txt | sed'2a drink milk'

1HELLO LINUX!

2Linux is a free unix-type opterating system.

Drink milk

3This is a linux testfile!

4good good study!

5hello world

6Linux test

If you add before the second line:

[root@127-0-0-1 yoon] # nl testfile.txt | sed'2i drink milk'

1HELLO LINUX!

Drink milk

2Linux is a free unix-type opterating system.

3This is a linux testfile!

4good good study!

5hello world

6Linux test

If you are adding more than two lines, add two lines after the second line, for example, drink milk,eat pig:

[root@127-0-0-1 yoon] # nl testfile.txt | sed'2a drink milk\ (new lines must be added with a backslash "\" between each line)

> eat pig'

1HELLO LINUX!

2Linux is a free unix-type opterating system.

Drink milk

Eat pig

3This is a linux testfile!

4good good study!

5hello world

6Linux test

[root@127-0-0-1 yoon] # nl testfile.txt | sed'2a drink milk\ neat pig' (\ nnewline character)

1HELLO LINUX.

2This is a linux testfile.

Drink milk

Eat pig

3good good study.

4hello world.

5Linux test.

6#This is good!

If you append a line, you do not need a newline character\ n. You only need a newline character if you append multiple lines, and there is no need to add a newline character on the last line. If you add it, there will be an extra space.

Add a line to the fourth line:

[root@127-0-0-1 yoon] # sed-e'4a new world' testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

New world

Linux test.

# This is good!

Append two lines to the fourth line:

[root@127-0-0-1 yoon] # sed-e'4a new world\ nold world' testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

New world

Old world

Linux test.

# This is good!

Append three lines (two lines of text and one space) to the fourth line:

[root@127-0-0-1 yoon] # sed-e'4a new world\ nold world\ n 'testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

New world

Old world

Linux test.

# This is good!

Add a blank line:

[root@127-0-0-1 yoon] # sed-e'4a\\ 'testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

Linux test.

# This is good!

Add two blank lines:

[root@127-0-0-1 yoon] # sed-e'4a\\ n 'testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

Linux test.

# This is good!

Replace and display with behavior units

Replace lines 3-4 with No 2-5 number

[root@127-0-0-1 yoon] # nl testfile.txt | sed'2 No 5c No 2-5 number' (this method can replace the whole line)

1HELLO LINUX!

No 2-5 number

6Linux test

List only lines 2-3:

[root@127-0-0-1 yoon] # nl testfile.txt | sed-n '2p'

2Linux is a free unix-type opterating system.

3This is a linux testfile!

Search and display of data

Lines that search for the linux keyword:

[root@127-0-0-1 yoon] # nl testfile.txt | sed'/ linux/p' (if linux finds it, it will match output lines in addition to all output lines)

1HELLO LINUX!

2Linux is a free unix-type opterating system.

3This is a linux testfile!

3This is a linux testfile!

4good good study!

5hello world

6Linux test

When using-n, only the lines containing templates are output.

[root@127-0-0-1 yoon] # nl testfile.txt | sed-n'/ linux/p'

3This is a linux testfile!

Search and delete data

Delete the line in testfile that contains linux, and the other lines output:

[root@127-0-0-1 yoon] # nl testfile.txt | sed'/ linux/d'

1HELLO LINUX!

2Linux is a free unix-type opterating system.

4good good study!

5hello world

6Linux test

Search for data and execute commands

Search for the line corresponding to linux, execute the commands in curly braces, separate each command with a semicolon, and replace testfile with newtestfile, and then output this line:

[root@127-0-0-1 yoon] # nl testfile.txt | sed-n'/ linux/ {t _ r _ r

3This is a linux newtestfile!

Search and replace of data

In addition to the whole row processing mode, sed can also search and replace part of the data with behavior units. Basically, the search for sed is quite similar to the alternative vi, a bit like this:

Sed's / string to be replaced / new string / g'

View the contents of the newtestfile file:

[root@127-0-0-1 yoon] # cat newtestfile.txt

Eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84

Inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

The ip of this machine is 192.168.1.100, delete the front part of the IP:

[root@127-0-0-1 yoon] # cat newtestfile.txt | grep 'inet addr:' | sed's / ^. * addr://g'

192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

The next step is to delete the subsequent part, that is, 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0, and delete the following part of the IP:

[root@127-0-0-1 yoon] # cat newtestfile.txt | grep 'inet addr:' | sed's / ^. * addr://g' | sed's

192.168.1.100

Multipoint editing sed-e 'cmd'-e' cmd' filename

A sed command that deletes the data from the third line to the end and replaces opterating with newopterating:

[root@127-0-0-1 yoon] # nl testfile.txt

1HELLO LINUX!

2Linux is a free unix-type opterating system.

3This is a linux testfile!

4good good study!

5hello world

6Linux test

Multiple sed:

[root@127-0-0-1 yoon] # nl testfile.txt | sed'3 _

1HELLO LINUX!

2Linux is a free unix-type newopterating system.

A sed:

[root@127-0-0-1 yoon] # nl testfile.txt | sed-e '3recoveryd'-e's UniversalopteratingGreater'newopteratingUniverse'

1HELLO LINUX!

2Linux is a free unix-type newopterating system.

(- e represents multipoint editing, the first-e edits the third line to the last line to delete, and the second-e search opterating is replaced by newopterating)

Modify the contents of the file directly

End each line in the testfile file with. Change it to! :

[root@127-0-0-1 yoon] # cat testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

Linux test.

[root@127-0-0-1 yoon] # sed's /\. $/\! / g 'testfile.txt

HELLO LINUX!

This is a linux testfile!

Good good study!

Hello world!

Linux test!

Type # This is good directly on the last line using sed!

[root@127-0-0-1 yoon] # sed-I'$a # This is good!' Testfile.txt

[root@127-0-0-1 yoon] # cat testfile.txt

HELLO LINUX.

This is a linux testfile.

Good good study.

Hello world.

Linux test.

The above is how to understand the Linux sed command, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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