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 tool in Shell

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

1. Sed introduces 1. What does sed do

Sed is an acronym for Stream Editor (stream editor); used to process files.

2. How does sed handle files

Sed reads the contents of the file line by line and processes them as required, and outputs the processed results to the screen.

First, sed reads a line from the file, saves it in a temporary cache (also known as pattern space), then processes the lines in the temporary buffer as needed, and sends the line to the screen when it is finished.

Summary:

Since sed stores each line in a temporary buffer and edits this copy, it will not modify the original file directly. Sed is mainly used to edit one or more files automatically; it simplifies repeated operations on files, filters and converts files. 2. Introduction to the use of sed

There are two common syntax formats for sed, one is called command line mode, and the other is called script mode.

1. Command Line format 1) Syntax format

Sed [options] 'processing Action' file name

Common option description Note-e to make multiple (multiple) edits-n cancel default output does not automatically print mode space-r use extended regular expression-I edit in place (modify source file)-f specify common processing actions for file names of sed scripts

To put it bluntly, all the following actions should be in single quotes.

Action Note'p 'print' I 'insert content similar to uppercase Olympiad in vim before specified line insert content similar to lowercase Olympus in vim after specified line replace all content in specified line' d' delete specified line 2) example document preparation # vim a.txt root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/ Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin298374837483172.16.0.25410.1.1.1 ① adds to the file Delete, modify, check = = operation

Syntax: files to be processed by the sed option 'Positioning + Command'

1) print the contents of the file [root@server ~] # sed''a.txt do nothing to the file [root@server ~] # sed-n 'p' a.txt print each line And cancel the default output [root@server ~] # sed-n '1p' a.txt print line 1 [root@server ~] # sed-n' 2p' a.txt print line 2 [root@server ~] # sed-n '1p' a.txt print 1 to 5 lines [root@server ~] # sed-n' $p 'a.txt Print the last line 2) add file content

I address location above insert

An insert below

[root@server ~] # sed'$a99999' a.txt file add content below the last line [root@server ~] # sed 'a99999' a.txt file add content under each line [root@server ~] # sed '5a99999' a.txt file line 5 add content [root@server ~] # sed' $i99999' a.txt Add content on the last line of the file [root@server ~] # sed 'i99999' a.txt file add content on each line [root@server ~] # sed' 6i99999' a.txt file line 6 add content [root@server ~] # sed'/ ^ uucp/ihello' insert content 3) modify the content of the file

C replaces the specified = = entire line = = content

[root@server ~] # sed '5chello world' a.txt replace file line 5 [root@server ~] # sed' chello world' a.txt replace all contents of the file [root@server ~] # sed'1 dagger 5chello world' a.txt replace file Nos. 1 to 5 replace hello world [root@server ~] # sed'/ ^ user01/c888888' a.txt replace the line starting with user01 4) delete the file content [root@ Server ~] # sed '1d' a.txt delete file line 1 [root@server ~] # sed' 1d' a.txt delete file line 1 to 5 [root@server ~] # sed'$d' a.txt delete the last line of the file ② searches and replaces the file

Syntax: sed options's / searched content / replaced content / action 'files to be processed

Where s represents search search; slash / represents delimiter, which can be defined by yourself; the action is usually print p and global replace g

[root@server ~] # sed-n's rootbank Root 1.txt 1.txt [root@server ~] # sed-n's / ^ # / / gp' 1.txt [root@server ~] # sed-n's pickup a.txt [root@server ~] # sed-n's / sbin\ / nologin/itcast/gp' a.txt [root @ server ~] # sed-n '10s substitution 2.txt a.txt uucp:x:10:14:uucp:/var/spool/uucp:itcast [root@server ~] # sed-n' a.txt Note: the delimiter in search replacement can be specified by yourself [root@server ~] # sed-n '1description 5s / ^ / # / p' a.txt comment out the file within 1-5 lines Allow # root:x:0:0:root:/root:/bin/bash#bin:x:1:1:bin:/bin:/sbin/nologin#daemon:x:2:2:daemon:/sbin:/sbin/nologin#adm:x:3:4:adm:/var/adm:/sbin/nologin#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin ③ other commands to interpret remarks r read from another file Save w content as & Save the lookup string so that the reference in the replacement string is the same as\ (\) = print the line number! Apply the command to all lines except the selected line, and exit after the number of lines.

