Shell programming --- sed command explanation

Source: Internet
Author: User

Shell programming --- sed command explanation
Sed is a non-interactive text editor that can edit text files and standard input.
Sed only edits the copy of the original file in the buffer, not the original file

Sed command invocation Methods
1. Output a command in the shell command line to call sed

Sed [option] 'sed cmd' file

2. Insert the sed command into the script.
Sed [option]-f'sed. Sh' file
# Vi sed. sh sed: the script to be called is sed. sh
#/File:/a \ This is a test line !!!
Call scripts
# Sed-f sed. sh test

3. Insert the sed command into the script and execute it directly.
./Sed. sh file
# Vi sed. sh sed script
#! /Bin/sed-f
#/File:/a \ This is a test line !!!
#./Sed. sh test run the script


Sed Command Options and Significance
-N # do not print all rows to standard output
-E # indicates parsing the Next string as the sed edit command. If only one edit command is passed to sed, the-e option can be omitted.
-F # Call the sed script file

Sed command to locate text

X # x indicates the specified row number.
X, y # specifies the row number range from x to y
/Pattern/# query rows in the contain Mode
/Pattern/# query rows that contain two modes
/Pattern/, x # the line from the matched row with pattern to the row between x
X,/pattern/# the line from Line x to the matched line with pattern
X, y! # Querying rows that do not contain rows of rows x and y

Sed edit command
P # print matching rows
= # Print the file line number
A \ # append text information after locating the row number
I \ # insert text information before locating the row number
D # delete a row
C # Replace positioning text with new text
S # replace the corresponding mode with the replacement Mode
R # Read text from another file
W # write text to a file
Y # character conversion
Q # exit after the first pattern match is complete
L # display control characters equivalent to octal ASCII codes
{}# Command group executed on the target line
N # Read the next input line and use the next command to process the new line
H # copy the text in the mode buffer to keep the buffer
H # append the text in the mode buffer to the keep buffer
X # swap mode buffer and keep buffer content
G # copy the content of the buffer to the mode Buffer
G # append the content of the buffer to the mode buffer.

========================================================== ========================================================== ======

Sed example

Print the first line
# Sed-n '1p' test

Print all content, and there are two first rows
# Sed '1p' test

Print 3-6 lines of text
# Sed-n'3, 6p' test

Pattern Match: print the row containing certificate
# Sed-n'/certificate/P' test

Sed [-e]: parses the Next string into a sed edit command.
(1) print the content of the certificate matching mode
(2) print the line number of the certificate matching mode
(3) print the content and row number of the certificate matching mode.
# Sed-n'/certificate/P' test
# Sed-n'/certificate/= 'test
# Sed-n-e '/certificate/p'-e'/certificate/= 'test

The general format of sed with multiple editing commands is
# Sed [option]-e cmd1-e cmd2-e cmdn inputfile

Append the following content to the text string file: This is a test file !!!
# Sed '/file:/a \ This is a test file !!! 'Test

Append the wanglei user to the root of the/etc/passwd text.
# Sed-I '/root: x: 0: 0/a \ wanglei: x: 0: 0: wanglei:/bin/bash'/etc/passwd

Print the line containing. characters in the text test.
# Sed-n'/\./P' test

Print the row containing the $ character in the text test.
# Sed-n'/\ $/P' test

Print the last line in the text test.
# Sed-n' $ P' test

Print the rows that end with the bus string
# Sed-n'/. * bus $/P' test

Print the rows between 2 and 10
# Sed-n' 2, 10! P'test

Print all rows except the string certificate
# Sed-n'/certificate /! P'test

Insert the content "This is a test line !!! ", And saved in the file
# Sed-I '/above/I \ @ This is a test line !!! 'Test

Print the content of the above string to 13 lines in the text test.
# Sed-n'/above/, 13p' test

Print 3 rows to the string/home/globus/. In the text test.
# Sed-n'3, // home \/globus \/\./P' test

Insert a line "This is a test \ I line!" in front of the test text $88 string !!! "
# Sed '/\ $88/I \ This is a test \ I line !!! 'Test
Replace the text line of the matched string with the new text in \ c
# Sed '/specified address/c \ test' file

Replace the line matching the above string in the text with the new line "This is a new line !!! "
# Sed '/above/c \ This is a new line !!! 'Test

Replace lines 1, 5, and 13 in the text with "This is a 1 line !!! "(1, 5, 13)
# Sed-e '1c \ This is a 1 line !!! '-E' 5c \ This is a 5 line !!! '-E' 13c \ This is a 13 line !!! 'Test

Delete row 3rd from the test text
# Sed '3d 'test

Delete 1-3 rows in the test text
# Sed '1, 3d 'test

Delete the content of the string above to 13 in the test text.
# Sed '/above/, 13d' test

Delete the last line of text
# Sed '$ d' test

Delete 5 rows to the last row
# Sed '5, $ d' test

Delete the line of the case-insensitive string certificate in the text
# Sed '/[Cc] [Ee] [Rr] [Tt] [Ii] [Ff] [Ii] [Cc] [Aa] [Tt] [Ee]/d' test


