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 sed, a centos text processing tool

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

Share

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

Editor to share with you how to use centos Chinese text processing tool sed, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The sed line editor sed is a stream editor that processes one line of content at a time. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen. Then read down the line and execute the next loop. If there is no special command such as'D', the pattern space is cleared between the two loops, but not the reserved space. This is repeated until the end of the file. The contents of the file have not changed unless you use redirect to store the output. The function is mainly used to edit one or more files automatically, simplify the repeated operation of files, write conversion programs and other common options.

-n    does not output mode space content to the screen, that is, it does not print automatically

-e    multipoint editing

-f / PATH/SCRIPT_FILE    reads the edit script from the specified file

-r    supports using extended regular expressions

-i.bak    backup file and edit it in place

Script    'address Command' sed tool address demarcation

(1) No address: process the full text

(2) single address:

   #: specified line, $: last line

Every line to which    / pattern/: can be matched by the pattern here

(3) address range

   #, #

   #, + #

   / pat1/,/pat2/

   #, / pat1/

(4): step by step

1x 2 odd lines

2-2 even-numbered line sed tool

Editing command

D    deletes the rows that match the pattern space and immediately enables the next loop

P    prints the contents of the current mode space, appended to the default output

A [] text    appends text after the specified line. Multiple line appends are supported.

I [] text    inserts text before the line

C [] text    substitution behavior single or multiple lines of text

W / path/file    saves lines that match the pattern to the specified file

After r / path/file    reads the text of the specified file to the matching line in the pattern space

=    prints line numbers for lines in mode space

! sed tool for matching row fetching and inverse processing in    pattern space

Scanplash /    find and replace, support the use of other delimiters, such as

Replace mark

G    in-line global replacement

P    displays the lines that have been replaced successfully

W    / PATH/FILE saves the replaced lines to a file

Advanced editing command

P:    print mode space starts to\ ncontent and appends to the default output

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

H:    appends the contents of the schema space to the hold space

G:    takes the data from the hold space and overwrites it to the schema space

G:    takes the content from the hold space and appends it to the pattern space

X:    interchanges the content in the schema space with the content in the hold space

N: the next row of the matched row read by    overrides the pattern space

N:    reads the next line of the matching line and appends it to the pattern space

D:    deletes rows in schema space

D:    if the pattern space contains a newline character, the text in the mode space up to the first newline character is deleted, the new input line is not read, and the loop is restarted using the composite pattern space. If the mode space does not contain a newline character, the normal new loop is started like the d command

Practice

\ 1. Find 3 to 6 lines

\ 2. Find 3 to 3 to 2 lines

\ 3. Add new line on line 2

\ 4. Add test line on line 11

\ 5. Match the text that begins with alias and save it to alias.log

\ 6. Increase the number of blanks after each cycle?

[root@centos ~] # sed'G' text.txt

\ 7. Insert "blank" after matching "regex"?

[root@centos ~] # sed'/ regex/G' text.txt

\ 8. Sed changes the haiwao in the 50th piece of test to haiwai?

[root@centos ~] # sed-n '50p' | sed's stop haiwaomax haiwaixue' test

\ 9. Replace the root:x:0:0:root:/root:/bin/bash line in a file / etc/passwd with the second root as test?

[root@centos ~] # cat / etc/passwd | sed'/ ^ root _ sed _

\ 10. Print / etc/passwd odd number "?

[root@centos ~] # sed-n '1room2p' / etc/passwd

\ 11. Print out all blogs between 05:00 and 12:00? Print out all blogs between 05:30:35 and 22:45:55?

[root@centos ~] # sed-n'/ 2016\ / 06\ / 12 05 a.log 0000lemaget / 0000lemor.2016\ / 06\ / 1212151500150000lemp 'a.log

[root@centos ~] # sed-n'/ 2016\ / 06\ / 12 05WR 30RX Jet 2016\ / 06\ / 12 2222 Flex 45Rd 55ip p 'a.log

\ 12. Remove all blank characters in centos7 system / etc/grub2.cfg components that start with a blank

[root@centos ~] # sed-r's / ^ [[: blank:]] + / /'/ etc/grub2.cfg

\ 13. Delete all the # and blank characters in the / etc/fstab package that begin with # followed by a blank character

[root@centos ~] # sed-r's / ^ # [[: blank:]] + / / 'g / etc/fstab

\ 14. Add # to the centos6 system / root/install.log for each additional number.

[root@centos ~] # sed-r's root/install.log.

\ 15. Add a # sign to the "etc/fstab" that does not begin with # in the / parts.

[root@centos ~] # sed-r's @ ^ [^ #] @ # & @ p' / etc/fstab

\ 16. Process the / etc/fstab path so that the sed command fetches its directory name and base name

Directory name:

[root@centos ~] # echo "/ etc/fstab" | sed-r's @ ^ (. /) ([^ /] + /?) $@\ 1percent'

Base name:

[root@centos ~] # echo "/ etc/fstab" | sed-r's @ ^ (. /) ([^ /] + /?) $@\ 2percent'

\ 17. Sed retrieves the local IPv4 address from the ifconfig command

[root@centos ~] # ifconfig | sed-n'/ broadcast/p' | sed-r 's@inet (.) netmask.@\ 1cm'

[root@centos ~] # ifconfig ens33 | sed-r '2Dead; s @ (.inet) (.) (netmask.*) @\ 2room'

\ 18. Count all the rpm components recorded by Package on the centos installation CD. Separate the number of repeats of the penultimate field

[root@centos ~] # ls .rpm | sed-r's @ ^.. ([^.] +) .rpm$@\ 1employees'| sort | uniq-c

[root@centos ~] # ls * .rpm | rev | cut-d. -f2 | rev | sort | uniq-c

\ 19. Count and sort the number of occurrences of each word in / etc/init.d/functions components (implemented by the methods of grep and sed respectively)

[root@centos ~] # sed-r "s / [^ [: alpha:]] /\ nzag" / etc/init.d/functions | sort | uniq-c | sort-nr

[root@centos ~] # egrep-o "[[: alpha:]] +" / etc/init.d/functions | sort | uniq-c | sort-nr

\ 20. Merge the n and nu 1 of this piece into an odd number.

[root@centos ~] # seq 10 | sed "1x 2N players /"

The above is all the contents of the article "how to use sed, a Chinese text processing tool for centos". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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