Linux sed Command Instance parsing

Source: Internet
Author: User

Recently, looking at project makefile, I saw the powerful editing ability of SED, and before Makefile worked, it was usually executed scripts or make Menuconfig to configure various global variables. The SED activity phase is usually in the bash shell, just a little bit more.

1. Introduction to Sed

Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

2. addressing

You can locate the row you want to edit by addressing it, which is made up of numbers, and two rows separated by commas represent the range of rows starting and ending with these two behaviors (including the two lines represented by the number of rows). The dollar sign ($) represents the last line, as 1,3 represents a one-line. The range can be determined by data, regular expressions, or by combining them.

3. Sed Command

called sed there are two forms of commands:

    • sed [options] ' command ' file (s)
    • sed [options]-F scriptfile file (s)
    • For example, below
chen:~$ sed usage: sed [options] ... {Script (if no other script)} [Input file] ...-N,--quiet,--silent cancels the automatic print mode space-e script,--expression= script adds "script" to the program's run list-F foot This article --file= script file to add "script file" to the program's run list--follow-symlinks directly modify the file when following the soft link-i[suffix],--in-place[=s Uffix] Edit files in place (makes backup if SUFFIX supplied)-L N,--line-length=n specify "L" Life                 The line wrapping expected length--posix closes all GNU extension-R,--regexp-extended uses the extended regular expression-s in the script,--separate                 Treat the input file as individual files instead of a long continuous input-u,--unbuffered read the least data from the input file, and refresh the output more frequently-Z,--null-data Separate lines by NUL characters--help print help and exit--version output version information and exit if there is no-e,--expression,-f or--file option, Then the first non-option parameter is considered an SED script. Other non-option parameters are treated as input files, and if there is no input file, the program reads the data from the standard input. GNU version of SED home: 

A\ adds a line of text after the current line.

b lable branches to the markup in the script, branching to the end of the script if the branch does not exist.

C\ changes the text of the bank with new text.

D from a template block ( Pattern Space ) location to delete the row.

D Delete the first line of the template block.

I\ inserts text above the current line.

H copies the contents of the template block into an in-memory buffer.

H Append the contents of the template block to the in-memory buffer

G Gets the contents of the memory buffer and overrides the text in the current template block.

G gets the contents of the memory buffer and appends it to the text of the current template block.

The list of characters cannot be printed.

N reads the next input line, processing the new row with the next command instead of the first command.

N appends the next input line to the template block and embeds a new line between them, changing the current line number.

P Prints the line of the template block.

P (uppercase) prints the first line of the template block.

Q Exit sed.

R file reads rows from file.

The T-label if branch, starting from the last row, will cause the branch to be at the command with a label, or to the end of the script, once the condition satisfies or t,t the command.

The T label Error branch, starting with the last line, will cause the branch to be at the command with a label, or at the end of the script, once an error or T,T command occurs.

W file writes and appends the template block to the end of file.

W file writes and appends the first line of the template block to the end of file.

! Indicates that the subsequent command has effect on all rows that are not selected.

S/re/string replaces the regular expression re with string.

= Prints the current line number.

# extend annotations before the next line break.

The following is the replacement tag

G indicates a full-line replacement within the row.

P indicates that the line is printed.

W means writing the line to a file.

X represents the interchange of text in the template block and the text in the buffer.

Y means translating a character to another character (but not for regular expressions)

4. Meta Character Set

^ start of the anchor line as:/^sed/matches all lines beginning with sed.

$ anchor Line ends such as:/sed$/matches all lines ending in sed.

. Match a non-newline character such as:/s.d/matches the s followed by an arbitrary character followed by D.

* Match 0 or more words such as:/*sed/match all the templates are one or more spaces followed by the SED line.

[] matches a specified range of characters, such as/[ss]ed/-match sed and sed.

[^] matches a character that is not within the specified range, such as:/[^a-rt-z]ed/matches the beginning of a letter that does not contain a-r and t-z, followed by the line of Ed.

\(.. \) Save matching characters, such as s/\ (love\) able/\1rs,loveable are replaced with lovers.

& Save Search characters to replace other characters, such as S/love/**&**/,love this into **love**.

\< anchors the beginning of a word, such as:/\<love/matches a line containing a word that begins with love.

\> anchors the end of the word, such as/love\>/matches the line that contains the word ending with love.

X\{m\} repeats characters x,m times, such as:/Oh o\{5\}/matches rows that contain 5 O.

X\{m,\} repeats a character x, at least m times, such as:/o\{5,\}/matches at least 5 rows of O.

X\{m,n\} repeats the character x, at least m times, not more than n times, such as:/o\{5,10\}/matches rows of 5--10 O.

5. Example

Delete: D Command

$ sed ' 2d ' example-----Delete the second line of the example file.

