Shell Script Programming-sed Editor (1)

Source: Internet
Author: User

1. The SED editor uses the SED editor, called the Stream editor, to edit the data flow based on a set of rules provided beforehand. Format: SED option script file (1) OPTION-E script when processing the input, add the command specified in the script to the running command, you can process multiple commands at the same time, and-f file The command specified in file while processing the input Added to a running command;-n does not generate output for each command, wait for the print command to output (2) SED basic use the SED editor at the command line, such as the Replace command
    $  echo""sed's/test/big test/'  This is    a big test
Replace SRC with DST with the S-ID replacement command, ' s/src/dst/', in the command-line echo data. Edit the data in the file, you need to specify the file to be processed
    Cat Test    There    is Dogssed's/dogs/cats/'  test There is-    cats 
It should be noted that SED does not modify the data in the file, only the data is edited and output to stdout. You need to use the-e option when you need to specify multiple processing commands.
    sed ' s/dogs/cats/; s/two/three/ ' Test    There is cats
A semicolon is required between the commands, and there can be no spaces between the end of the command and the semicolon. Read the processing command from the text, use the-F option to specify the command file
    Cat script    s/dogs/cats/    s/two/three/    sed -F script test
2. Replace the command (1) format: s/src/dst/flags (2) Replace the tag number: Indicates that the first match of each line in the text needs to be replaced by G: Indicates that all matching data appearing on each line in the text is replaced by P: indicates that the contents of the modified line need to be printed w fil: Replace the The results are written to the file by default, the Replace command modifies only the first matching data in each line of text, and with a numeric marker, you can specify that the number of matches in each line in the text need to be replaced, and the G tag to specify that all matches for each line in the text need to be replaced.
$CatTest There is cats and dogs there are, cats and both dogs $sed 's/two/three/'Test There is three cats and dogs there is three cats and both dogs $sed 'S/TWO/THREE/2'Test There is cats and three dogs there are both cats and three dogs $sed 's/two/three/g'Test There is three cats and three dogs there are three cats and three dogs
By default, the data processed by SED will be output to stdout, using the-N option to specify that the processed data is not output, and you can use the-N option with the add P tag to output only the data that needs to be processed. (3) When processing characters with/in the processed characters, the substitution occurs with an exception and needs to be escaped with \, for example:
    sed ' s/\/bin\/bash/\/bin\/csh/ ' /etc/passwd
The SED command also allows the use of other separators, such as an exclamation mark
    sed ' s!/bin/bash!/bin/csh! ' /etc/passwd
In this example, the path name is easier to read and understand. In shell scripts, it is common to use the words that need to be replaced characters in a variable, and the error occurs when you replace them directly, and you need to specify the variables that need to be replaced by using single quotation marks with double quotes;
    Cat Test. SH     var=cats    sed's/dogs/' "$var" '  /' test
(4) Specify filtering options by default, the SED command acts on all lines of text data and can be routed by specifying filtering options. There are two ways of line addressing: The number range of the row and the addressing of the text mode filter number, you can specify the line number to be processed by using a single number, you can specify the row range to be processed using the starting line number, the comma, the ending line number, and you can use the starting line number, comma, or $ to specify a line from
$CatTest There is dogs there is a dogs there is a dogs there is a dogs $sed '2s/dogs/cats/'Test There is dogs there is a cats there is a dogs there is a dogs $sed '2,3s/dogs/cats/'Test There is dogs there is a cats there is a cats there is a dogs $sed '2, $s/dogs/cats/'Test There is dogs there is a cats there is a cats there is a cats
The method of addressing text filtering, using/string/to specify the line for matching characters.
Catsed'/cats/s/two/three/p'  testthere is three cats
You can use regular expressions to create advanced text patterns for filtering. Using the combo command, you can apply addressing to all commands, such as:
sed ' 2,${s/dogs/cats/; s/two/three/} ' test
Just use {} to combine all the commands you want to work with. 3. Delete, insert, modify, convert commands (1) Delete command format: sed ' d ' input_file when no matching option is specified, the D command deletes all rows, usually when you use the Delete command, you need to specify the row addressing, specify the line number or the character match two ways to apply; For example: sed ' 3d ' input_file; or specify the line range, for example: sed ' 2,3d ' input_file or sed ' 3, $d ' input_file; The text match pattern is consistent with the substitution command usage, for example: sed '/keyword/d ' Input_file;
$Cattestthe Line Num is1The line Num is2The line Num is3$ sed '2, $d'testthe Line Num is1$ sed '/2/$d'testthe Line Num is1The line Num is3
It is also necessary to note that the D command does not modify the contents of the original file, but modifies the output. (2) Insert and append text format: sed ' i\string line ' test--inserts text into the previous row of the specified row sed ' a\string line ' test--append text to a row after the specified lines
$Cattest the line num is1The line Num is2The line Num is3$ sed '2i\this is a test line'testthe Line Num is1This was a test linethe line num is2The line Num is3$ sed '2a\this is a test line'testthe Line Num is1The line Num is2This was a test linethe line num is3
(3) Modify the line format: sed ' c\string text ' input_file Modify the command and insert, additional formatting is basically the same, after execution will replace the specified line content modification command can use matching mode. (4) Conversion command format: sed ' [address]y/inchars/outchars/' input_file conversion command y is the only sed edit command that can handle a single character
Cat12 3sed'y/12/45  ' 4 5 3
Require Inchars and outchars length to be the same, otherwise will error. The transform command replaces all occurrences of the character in the text.

Shell Script Programming-sed Editor (1)

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.