Examples are as follows:

R read the input line from the file w write the selected line to the file [root@server ~] # sed'3r / etc/hosts' 2.txt [root@server ~] # sed'$r / etc/hosts' 2.txt [root@server ~] # sed'/ root/w a.txt' 2.txt [root@server ~] # sed'/ [0-9] {4} / w a.txt' 2.txt [root@server ~] # sed-r'/ ([ 0-9] {1pr 3}\.) {3} [0-9] {1pr 3} / w b.txt '2.txt! Apply a command to all lines except the selected line Put after the number of lines [root@server ~] # sed-n '1roomp' 1.txt [root@server ~] # sed-n' 4p' 1.txt [root@server ~] # sed-n '4roomp' 1.txt [root@server ~] # cat-n 1.txt [root@server ~] # sed-n' 1Mold 17p' 1.txt [root@server ~] # sed-n '1mlm17roomp' 1.txt & save the search string for use in the replacement string Quote\ (\) [root@server ~] # root-n'/ root/p' a.txt root:x:0:0:root:/root:/bin/bash [root@server ~] # sed-n's passwd comment out the line # root-n-r's / ^ passwd beginning with root # root-n-r / ^ root | ^ root / # & / p'/ etc/passwd comment out lines that begin with root or begin with stu # sed-n '1Magi 5s / ^ [a sed z]. * / # & / p' passwd comment out lines beginning with any lowercase letter in lines 1 to 5 # sed-n'1 etc/passwd 5s / ^ / # / p'/ etc/passwd comment 5x5 or sed-n '1head 5s / ^ / # / p' passwd begins with blank and adds # sed-n '1head 5s / ^ # # / / replace p'root with an empty [root@server ~] # sed-n'/ ^ root/p' 1.txt [root@server ~] # sed-n / ^ 1.txt [root@server ~] # sed-n /\ (^ root\) / #\ 1p'root [root@server] # sed-nr'/ ^ root | ^ stu/p' 1.txt [root@server ~] # sed-nr's / ^ root | ^ stu/#& / p '1.txt = print line number # sed-n' / bash$/=' passwd print line number # sed-ne'/ root/='-ne'/ root/p' passwd # sed-n'/ nologin$/= for lines ending in bash / nologin$/p' 1.txt# sed-ne'/ nologin$/='-ne'/ nologin$/p' 1.txtq exit # sed '5q' 1.txt# sed' / mail/q' 1.txt# sed-r'/ ^ yunwei | ^ mail/q' 1.txt [root@server] # sed-n'/ bash$/p 10q' 1.txtROOT:x:0:0:root:/root:/bin/bash Comprehensive use: [root@server ~] # sed-n '1Magi 5s / ^ / # & / p' 1.txt # root:x:0:0:root:/root:/bin/bash#bin:x:1:1:bin:/bin:/sbin/nologin#daemon:x:2:2:daemon:/sbin:/sbin/nologin#adm:x:3:4:adm:/var/adm:/ Sbin/nologin#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@server ~] # sed-n '1Magi 5s /\ (^\) / #\ 1Maple p' 1.txt # root:x:0:0:root:/root:/bin/bash#bin:x:1:1:bin:/bin:/sbin/nologin#daemon:x:2:2:daemon:/sbin:/sbin/nologin#adm:x:3:4:adm:/var / adm:/sbin/nologin#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin ④ other options-e multiple edits-r extension regular-I modify the original file [root@server ~] # sed-ne'/ root/p' 1.txt-ne'/ root/='root:x:0:0:root:/root:/bin/bash1 [root@server ~] # sed-ne'/ root/='-ne'/ Root/p' 1.txt 1root:x:0:0:root:/root:/bin/bash inserts "hello world" before line 5 in the 1.txt file Insert "" [root@server] # sed-e '5ihello world'-e' 8a ha '1.txt-e' 5potential 8p under line 8 of the 1.txt file. 5p '1.txtsed-ne' 1p'-ne'5p '1.txtsed-ne' 1p 5p '1.txt filters vsftpd.conf files starting with # and blank lines: [root@server ~] # grep-Ev' ^ # | ^ $'/ etc/vsftpd/vsftpd.conf [root@server ~] # sed-e'/ ^ # / d'-e'/ ^ $/ d' / etc/vsftpd/vsftpd.conf [root@server ~] # sed'/ ^ # / d / ^ $/ d' / etc/vsftpd/vsftpd.conf [root@server ~] # sed-r'/ ^ # | ^ $/ d' / etc/vsftpd/vsftpd.conf filters lines in the smb.conf file that are in effect: # sed-e'/ ^ # / d'-e'/ ^; / d'-e'/ ^ $/ d'-e'/ ^\ tbind smb.conf# sed-r'/ ^ (# | $| $| | |\ t# |\ t$) / d 'smb.conf # sed-e' / ^ # / d'- e'/ ^ / d'- e'/ ^ $/ d'- e'/ ^\ t pick up the IP address in the file: [root@server ~] # grep-E'([0-9] {1} 3}\.) {3} [root@server ~] # grep'^ [^ amurz] '1.txt [root@server ~] # sed-n' / ^ [^ amurz] / p '1.txt filter out the IP address in the file: [root@server ~] # grep-E' ([0-9] {1mer3}\.) 9] {1 3} '1.txt 192.168.0.254 [root@server ~] # sed-nr' / ([0-9] {1 1.txt 3}\.) {3} [0-9] {1 1.txt 192.168.0.254 [root@server ~] # grep-o-E'([0-9] {1pm 3}\.) {3} [0-9] {1Mague 3} '2.txt 10.1.1.110.1.1.255255.255. 255.0 [root@server ~] # sed-nr'/ ([0-9] {1J 3}\.) {3} [0-9] {1J 3} / p '2.txt10.1.1.110.1.1.255255.255.255.0 filter out the IP, Subnet mask, Broadcast address: [root@server shell06] # grep-Eo'([0-9] {1jue 3}\.) {3} [0-9] {1} 3} 'ifcfg-eth0 10.1.1.1255.255.255.010.1.254 [root@server shell06] # sed-nr' / ([0-9] {1jue 3}\.) {3} [0-9] {1jue 3} / p 'ifcfg-eth0 | cut-dcards'-f210.1.1.1255.255.255 .010.1.1.254 [root@server shell06] # sed-nr'/ ([0-9] {1sed 3}\.) {3} [0-9] {1 ifcfg-eth0 3} / p 'ifcfg-eth0 | sed-n's / [Amurz =] / / gp'10.1.1.1255.255.255.010.1.1.254 [root@server shell06] # ifconfig eth0 | sed-n' 2p' | sed-n's / [: a Meiz] / / gp' | sed-n's / / \ nUniverse gp' | sed'/ ^ $/ dbuyer 10.1.1.110.1.1.255255.255.255.0 [root@server shell06] # ifconfig | sed-nr'/ ([0-9] {1 root@server 3}\.) [0-9] {1 root@server 3} / p' | head-1 | sed-r's / ([a Maiz:] | [A-Z/t]) / / g' | sed's / /\ nUniverse g' | sed'/ ^ $/ d' [root@server Shell06] # ifconfig eth0 | sed-n '2p' | sed-n' s/.*addr:\ (. *\) Bcast:\ (. *\) Mask:\ (. *\) /\ 1\ n\ 3/p'10.1.1.1 10.1.1.255 255.255.255.0Coli option directly modifies the original file # sed-I 's/root/ROOT/ 11.txt# sed ni STUBN '11.txt# sed-I' 17 {ni @ @ sed @ @ sed @ @-I'1 ni 5s / ^ / # & /'ni Note:-do not use the p command together do not use the-I command do not use it in conjunction with regular use

