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 in shell

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use the sed command in shell. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Sed tool: sed is a text stream editor that processes the contents of a file line by line

Usage: sed [option]. 'script' inputfile...

Options:

-n: silent mode, does not output the contents of the mode space to the screen

-e: realize multi-point functional editing

-f / path/from/script_file: read the edit script from the specified file

-r: regular expressions that use extensions are supported

Address interface method:

(1) No address: process the full text

(2) one address (single address)

#: specified line

/ pattern/: every line that can be matched by the referenced pattern:

(3) address range

#, #: from line to line

#, + #: displays the backward N line from which line

/ pat1/,/pat2/: matches two parameters at the same time, indicating that all lines between the two parameters are matched

#, / pat1/: indicates that all lines between these two parameters are matched

(4): step by step

# sed-n '1percent 2p' test.txt (starting with the first line, adding 2 lines at a time, meaning to show odd lines)

# sed-n '2cm 2p' test.txt (display from the second line, adding 2 lines at a time, meaning to show even lines)

Edit commands:

D: delete the content selected by the address definition

P: display (print) the contents of the mode space to the screen, printing only qualified lines, often used in combination with-n

A\ text: append text after a line: support using\ nMultiline append

Example: # sed'/ ^ Tramma a\ abc' a.txt

I\ text: append text in front of the line: support the use of\ nMultiline append

Example: # sed'/ ^ T _ I\ abc' a.txt

C\ text: replace the matching line:

Example: # sed'/ ^ Tmax c\ abc' a.txt

W / path/to/somefile: save the matching content in the pattern space to the specified file

R / path/from/somefile: after reading the text game of the specified file to the matching line in the pattern space

=: print the line number for the line in the mode space

Example: # sed-n'/ ^ T _ a.txt

Q: once the content is matched, exit; no further matching

!: take the inverse condition; operate on rows other than the rows to which the pattern matches

Splash: support the use of other delimiters, such as swatches, lemmas, etc. #

Replace the tag:

G: global intra-line replacement: for example: sed-n's replacement r.. etc/passwd with no g option, only the first time of each line is replaced

#: inline replacement, replace the matching content that occurs for the first time, for example: sed-n's Unix a.txt, replace the second occurrence of Unix

P: if the replacement is successful, the lines that have been replaced are displayed, used with-n

W / path/to/somefile: save the replaced line to the specified file

Exercise 1: remove all white space characters at the beginning of a line that begins with white space in the / boot/grub/grub.conf file

# sed's @ ^ [[: space:]]\ + @ @'/ boot/grub/grub.conf

Exercise 2: remove all # and white space characters starting with # followed by at least one white space character in the / etc/fstab file

# sed's @ ^ # [[: space:]]\ + @ @'/ etc/fstab

Practice 3:echo an absolute path to the sed command and take out its base name:

# echo "/ etc/sysconfig" | sed's @ [^ /]\ + $@ @'

Exercise 4: modify file names in batches

# ls * .jpg | sed's #\ (. *\) _ finished\ (. *\). * # mv &\ 1\ 2roomg' | bash

Advanced editing commands:

H: overwrite the contents of the schema space to the holding space

H: append the contents of the schema space to the holding space

G: take the data from the hold space and overwrite it to the schema space

G: take the data from the hold space and append it to the schema space

X: interchange the content in the pattern space with the content in the hold space

N: read the next row of the matched row to overwrite the pattern space

N: the next row reading the matching line is appended to the pattern space

D: delete all rows in the schema space

D: delete the first line in the multiline mode space

Example usage:

# sed-n'nscape p 'test.txt (output even lines)

# sed 'ntransferd' test.txt (only odd lines are displayed)

# sed'$! d 'test.txt (the last line out of the file)

# sed'G' test.txt (add a blank line below each line)

# sed'/ ^ $/ dash G'test.txt (only one blank line is left between each line)

# sed '1displaying Gentleman test.txt (displaying the contents of the file retrograde)

# sed'$! n 'test.txt (read the last two lines of the file)

# sed-n '1displaying Gentleman test.txt (retrograde display of file contents)

An example of keeping space in use:

Requirement: change all the words between the words the and statement to uppercase

Original text:

Find the Match statement

Consult the Get statement

Using the Read statement to retrieve data

Sed is written:

/ the. * statement/ {

H

S/.*the\ (. *\) statement.*/\ 1 /

Y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/

G

S /\ (. *\)\ n\ (. * the\). *\ (statement.*\) /\ 2\ 1\ 3 /

}

Results:

Find the MATCH statement

Consult the GET statement

Using the READ statement to retrieve data

Parsing:

/ the. * statement/: matches the rows of this pattern and then modifies it

H: overwrite the matching content to the holding space and keep a copy

S/.*the\ (. *\) statement.*/\ 1ax: converts matches to rows into words to be changed in pattern space

YUnip abcdefghijklmnopqrstuvwxyzhip ABCDEFGHIJKLMNOPQRSTUVWXYZ: convert modified words to uppercase

G: append a copy of the hold space to the pattern space

S /\ (. *\)\ n\ (. * the\). *\ (statement.*\) /\ 2\ 1\ 3Universe: replace the word changed to uppercase with the word in the original statement

Thank you for reading! This is the end of the article on "how to use sed commands in shell". I hope the above content can be of some help to you, so that you can learn more knowledge. 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

Network Security

Wechat

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

12
Report