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

Introduction to the usage of sed command under 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 "introduction to the usage of sed command under Linux". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "introduction to the usage of sed command under Linux".

The Linux sed command details

Sed is a stream editor for filtering and transforming text. Used to perform basic text conversion on an input stream (file or input from a pipe).

Although sed is in some ways similar to an editor that allows script editing, such as ed, it works by passing only one input, so it is more efficient.

Think about:

View the current server IP

[root@cjcos01 cjc] # ifconfig

Although you can view IP through ifconfig, you can print out a lot of information that you don't care about. How to get rid of this useless information?

You can do this through sed plus grep, as shown in the example below.

test data

[root@cjcos01 cjc] # cat t1.txt

Tao flower nunnery song tao flower dock tao flower nunnery, tao flower nunnery under tao flower immortal; tao flower fairy plant tao tree, and pick tao flowers to sell wine money. When you wake up drunk, you only sit in front of the flowers, and when you are drunk, you come to sleep; half awake, half drunk day after day, flowers bloom year after year. I hope the old dead flower wine room, do not want to bow in front of the chariot and horse; chariot dust and horse rich interest, wine flowers and branches of the poor. If you compare the rich and the poor with the poor, one on the flat ground and the other on the sky; if you compare the poor to the chariots and horses, he will drive me free. Others laugh at me for being too crazy, but I laugh at others that I can't see through it. I don't see the tomb of Wuling heroes, making fields with no flowers and no wine hoe.

1 print line

Print the second line

[root@cjcos01 cjc] # sed-n '2p' / cjc/t1.txt

Tao Flower nunnery in tao Huawu, tao Flower Fairy under tao Flower Nunnery

Print lines 2-5

[root@cjcos01 cjc] # sed-n'2j 5p'/ cjc/t1.txt

Tao Flower nunnery in tao Flower Wharf, tao Flower Fairy under tao Flower Nunnery; tao Flower Fairy planting tao tree, and picking tao flowers to sell wine money. When you wake up drunk, you only sit in front of the flowers, and when you are drunk, you come to sleep; half awake, half drunk day after day, flowers bloom year after year.

Print line 10 to the end

[root@cjcos01 cjc] # sed-n'10 recorder p' / cjc/t1.txt

Others laugh at me for being too crazy, but I laugh at others that I can't see through it. I don't see the tomb of Wuling heroes, making fields with no flowers and no wine hoe.

Print line 2, line 6, line 8, line 9, line 10

[root@cjcos01 cjc] # sed-n'2p * 6p * / 10p'/ cjc/t1.txt

Tao flower nunnery in tao flower dock, tao flower nunnery in tao flower nunnery; I hope the old dead flower wine room, do not want to bow in front of the chariot and horse; if you compare wealth to poverty and inferiority, one to heaven; if you compare poverty and inferiority to chariots and horses, he has to drive me at leisure. Others laugh at me for being too crazy, but I laugh at others for not seeing through.

Print a line containing the word tao

[root@cjcos01 cjc] # sed-n'/ tao/p' / cjc/t1.txt

Tao flower nunnery song tao flower dock tao flower nunnery, tao flower nunnery under tao flower immortal; tao flower fairy plant tao tree, and pick tao flowers to sell wine money.

Print the line at the beginning of the word "wine"

[root@cjcos01 cjc] # sed-n'/ ^ wine / p' / cjc/t1.txt

When you wake up drunk, you only sit in front of the flowers, and when you get drunk, you come to sleep.

Print. " The line at the end

[root@cjcos01 cjc] # sed-n'/\. $/ p' / cjc/t1.txt

Tao flower fairies plant tao trees, and pick tao flowers to sell wine money. Half awake and half drunk day after day, flowers bloom year after year. The rickshaw dust and the horse are the rich, and the wine and flowers are the poor. If you compare poverty with horses and chariots, he has to drive me free. No tombs of Wuling heroes, no flowers and no wine hoe to make fields.

2 insert row

[root@cjcos01 cjc] # cp t1.txt t1.txt.bak

People worry too much about the young and old, and flowers for the unworried old. Old and young people don't care, and they get drunk before they get drunk.

Add before the line, write to the source file

[root@cjcos01 cjc] # sed-I'2i A young man with much sorrow is old, and a flower is an old boy without sorrow.' / cjc/t1.txt

[root@cjcos01 cjc] # cat t1.txt

Tao Flower nunnery singers are sorrowful young and old, and flowers are unworried old teenagers. Tao Flower nunnery in tao Huawu, tao Flower Fairy under tao Huayan.

Add after the line (modify the original file directly)

[root@cjcos01 cjc] # sed-I'2a don't care about the old and young, and get the poem drunk.' / cjc/t1.txt

[root@cjcos01 cjc] # cat t1.txt

Tao Flower nunnery singers are sorrowful young and old, and flowers are unworried old teenagers. Old and young people don't care, and they get drunk before they get drunk. Tao Flower nunnery in tao Huawu, tao Flower Fairy under tao Huayan.

3 replace the line (modify the original file directly)

[root@cjcos01 cjc] # sed-I'2c spare time to write and sell it on Castle Peak, not to make money for human iniquity.' / cjc/t1.txt

[root@cjcos01 cjc] # cat t1.txt

Tao Flower nunnery is free to write songs and sell them on Castle Peak, which does not make the world make money. Old and young people don't care, and they get drunk before they get drunk. .

4 replacement character

-n's new old changes the first character of each line in the file into a new character, prints out the line that only changes, and the content of the source file remains the same

