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

The usage of 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 "the usage of sed command in linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of the sed command in linux.

Sed is a good file processing tool. It is a pipeline command itself, which is mainly processed in a behavior unit. Data rows can be replaced, deleted, added, selected and other specific tasks. Let's take a look at the usage of sed.

The format of the sed command line is:

Sed [- nefri] 'command' input text

Common options:

-n ∶ uses quiet (silent) mode. In the normal use of sed, all data from STDIN is usually listed on the screen. But if you add the-n argument, only the row (or action) that has been specially handled by sed will be listed.

-e ∶ edits sed actions directly in instruction line mode

-f ∶ directly writes sed actions in a file, while-f filename can perform sed actions in filename.

The action of-r ∶ sed supports the syntax of extended regular representations. (the preset is the basic formal representation syntax)

-I ∶ directly modifies the contents of the read file instead of outputting it on the screen.

Common commands:

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

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

D ∶ is deleted, 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 a new line (the current previous line)

P ∶ printing, that is, printing a selected data. Usually p works with the parameter sed-n ~

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

For example: (suppose we have a file called ab)

Delete a row

[root@localhost ruby] # sed '1d' ab # Delete the first line

[root@localhost ruby] # sed'$d' ab # Delete the last line

[root@localhost ruby] # sed'1 ab # delete the first line to the second line

[root@localhost ruby] # sed'2 ab # delete the second line to the last line

Show a row

. [root@localhost ruby] # sed-n '1p' ab # shows the first line

[root@localhost ruby] # sed-n'$p'ab # displays the last line

[root@localhost ruby] # sed-n'1J 2p'ab # shows the first to second lines

[root@localhost ruby] # sed-n'2 ab # shows the second line to the last line

Use schema for query

[root@localhost ruby] # sed-n'/ ruby/p' ab # query includes all lines where the keyword ruby is located

[root@localhost ruby] # sed-n'/\ $/ p'ab # query includes all lines where the keyword $is located, using backslash\ to mask special meaning

Add one or more lines of string

[root@localhost ruby] # cat ab

Hello!

Ruby is me,welcome to my blog.

End

[root@localhost ruby] # sed'1a drink tea' ab # add the string "drink tea" after the first line

Hello!

Drink tea

Ruby is me,welcome to my blog.

End

[root@localhost ruby] # sed '1jue 3a drink tea' ab # add the string "drink tea" after the first to third lines

Hello!

Drink tea

Ruby is me,welcome to my blog.

Drink tea

End

Drink tea

[root@localhost ruby] # sed'1a drink tea\ nor coffee' ab # add multiple lines after the first line, using the newline character\ n

Hello!

Drink tea

Or coffee

Ruby is me,welcome to my blog.

End

Instead of one or more lines

[root@localhost ruby] # sed'1c Hi' ab # replace the first line with Hi

Hi

Ruby is me,welcome to my blog.

End

[root@localhost ruby] # sed'1 Hi' ab 2c replace lines 1 to 2 with Hi

Hi

End

Replace a part of a line

Format: sed's / string to be replaced / new string / g' (string to be replaced can use regular expression)

[root@localhost ruby] # sed-n'/ ruby/p' ab | sed's bird replacement of ruby for bird

[root@localhost ruby] # sed-n'/ ruby/p' ab | sed's _

insert

[root@localhost ruby] # sed-I'$a bye' ab # enter "bye" directly on the last line in the file ab

[root@localhost ruby] # cat ab

Hello!

Ruby is me,welcome to my blog.

End

Bye

Delete matching lines

Sed-I'/ match string / d 'filename (Note: if the match string is a variable, you need "" instead of "". I remember it seems to be)

Replace a string in a matching line

Sed-I'/ match string / s / replace source string / replace target string / g 'filename

Replace:

-e is an editing command that is used when sed performs multiple editing tasks. Before the next line starts editing, all editing actions are applied to the lines in the pattern buffer.

Sed-e '1mlm10d'-e's datafile MyUnimax YourUnique

The # option-e is for multiple edits. The first editor deletes lines 1-3. The second editor replaces all My that appears with Your. Because these two edits are made line by line (that is, both commands are executed on the current line of the mode space), the order of the editing commands affects the result.

# replace two or more spaces with one space

Sed's / [] [] * / / g 'file_name

# replace two or more spaces with delimiters:

Sed's / [] [] * /: / g 'file_name

# if spaces coexist with tab, replace them with the following command

# replace with a space

Sed's / [[: space:]] [[: space:]] * / / g 'filename

# replace component separator:

Sed's / [[: space:]] [[: space:]] * /: / g 'filename

=

Invocation of the sed command:

Type the command on the command line; insert the sed command into the script file, then call sed; to insert the sed command into the script file, and make the sed script executable

Sed [option] sed command input file uses the sed command in the command exercise, and the actual command should be in single quotation marks

Sed [option]-f sed script file input file using sed script file

Sed script file [option] input file first line sed script file with sed command interpreter

Option is as follows:

N does not print; sed does not write edit lines to standard output, default is to print all lines (edited and unedited), p command can be used to print edit lines

C the next command is the edit command, which is added when using multiple edits

F if you are calling a sed script file, use this option to inform sed that a script file supports sed commands, as shown in

Sed-f myscript.sed input_file where myscript.sed is the file that supports the sed command

The output of sed can be saved by using a redirect file

How to use sed to locate text in text:

X x is a line number, such as 1

XQuery y indicates that the line number ranges from x to y, for example, 2Pol 5 means from line 2 to line 5.

/ pattern/ query contains rows of patterns, such as / disk/ or / [amurz] /

/ pattern/pattern/ query contains rows of two patterns, such as / disk/disks/

