Shell programming-detailed introduction to sed commands, shellsed detailed introduction

Source: Internet
Author: User

Shell programming-detailed introduction to sed commands, shellsed detailed introduction

Sed Introduction

Sed editor is called Stream editor. It is used to perform basic text conversion in an input stream (file or input from a pipeline. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the sed command is used to process the content in the buffer, send the buffer content to the screen, process the next line, and repeat until the end of the file. The file content has not changed, unless the redirection storage output is used, sed is mainly used to automatically edit one or more files, simplifying repeated operations on files, writing conversion programs, and so on.

Sed command line options

Sed command complete format: sed OPTIONS... [SCRIPT] [INPUTFILE...] command line options include: -- version: display version information;-n -- quiet -- silent: only the results after script processing are printed;-e -- expression = script: use the script specified in the option to process the input file;-f script-file or -- file = script-file: use the file specified in the option to process the input file; -I [SUFFIX] Or -- in-place [= SUFFIX]: directly modifies the content read by the document and does not output it to the screen;-r or -- regexp-extended: sed uses extended regular expressions.

Sed exit status

If the exit status is 0, the operation is successful. If the exit status is non-zero, the operation fails. 0: Successful completion; 1: Invalid Command, invalid syntax or invalid regular expression; 2: the specified file in the command line cannot be opened (for example, the file cannot be found or has no permission), and other files can be processed. 4: I/O errors or serious processing errors during running;

Sed metacharacters

^: Match the beginning of a row, such as:/^ sed/match all rows starting with sed; $: match the end of a row, such: /sed $/match all rows ending with sed ;.: match any character that is not a line break, for example:/s. d/match s followed by any character, followed by d; *: Match 0 or multiple characters, such: /* sed/match all templates with one or more spaces followed by sed rows. []: match a character in a specified range, for example,/[ss] ed/matches sed and Sed ;? [^]: Match a character that is not within the specified range, for example:/[^ A-RT-Z] ed/match a line that begins with a letter that does not contain a A-R and a T-Z, followed by ed; \(.. \): match the substring and save the matched characters, such as s/\ (love \) able/\ 1rs. loveable is replaced with lovers ;&: save the search character to replace other characters, such as s/love/** & **/. love is like ** love **; \ <: match the start of a word, for example: /\ : Match the end of a word, such as/love \>/match the row containing the word ending with love; x \ {m \}: repeated characters x, m times, such: /0 \ {5 \}/match rows containing 5 0; x \ {m, \}: Repeated character x, at least m times, for example:/0 \ {5, \}/match at least five 0 rows; x \ {m, n \}: Repeated character x, at least m times, no more than n times, such: /0 \ {5, 10 \}/match 5 ~ 10 0 rows.

Sed command

You can use man sed to view the detailed parameter information of this command:

[root@strong sed_stu]# man sed

Sed command demonstration

Test Data

[root@strong sed_stu]# cat emp.txt 0001  Alen   M  240002  Tiboo  M  320003  Felix  M  260004  Jack   F  240005  Tim    M  250006  Audi   F  300007  Bobo   F  320008  Geo    M  210009  Andy   F  1900010 Peter  M  28[root@strong sed_stu]# 

1. Print: p command

-- Print the first line of information [root @ strong sed_stu] # sed '1p' emp.txt 0001 Alen M 240001 Alen M 240002 Tiboo M 320003 Felix M 260004 Jack F 240005 Tim M 250006 Audi F 300007 bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 by default, the input line is printed to the screen, and option-n is used together to cancel the default printing. Only the selected content is printed: -- print the first line [root @ strong sed_stu] # sed-n '1p' emp.txt 0001 Alen M 24 -- print the records from the third line to the fifth line [root @ strong sed_stu] # sed- n'3, 5 p 'emp.txt 0003 Felix M 260004 Jack F 240005 Tim M 25 -- print the row containing Tim [root @ strong sed_stu] # sed-n'/Tim/P' emp.txt 0005 Tim M 25 -- print the line from Tim to Bobo [root @ strong sed_stu] # sed-n'/Tim /, /Bobo/p 'emp.txt 0005 Tim M 250006 Audi F 300007 Bobo F 32

2. Delete: d command

-- Delete the fifth line. By default, other lines will be printed to the screen [root @ strong sed_stu] # sed '5d 'emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 260004 Jack F 240006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 -- delete records from the third row to the last row [root @ strong sed_stu] # sed '3, $ d 'emp.txt 0001 Alen M 240002 Tiboo M 32 -- delete the line from the include mode Felix to Geo [root @ strong sed_stu] # sed '/Felix /, /Geo/d 'emp.txt 0001 Alen M 240002 Tiboo M 320009 Andy F 1900010 Peter M 28

3. Replace: s command

-- Replace 2 in the text with 222 [root @ strong sed_stu] # sed's/2/222/'emp.txt 0001 Alen M 2224000222 Tiboo M 320003 Felix M 22260004 Jack F 22240005 Tim M 22250006 audi F 300007 Bobo F 32220008 Geo M 22210009 Andy F 1900010 Peter M 2228 -- replace the logo with number 2, replace the [root @ strong sed_stu] # sed's/2 /.... 222/2 'emp.txt 0001 Alen M 240002 Tiboo M 3 .... 2220003 Felix M 260004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008G Eo M 210009 Andy F 1900010 Peter M 28 Note: In this example, the value of the first occurrence of 2 in the row is 222. If you replace the record of the positive row, use it with g, indicates global replacement. -- Replace the ID with g, replace all instances of existing text with new text [root @ strong sed_stu] # sed's/2/222/G' emp.txt 0001 Alen M 2224000222 Tiboo M 32220003 Felix M 22260004 Jack F 22240005 Tim M 22250006 Audi F 300007 Bobo F 32220008 Geo M 22210009 Andy F 1900010 Peter M 2228 --- replace ID p, print the content of the original line [root @ strong sed_stu] # sed-n's/2/222/gp 'emp.txt 0001 Alen M 2224000222 Tiboo M 32220003 Felix M 22260004 Jack F 22240005 Tim M 22250007 bobo F 32220008 Geo M 222100010 Peter M 2228 -- replace the ID with w file, write the replaced result to the file [root @ strong sed_stu] # sed's/2 /.... 222/w test.txt 'emp.txt 0001 Alen M .... 2224000 .... 222 Tiboo M 320003 Felix M .... 22260004 Jack F .... 22240005 Tim M .... 22250006 Audi F 300007 Bobo F 3 .... 2220008 Geo M .... 22210009 Andy F 1900010 Peter M .... 2228 [root @ strong sed_stu] # cat test.txt 0001 Alen M .... 2224000 .... 222 Tiboo M 320003 Felix M .... 22260004 Jack F .... 22240005 Tim M .... 22250007 Bobo F 3 .... 2220008 Geo M .... 222100010 Peter M .... 2228 -- add the *** HI *** [root @ strong sed_stu] # sed '/Felix /, /Audi/s/$/*** HI ***/'emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 26 *** HI *** 0004 Jack F 24 * ** HI *** 0005 Tim M 25 *** HI *** 0006 Audi F 30 *** HI *** 0007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28

4. Use multiple editor commands: e command

[Root @ strong sed_stu] # sed-e's/Alen/ALEN/;/Jack /, /Audi/s/$/***** HI *****/'emp.txt 0001 alen m 240002 Tiboo M 320003 Felix M 260004 Jack F 24 ***** HI *** * 0005 Tim M 25 ***** HI ***** 0006 Audi F 30 ***** HI ***** 0007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 -- or separate the use of-e [root @ strong sed_stu] # sed-e's/Alen/ALEN/'-E'/Jack /, /Audi/s/$/***** HI *****/'emp.txt 0001 alen m 240002 Tiboo M 320003 Felix M 260004 Jack F 24 ***** HI *** * 0005 Tim M 25 ***** HI ***** 0006 Audi F 30 ***** HI ***** 0007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 -- use the prompt [root @ strong sed_stu] # sed-e '> s/Alen/>/Jack /, /Audi/s/$/***** HI ***/'emp.txt 0001 alen m 240002 Tiboo M 320003 Felix M 260004 Jack F 24 ***** HI *** 0005 tim M 25 *** HI *** 0006 Audi F 30 *** HI *** 0007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28

5. Read the editor command from a file

-- Sed Command [root @ strong sed_stu] # cat script.txt s/Alen/ALEN // Jack /, /Audi/s/$/***** HI *****/--- f specifies the sed script file [root @ strong sed_stu] # sed-f script.txt emp.txt 0001 alen m 240002 Tiboo M 320003 Felix M 260004 Jack F 24 ***** HI ***** 0005 Tim M 25 ****** HI ***** 0006 Audi F 30 ***** HI * * ** 0007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28

6. Additional Text: a command

-- Add a row after the specified row [root @ strong sed_stu] # sed '3a \ This is append test 'emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 26 This is append test0004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28

7. Insert text: I command

[root@strong sed_stu]# sed '3i\This is inserted line' emp.txt 0001  Alen   M  240002  Tiboo  M  32This is inserted line0003  Felix  M  260004  Jack   F  240005  Tim    M  250006  Audi   F  300007  Bobo   F  320008  Geo    M  210009  Andy   F  1900010 Peter  M  28

8. Change the line: c command

-- Modify the entire line of text content [root @ strong sed_stu] # sed '3c \ This is updated by new line' emp.txt 0001 Alen M 240002 Tiboo M 32 This is updated by new line0004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28

9. delete a row: d command

[root@strong sed_stu]# sed '1,5d' emp.txt 0006  Audi   F  300007  Bobo   F  320008  Geo    M  210009  Andy   F  1900010 Peter  M  28

10. Change the line: y command

[root@strong sed_stu]# sed '1,5y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' emp.txt 0001  ALEN   M  240002  TIBOO  M  320003  FELIX  M  260004  JACK   F  240005  TIM    M  250006  Audi   F  300007  Bobo   F  320008  Geo    M  210009  Andy   F  1900010 Peter  M  28

11. Obtain the next line: n command

-- Get the next line of Jack and use .... 2222 replace 2 [root @ strong sed_stu] # sed '/Jack/{n; s/2 /.... 2222/;} 'emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 260004 Jack F 240005 Tim M .... 222250006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 -- delete empty rows [root @ strong sed_stu] # cat emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 260004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 -- delete spaces in the next row of Felix [root @ strong sed_stu] # sed '/Felix/{n; d} 'emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 260004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28 -- delete all spaces [root @ strong sed_stu] # sed '/^ $/d' emp.txt 0001 Alen M 240002 Tiboo M 320003 Felix M 260004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008 Geo M 210009 andy F 1900010 Peter M 28

12. Combine multiple lines of text: N command

-- Add the next row to the data stream to create a multi-row group for processing [root @ strong sed_stu] # sed '/Alen/{N; s/\ n //;} 'emp.txt 0001 Alen M 24 0002 Tiboo M 320003 Felix M 260004 Jack F 240005 Tim M 250006 Audi F 300007 Bobo F 320008 Geo M 210009 Andy F 1900010 Peter M 28

13. Negative command :! Command

-- Output data rows not between 3 and 7 [root @ strong sed_stu] # sed-n' 3, 7! P'emp.txt 0001 Alen M 240002 Tiboo M 320008 Geo M 210009 Andy F 1900010 Peter M 28

14. Mode replacement

-- & Indicates The match mode in The replace command -- quote a word with [root @ strong sed_stu] # echo "The cat sleeps in his hat" | sed's /. at/"&"/G' The "cat" sleeps in his "hat" -- use parentheses to define The child string elements in The replacement mode and reference them with specific symbols, the replacement character consists of a backslash and a number. [root @ strong sed_stu] # echo "This is Alen Liu, not Alen Li" | sed's/\ (Alen \) liu/\ 1 L/'This is Alen L, not Alen Li

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.