$ sed ' 2, $d ' Example-----Delete the second line of the example file to the end of all lines.

$ sed ' $d ' example-----Delete the last line of the example file.

$ sed '/test/' d example-----Delete all rows containing test in example file.

Replacement: s Command

$ sed ' s/test/mytest/g ' example-----Replace test with mytest in the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.

$ Sed-n ' s/^test/mytest/p ' Example-----(-N) option and the P flag are used together to indicate that only those rows that have substitution occur are printed. That is, if the test at the beginning of a line is replaced with mytest, it is printed.

$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol indicates that a replacement character is added after the string is found. All lines starting with 192.168.0.1 will be replaced with a self-added localhost, which becomes 192.168.0.1localhost.

$ Sed-n ' s/\ (love\) able/\1rs/p ' Example-----Love is marked as 1, all loveable will be replaced with lovers, and the replacement guild is printed.

$ sed ' s#10#100#g ' example-----No matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the delimiter, instead of the default "/" delimiter. means to replace all 10 with 100.

Range of selected rows: comma

$ Sed-n '/test/,/check/p ' example-----All lines in the range determined by the template test and check are printed.

$ Sed-n ' 5,/^test/p ' example-----prints all rows starting from line fifth to the first containing the line starting with test.

$ sed '/test/,/check/s/$/sed test/' example-----for the line between the template test and West, the end of each line is replaced with the string sed test.

Multi-point editing: e Command

$ Sed-e ' 1,5d '-e ' s/test/check/' example-----(-E) option allows multiple commands to be executed on the same line. As shown in the example, the first command deletes rows 1 through 5, and the second command replaces test with a check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.

$ sed--expression= ' s/test/check/'--expression= '/love/d ' example-----A better command than-E is--expression. It can assign values to an SED expression.

read in from file: R Command

The contents of the $ sed '/test/r file ' example-----file are read in and displayed after the line matching the test, and if multiple lines are matched, the contents of file are displayed below all matching rows.

Write file: W Command

$ Sed-n '/test/w file ' example-----All rows containing test in example are written to file.

Append command: a Command

$ sed '/^test/a\\--->this is a example ' example<-----' This is a example '-----appended to the line beginning with test, SED requires that command A has a backslash followed by a backslash.

Insert: I Command

$ sed '/test/i\\-------------------------' example-----if test is matched, insert the text after the backslash into the front of the matching line.

High-risk action:-I Command

#edit env.cfg
Sed-i ' s/target_cpu=.*/target_cpu=arm/g ' ${env_cfg_file}

"." = matches any character (any single character except "\ r \ n"), "*" = matches 0 or more, S is to find target a (target_cpu=.* meaning target_cpu= (+ any string)) and replace with Target B (target_cpu=arm),

Replace the target_cpu=.* with Target_cpu=arm in the entire row range. If there is no G tag, only the first matching target_cpu= (+ any string) of each row is replaced with a target_cpu=arm. In fact, in this example, the addition of the G symbol can be achieved.

Sed can directly modify the contents of a file without using a pipe command or data flow redirection! However, because this action will be directly modified to the original file, so do not take the system configuration to test!

Next: N Command

$ sed '/test/{n; s/aa/bb/;} ' example-----if test is matched, move to the next line of the matching line, replace the AA for this line, change to BB, and print the line, and then continue. N represents Next.

variants: y Command

$ sed ' 1,10y/abcde/abcde/' example-----convert all ABCDE in the 1--10 line to uppercase, note that the regular expression metacharacters cannot use this command.

exit: Q Command

$ sed ' 10q ' example-----After the 10th line has been printed out, SED is exited.

Keep and get: h Commands and G Command

$ Sed-e '/test/h '-e ' $G example-----When the SED processes the file, each row is stored in a temporary buffer called the pattern space, unless the row is deleted or the output is canceled, and all the processed rows are printed on the screen. The pattern space is then emptied and a new line is stored for processing. In this example, when a row matching test is found, it is stored in the pattern space, and the H command copies it into a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the row holding the buffer, then puts it back into the pattern space and appends it to the end of the line that already exists in the pattern space. In this example, it is appended to the last line. Simply put, any line containing test is copied and appended to the end of the file.

To maintain and interchange: h Commands and x Command

$ Sed-e '/test/h '-e '/check/x ' example -----swap mode space and keep the contents of the buffer. That is, the line that contains test and check is swapped.

6. Scripts

The SED script is a list of SED commands that boot the script file name with the-F option when you start sed. Sed is very picky about the commands entered in the script, and cannot have any whitespace or text at the end of the command, separated by semicolons if there are multiple commands in a row. The behavior that begins with # comments lines, and cannot span rows.

Linux sed Command Instance parsing

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.