Sed option 'sed command or regular expression or address location' file name

Addressing is used to determine which rows to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, sed processes all lines of the input file. Comment on regularization / key/ query line containing keywords sed-n'/ root/p' 1.txtqqqlash sed / match the line between two keywords sed-n'/\ ^ adm/,/ ^ mysql/p' 1.txt _ ftp/,7p'x x from the line that matches the keyword to the line between line x of the file (including the line of the keyword) sed-n'/ ^ ftp/,7p'x / key/ starts from line x of the file to the line that matches the keyword. Does not contain lines x to y / keybank! Lines excluding keywords sed-n'/ bash garbage 1.txt2. Script format 1) using # sed-f scripts.sh file / / using script processing files it is recommended to write #! / bin/sed-F1 script on the first line of the. / bin/sed-F1 script with #! / bin/sed-F1 memo 5dsG3i7775i888a999p2) Note 1) the script file is a command line listing for sed.' Commands'2) cannot have any spaces, tabs, or other text at the end of each line. 3) if there are multiple commands on one line, they should be separated by semicolons. 4) do not need and cannot use quotation marks to protect commands 5) behavior notes starting with #) 3) give examples of # cat passwdstu3:x:509:512::/home/user3:/bin/bashstu4:x:510:513::/home/user4:/bin/bashstu5:x:511:514::/home/user5:/bin/bash# cat sed.sh #! / bin/sed-F2a\ * 2 $s/stu/user/$a\ we inster new lines/ ^ [amurz]. * / # & / [root@server ~] # cat 1.sed #! / bin/sed-f3axiangzhongchelloworld1 3s / ^ / # & / [root@server ~] # sed-f 1.sed-I 11.txt [root@server ~] # cat 11.txt # root:x:0:0:root:/root:/bin/bash # bin:x:1:1:bin:/bin:/sbin/nologin#daemon:x:2:2:daemon:/sbin:/sbin/nologin**adm:x:3:4:adm:/var/adm:/sbin/nologinhelloworld3. Supplementary extension summary 1. Regular expressions must be at regular intervals before and after "/" for example: sed'/ root/d' file for example: sed'/ ^ root/d' file2, if they match extended regular expressions You need to use the-r option to extend sedgrep-Esed-r +? () {nsedgrep m} |\ d Note: if a special character (^ $. * / []) appears in the regular expression, the leading "\" sign needs to be used to escape eg:sed'/\ $foo/p' file3. Comma separator such as: sed '5Magazine 7d' file deletes lines 5 to 7 such as: sed' / root/ / ftp/d' file deletes all lines from the first matching string "root" to the first matching string "ftp" without looking for a loop to execute 4. Combination mode such as: sed'1 sed'/ foo/ delete all lines from the first line to the first matching string "foo" + 4d' file deletes lines starting with the matching string "foo" and ending with the next four lines for example: sed'/ foo/ ~ 3d' file deletes a multiple line (in the file) from the matching string "foo" to 3 for example: sed '1delete 5d' file deletes every five lines from the first line for example: sed-nr' / foo | bar/p' file displays the line of the configuration string "foo" or "bar" for example: sed-n'/ foo/ / bar/p' file displays matching lines from foo to bar for example: sed'1 deleted 2d' file deletes odd lines for example: sed'0-2d' file deletes even lines sed'1 deleted 2d 'file5, special cases for example: sed' $d' file deletes the last line for example: sed '1d' file deletes the first line 6, Other: sed's sed. Sed 'a.txt deletes the first character in each line, sed's a.txt. Universe 2' a.txt deletes the second character in each line, sed's Universe. Delete the nth character in each line (N > 2) sed's a.txt. Delete the last character in each line [root@server ~] # cat 2.txt 1 a2 b3 c4 d5 e6 f7 u8 k9 o [root@server ~] # sed'/ c Universe 2d' 2.txt 1 a2 b5 e6 f7 u8 k9 o

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