Replace text # replace one or more strings
S/replaced string/New String/[replacement option]
G # indicates all the characters in the replaced text that appear in the replaced string.
P # combined with the-n option, only replace rows are printed
W file name # indicates directing the output to a file

# Sed-n's/replaced string/New String/P' input file

Replace the Certificate string in the test file with CERTIFICATE (replace only the previous one if there are multiple strings in the same industry)
# Sed-n's/certificate/CERTIFICATE/P' test

Replace all the strings in the test file with the shabi and save them to the/root/char file.
# Sed-n's/above/shabi/pg w/root/char 'test

Replace the string in the test text, replace the above with char, and replace the second char in each line in the text.
# Sed-n's/above/char/2p 'test

Replace the character string in the test text, replace the above with the charset, replace the 6th above in each line in the text, and save it to/root/save
# Sed-n's/above/charset/6 p w/root/save 'xx

Replace the CERTIFICATE string in test with oye and save it as oye.
# Sed-n's/CERTIFICATE/oye/pg w oye 'test

& Can be used to save the replaced string for calling. & the symbol is equivalent to the string before replacement.
# Sed-n's/seu/(&)/pg 'test
# Sed-n's/seu/(seu)/pg 'test

Print 1-5 lines of the test file and write them to the output
# Sed-n'1, 5 w output 'test

Read the content of file xx to the end of the file test string above.
# Sed '/above/r xx' test

Read xx contents of the file to the end of line 3 of the file test.
# Sed '5r xx' test

Read the contents of file xx to the end of line 5-7 of file test.
# Sed '5, 7r xx' test

Read xx contents to lines 5, 7, and 12 of the test file.
# Sed-e '5r xx'-e '7r xx'-e '12r xx' test

Print the. r. * matching file in the test file.
# Sed-n'/. r. */P' test

Print the. r. * matching file in the test file, and print only the first line that matches.
# Sed '/. r. */Q' test

========================================================== ============================
Conversion command y
It indicates character conversion. It converts a series of characters into corresponding characters and processes the characters one by one. Processing 1, processing 2...
# Sed 'y/transformed Character Sequence 'input file

Convert all a, B, c, d, e, f, g, h, and I in the test file to numbers 1, 2, 3, 4, 5, 6, 7, 8, 9
# Sed 'y/abcdefghi/123456789/'test

Convert a, B, o, v, e, and character in the test file to A, B, O, V, E
# Sed 'y/above/ABOVE/'test

Replace 27 English letters with 1234567890-= '~! In the test file '~! @ # $ % ^ & * () _ +/
# Sed 'y/abcdefghijklmnopqrstuvwxyz/1234567890-= '~! @ # $ % ^ & * () _ +/'Test

Execute Command Group {} on the target line {}
# Sed-n'/Certificate/{p; =} 'test
Equivalent to: sed-n-e '/Certificate/p'-e'/Certificate/= 'test

In the test text, replace all the I of the row that matches the Certificate keyword with I, and replace the first le with 99.
# Sed '/Certificate/{s/I/I/g; s/le/99/;}' test

N # process the next row of matching rows

Replace the user character in the line under the certificate string in the test text with 99
# Sed '/certificate/{n; s/user/99/;}' test

Replace the \ character of the line under the certificate string in the test text with gang
# Sed '/certificate/{n; s/\\/ gang/;}' test
======================================
Sed buffer Processing

# Sed-e '/file1/H'-E'/file2/X'-e' $ G' xxx
1. h # copy the row matching file1 to keep the Buffer Zone
2. x # swap the rows that match file2 and the rows that keep the buffer (file1)
3.G # append the content of the buffer to the mode buffer $ as the last row.

Use semicolons to separate multiple edit commands-e {};
Print the line and line number of the Ceritificate string in the test file using semicolons.
# Sed-n'/Certificate/p;/Certificate/= 'test

Replace the characters matching file1 In the xxx file with wl, the matching file2 with fx, the matching file3 with ywc, and the matching file4 with ly.
# Sed's/file1/wl/; s/file2/fx/; s/file3/ywc/; s/file4/ly/; 'xxx

The following are the scripts used by the previous instance:

========================================================== ====================================
1 --- This is a read file1
2 --- This is a read file2
3 --- This is a read file3
4 --- This is a read file4
5 --- This is a read file5
6 --- This is a read file6
7 --- This is a read file7
========================================================== ====================================
1 \ This is a Certificate Request file:
2 \
3 \ It shoshould be meailed to zawu@seu.edu.cn
4 \
5 ============================================== ==================================
6 \ Certificate Subject:
7 \
8 \/O = Grid/OU = GlobusTest/OU = simpleca-seugridl.seu.edu.cn/ouw.seu.edu.cn/cnw.globus
9 \
10 \ the above string is known as your user certificate subject, and it uniquely identifies
11 \ this user. $88
12 \ To install this user certificate, please save this e-mail message into the following file.
13 \
14 \/home/globus/. globus/usercert. pem
15 \ fawnbusgfqa
16 \ fwafawfbus

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.