/ pattern/,x queries rows containing patterns on a given line number, such as / disk/,3

XQuery / query for matching rows by line number and pattern, such as 3memore _ disk /

X,y! The query does not contain rows with specified line numbers x and y

Basic sed editing commands:

P print matching line c / replace positioned text with new text

= display file line number s replace the corresponding mode with the replacement mode

A / append new text information after locating the line number r read the text from another text

I / insert new text message w write text to a file after locating the line number

D delete positioning line Q exit or exit immediately after the first pattern matching is completed

L displays the control character y transfer character equivalent to the octal ASCII code

N read the next line of text from another text and append it to the next line {} the group of commands executed on the location line

G paste mode 2 to / pattern n /

Basic sed programming examples:

Use p (rint) to display the line: sed-n '2p' temp.txt displays only the second line, using the option n

Print range: sed-n'1j 3p 'temp.txt print lines 1 to 3

Print mode: sed-n'/ movie/'p temp.txt prints lines with movie

Query using mode and line number: sed-n'3 temp.txt moviepplemp find movie and print only on line 3

Show the entire file: sed-n '1jpp temp.txt $is the last line

Any character: sed-n'/. * ing/'p temp.txt Note is. * ing, not * ing

Print line number: sed-e'/ music/=' temp.txt

Additional text: (create sed script file) chmod Ubunx script.sed, runtime. / script.sed temp.txt

#! / bin/sed-f

/ name1/ a / # a / indicates that text is added here as a new line

HERE ADD NEW LINE. # added text content

Insert text: / name1/ a / change to 4 i4 for line number, I insert

Modify the text: / name1/ a / change to / name1/ c / will modify the whole line, c modify

Delete text: sed '1d' temp.txt or sed' 1mem4d' temp.txt

Replacement text: sed's source _ source _ OKSTR _ temp.txt to replace source with OKSTR

Sed's Placer Placer G' temp.txt deletes all the $symbols in the text

Sed 's/source/OKSTR/w temp2.txt' temp.txt writes the replaced record to the file temp2.txt

Replace the modified string: sed 's/source/ "ADD BEFORE" & / p' temp.txt

The result will be preceded by "ADD BEFORE" before the source string, where the & indicates the found source character and saves it.

The sed result is written to a file: sed'1J 2w temp2.txt' temp.txt

Sed'/ name/ w temp2.txt' temp.txt

Read the text from the file: sed'/ name/r temp2.txt' temp.txt

Add text at the end of each column: sed's / [0-9] * / & Pass/g' temp.txt

Pass a value from shell to sed: echo $NAME | sed "s/go/$REP/g" Note: use double quotation marks

Quick one-line command:

'sPlacer Universe'deletes the line ending with a period

'- e / abcd/d' deletes the line containing abcd

's / [] * / [] / g' delete more than one space and replace it with one space

's / ^ [] [] * / / g' delete the first space of the line

Delete the full stop followed by two or more spaces and replace it with a space

'/ ^ $/ d' Delete blank lines

's / ^. / / g' deletes the first character, distinguishing 'sUnix. Universe' deletes all periods

's/COL/ (... /) / / g' deletes the last three letters immediately following COL

's/ ^ / g' Delete the first / in the path

/

Use a period to match a single character period. Can match any single character. "." It can match the string header or any character in the middle. Suppose you are filtering a text file. For a script set with 10 characters, the first 4 characters are required to be followed by X C, and the matching operation is as follows:. . . .X C.. . .

2. Matching a string or character sequence with ^ at the beginning of a line is only allowed to match characters or words at the beginning of a line. The fourth character at the beginning of the line is 1, and the matching operation is expressed as: ^. . . one

3. Match a string or character with $at the end of the line. It can be said that $is the opposite of ^. It matches the string or character at the end of the line, and the $symbol is placed after the matching word. If the word j e t 0 1 is matched at the end of the line, the action is as follows: j e t 0 1$ if only a line containing one character is returned, the action is as follows: ^. $

4. Use * to match a single character in a string or its repeating sequence. Use this special character to match the repeated multiple expressions of any character or string.

5. Using / masking the meaning of a special character sometimes requires looking for characters or strings that contain a character designated by the system as a special character. If you want to match with * in a regular expression. For all the files at the end of p a s, you can do the following: / * /. P a s

6. Using [] to match a range or collection to match a specific string or set of strings, you can use commas to separate different strings to match in parentheses, but this is not mandatory (some systems advocate the use of commas in complex expressions), which increases the readability of the pattern. Use "-" to indicate a string range, indicating that the string range starts on the left side of "-" and ends on the right side of "-". Suppose you want to match any number, you can use: [0 1 2 3 4 5 6 7 8 9] to match any letter, use: [A-Z a-z] to indicate the letter range from A-Z, a-z.

7. The number of times to use / {/} matching pattern results can be matched any number of times using *, but if you specify a number of times, you should use / {/}. There are three forms of this pattern, namely:

The pattern/ {n /} matching pattern occurs n times.

There are at least n matching patterns in pattern/ {NJ /}.

The matching pattern of pattern/ {n ~ m} appears between n and m times, and n ~ m is any integer in 0-255.

The matching letter An appears twice and ends with B. the operation is as follows: a / {2 /} B matching value is An A B matching An at least 4 times, using: a / {4, /} B.

=

Replace single quotation marks with blank:

You can write like this:

Sed's Candle'"/ / g'

Sed's Universe\'/ / g'

Sed s /\'/ / g

=

Insert a line abc before the first line of the file

Sed-I'1i\ abc' urfile

At this point, I believe you have a deeper understanding of "the use of the sed command in linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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