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

Sample Analysis used by Linux sed

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis on the use of Linux sed. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Sed, a flow editor stream editor, is one of the three major file processing tools (awk, sed, grep) in Linux. It can cooperate with regular expressions and plays an important role in file processing.

The sed streaming process is divided into three stages: read, execute, and display.

1. Sed reads read a line from the input stream (file, pipe, standard input) and store it in a pattern space called pattern buffer.

2. Sed executes. By default, all SED commands are executed sequentially in the mode space. Unless the address of the line is specified, the SED command will be executed on all lines in turn.

3. Sed sends the modified content to the output stream. After sending the data, the schema space will be emptied. The above process will be repeated before all the contents of the file are processed.

Sed carries on the text processing by the behavior unit, may carry on the deletion, the replacement, the addition, the selection and so on to the data, matches the regular, the power increases greatly, do not want to give it a try, then we will have a good plate of sed.

one。 Execution process example

Print content

After the sed "test file is read into the pattern space, there is no script to handle it, and the content of the pattern space is output directly to the screen.

Receive input

Sed "receives the standard input and performs script processing. The script here is empty, so it is not processed and output directly to the screen."

II. Two implementation modes of sed

Sed [- n] [- e] 'command (s)' files

Sed [- n]-f scriptfile files

The first is used on the command line, and the second is used in the SED script file; both execution methods can be used at the same time.

File:

$cat test

Aaa

Ddd

two hundred and twenty two

Sss

12ds45

Method 1:

Sed-e '1d'-e' 2d'-e '3d' test

Sss

12ds45

Method 2:

Echo-e "1D\ n2d\ n3D" > commands.txt

Sed-f commands.txt test

Sss

12ds45

III. Sed command line arguments

-n by default, content in mode space is printed to standard output after processing is completed, which is used to prevent this behavior

-e specifies the command to be executed. With this parameter, we can specify multiple commands so that we can print each line twice:

Sed-e "- e 'p' quote.txt

-n,-quiet,-slient: same as the standard-n option

-e script,-expression=script: same as the standard-e option

-f script-file,-file=script-file: same as the standard-f option

-follow-symlinks: if this option is provided, SED will follow the link when the edited file is a symbolic link

-I [SUFFIX],-in-place [= SUFFIX]: this option is used to edit the current file. If SUFFIX is provided, the original file will be backed up, otherwise the original file will be overwritten.

-l N,-line-lenght=N: this option sets the length of the line to N characters

-posix: this option disables all GNU extensions

-rmaine regexp extended: this option will enable extended regular expressions

-u,-unbuffered: when this option is specified, SED will load the least amount of data from the input file and flush it out to the output buffer more frequently. This option is useful when editing the output of the tail-f command and you don't want to wait for the output.

By default, SED splits each line using newline characters. If this option is provided, it will split lines using NULL characters.

four。 Sed basic command

This chapter will explain some commonly used SED commands, including commands such as DELETE,WRITE,APPEND,CHANGE,INSERT,TRANSLATE,QUIT,READ,EXECUTE

1. Delete d

Format: [address1 [, address2]] d

$: sed 'd' test.txt deletes every line in the schema space, leaving the source file unchanged

$: sed '4d' test.txt deletes the fourth line

$: sed '4jue 9d' test.txt deletes lines 4 to 9

$: sed'/ Time/,/Good/d' deletes lines starting with Time and Good

two。 Replace s

Format: [address1 [, address2]] s/pattern/replacement/ [flags]

Sed's pinch test.txt / | / g 'par g: replace all content

Sed's test.txt test.txt 2: replaces only the second comma in each line

Sed-n's go to GenomeUniGen 'test.txt p: output only changed lines

Sed-n 's/genome/gene/w junk.txt' test.txt w: the output after replacement is saved to a new file

Sed-n's Universe GenomeUniGen pi 'test.txt I: match case

3. Write w

Format: [address1 [, address2]] w file

W specifies the write command, and file refers to the file name in which the contents of the file are stored. Be careful when using the file operator, which is automatically created when a file name is provided but the file does not exist, and overwrites the contents of the original file if it already exists.

$: sed-n'w books.bak' books.txt creates a copy of books.txt

4. Regular expression

^ the beginning of the line

$end of line

. Line single character (except at the end of the line)

[] matching character set

[^] exclude character set

[-] character range

?, +, * correspond to 0 to 1, one to many, and 0 to many matches, respectively.

{n}, {n,}, {m, n} exactly match N times, at least N times, match Mmurn N times

| or |

\ s matches a single blank content

\ s matches a single non-blank content.

\ W,\ W single word, non-word.

5. Common code block

a. Remove blank lines

$: echo-e "Line # 1\ n\ n\ nLine # 2" | sed'/ ^ $/ d'

b. Delete consecutive blank lines

$: echo-e "Line # 1\ n\ n\ nLine # 2" | sed'/. /, / ^ $/! d'

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

This is the end of the sample analysis on the use of Linux sed. I hope the above content can be helpful to you and learn more. If you think the article is good, you can 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

Development

Wechat

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

12
Report