[root@cjcos01 cjc] # sed-n 's/tao/ load / p' / cjc/t1.txt

Lotus nunnery song lotus dock tao flower nunnery, tao flower nunnery under tao flower immortal; lotus fairy species tao tree, and pick tao flowers to sell wine money.

-n's old new: replace all the characters in the file with new characters, print out lines that only change, and keep the contents of the source file unchanged.

[root@cjcos01 cjc] # sed-n 's/tao/ load / pg' / cjc/t1.txt

The lotus nunnery, the lotus fairy, the lotus fairy, the lotus tree, the lotus fairy, the lotus tree, the lotus and the wine.

-n's old new p3g': replace each line in the file with the new character starting from the third character, and print out the line that only changes, and the content of the source file remains unchanged.

[root@cjcos01 cjc] # sed-n 's/tao/ load / p3g' / cjc/t1.txt

Tao nunnery in tao dock, lotus nunnery under lotus nunnery; tao flower fairy plant tao tree, and pick lotus to sell wine money.

-I, replace the first old character of each line in the file with the new character, and modify the content of the source file

[root@cjcos01 cjc] # sed-I 's/tao/ load / g' / cjc/t1.txt

[root@cjcos01 cjc] # sed-I's / Netherlands / tao/g' / cjc/t1.txt

5 Delete rows

Delete line 2

[root@cjcos01 cjc] # sed-I '2d' / cjc/t1.txt

Delete lines 3 to 5

[root@cjcos01 cjc] # sed-I '3recovery5d' / cjc/t1.txt

Delete line 2, line 4, line 5, line 6

[root@cjcos01 cjc] # sed-I '2dterter4jue 6d' / cjc/t1.txt

For example:

Example 1: only IP addresses in ifconfig are displayed

[root@cjcos01 ~] # ifconfig | grep "inet" | grep-v "inet6" | grep-v "127.0.0.1" | grep-v "122.1" | sed's Universe netmask.accountUniplash'| sed's / ^. * inet//' 192.168.38.10

Example 2: remove the # and blank lines from the ssh configuration file, do not modify the source file, and print the results to the foreground

[root@cjcos01 cjc] # echo > t1.txt

[root@cjcos01 cjc] # cat / etc/ssh/ssh_config > t1.txt

[root@cjcos01 cjc] # sed's Unip / cjc/t1.txt. Sed'/ ^ $/ d'

Host * GSSAPIAuthentication yesForwardX11Trusted yesSendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESSendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTSendEnv LC_IDENTIFICATION LC_ALL LANGUAGESendEnv XMODIFIERS

Example 3: each line ends with. Replace it with "." Need to add escape character), do not change the source file (specify-I to change the source file)

[root@cjcos01 cjc] # sed-n's /\. $/! / p' / cjc/t1.txt

GSSAPIAuthentication yes!ForwardX11Trusted yes!SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT!

Example 4: add @ at the end of the line starting with H

[root@cjcos01 cjc] # sed-n's / ^ H. * $/ & @ @ / p' / cjc/t1.txt

Host * @

Sed help Information:

[root@cjcos01] # sed-- helpUsage: sed [OPTION]. {script-only-if-no-other-script} [input-file]... -n,-- quiet,-- silent suppress automatic printing of pattern space-e script,-- expression=script add the script to the commands to be executed-f script-file,-- file=script-file add the contents of script-file to the commands to be executed-- follow-symlinks follow symlinks when processing in place-I [SUFFIX] -- in-place [= SUFFIX] edit files in place (makes backup if SUFFIX supplied)-c,-- copy use copy instead of rename when shuffling files in- I mode-b,-- binary does nothing For compatibility with WIN32/CYGWIN/MSDOS/EMX (open files in binary mode (CR+LFs are not treated specially))-l N,-- line-length=N specify the desired line-wrap length for the `l' command-- posix disable all GNU extensions. -r,-- regexp-extended use extended regular expressions in the script. -s,-- separate consider files as separate rather than as a single continuous long stream. -u,-- unbuffered load minimal amounts of data from the input files and flush the output buffers more often-z,-- null-data separate lines by NUL characters-- help display this help and exit-- version output version information and exitIf no-e,-- expression,-f, or-- file option is given, then the firstnon-option argument is taken as the sed script to interpret. Allremaining arguments are names of input files; if no input files arespecified, then the standard input is read.GNU sed home page: .General help using GNU software: .E-mail bug reports to: .be sure to include the word ``sed'' somewhere in the ``Subject:'' field. [root@cjcos01 ~] # man sedNAME sed-stream editor for filtering and transforming textSYNOPSIS sed [OPTION]. {script-only-if-no-other-script} [input-file]... DESCRIPTION Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input (s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.SEE ALSO awk (1), ed (1), grep (1), tr (1), perlre (1), sed.info, any of various books on sed, the sed FAQ (http://sed.sf.net/grabbag/tutorials/sedfaq.txt), http://sed.sf.net/grabbag/.) The full documentation for sed is maintained as a Texinfo manual. If the info and sed programs are properly installed at your site, the command info sed [root@cjcos01 ~] # info sedFile: sed.info, Node: Top, Next: Introduction, Up: (dir) sed, a stream editor*This file documents version 4.2.2 of GNU `sed', a stream editor. Thank you for your reading, the above is the content of "introduction to the usage of sed command under Linux". After the study of this article, I believe you have a deeper understanding of the introduction of the usage of sed command under Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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