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 use the sed command of linux to process editing text files

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Today, the editor shares with you how to use linux's sed command to deal with editing text files. I believe many people don't know much about it. In order to make you understand better, I summarized the following contents for you. Let's look down together. I'm sure you'll get something.

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 format: sed [parameters]

Common parameters:

-e or-- expression= processes the input text file with the script specified in the option or-- file= processes the input text file with the script file specified in the option-h or-- help display help-n or-- quiet or-- silent displays only the script-processed result-V or-- version display version information

Reference example

Add a line after the fourth line of the file file and output the result to standard output:

[root@linuxcool] # sed-e 4a\ newline file HELLO LINUX! Linux is a free unix-type opterating system. This is a linux testfile! Linux test newline

List the contents of / dir and print the line number, while deleting line 2-5:

[root@linuxcool ~] # nl / dir | sed'2 5 days 1 root:x:0:0:root:/root:/bin/bash6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Add two lines of words after the second line, such as Drink tea or. .. With drink beer:

[root@linuxcool ~] # nl / dir | sed'2a Drink tea or.\ > drink beer?'1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologinDrink tea or. Beer? 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

Replace lines 2-5 with No 2-5 number:

[root@linuxcool ~] # nl / dir | sed'2 number'1 root:x:0:0:root:/root:/bin/bashNo 5c No 2-5 number'1 root:x:0:0:root:/root:/bin/bashNo 2-5 number6 sync:x:5:0:sync:/sbin:/bin/sync

To display in units of behavior:

[root@linuxcool ~] # nl / dir | sed-n '5Jing 7paired 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown about how to use linux's sed command to process and edit text files is here. I hope the above content can be of some reference value to you and can be applied. If you like this article, you might as well share it for more people to see.

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