Sed function parameters

Source: Internet
Author: User
Tags case statement
Function data Function
: Label Create the locations where the commands in script. file are mutually tested.
# Establish a solution
{} A set of commands with the same IP address limit.
! The number of rows.
= Line number ).
A/ Add the User Token.
B label Jump the commands of the test line to the test position created.
C/ Replace the data with the data imported by the user.
D Divide the data.
D Except the first newline letter/prefix in pattern space.
G Copy the metadata from hold space.
G Add data from hold space to pattern space.
H Copy the metadata from pattern space to hold space.
H Add information from pattern space to hold space.
L Nonprinting character in the printed l data is printed using ASCII character.
I/ Insert the data row that the user adds to the consumer.
N Import the next item.
N Add the next expected pattern space.
P Print data.
P Print the first newline letter/prefix in the pattern space.
Q Jump out of SED sort failed.
R Into its internal content.
S Replace string.
T label First, repeat the begin command for the slave node. If the slave node is converted into a cow P>, the slave node command jumps to the label node for the slave node.
W Information is expected to be included in the website.
X Intersection hold space and pattern space content.
Y Transform character.
Parameter number

 

Parameter number

This chapter describes the number of functions provided by all sed by using one function.

| S | d | A | I | c | p | L | r | w | Y |! | N | q | = | # | n | d | p | H | G | x | B | T |

In addition, in each region, the function of introducing the function of function data is described first, and then the format of function data matching the number of addresses is described, here, we also describe how sed reads the data of this function.

4.1 s

The number of bytes in the response (s) indicates the string in the substitute file. The command format is as follows:
[Address1 [,Address2]S/Pattern/Replacemen/[Flag]

The preceding format is described as follows:

  1. Function data s can be used with a maximum of two addresses.
  2. For details about "s/pattern/replacement/[flag]" (solution [12]), refer to the following annotations:
    • Pattern: it is a regularexpression string. It indicates the string to be replaced in the file.
    • Replacement: it is a general string. However, the following characters have special meanings:

       

      1. &: Represents the previous pattern string. For example

        Sed-E's/test/& my car/'item shard name

        Command, & indicates the pattern string "test ". Therefore, the "test" of the item is converted into "test my car ".

      2. /N: indicates the string enclosed by the N/(,/) (appendix A]) in pattern. For example
        Sed-E's // (test/)/(my/)/(CAR/)/[/2/3/1]/'item shard name

        In the command,/1 indicates "test",/2 indicates "my", And/1 indicates the "car" string. Therefore, the "test my car" of the item is converted into "[my car test]" after the row is closed.

      3. /: Use it to return the literal meaning of some special operators (such as the preceding &/), or use it to represent the original line.
    • Flag: it is mainly used to control some alternative scenarios:
      • When the flag is g, all matching strings are replaced.
      • When the flag is 10-digit m, it indicates the M-th matching string in the response line.
      • When the flag is P, it indicates that after the first pattern-compliant string is replaced, the information is marked out.
      • When the flag is W wfile, it indicates that after the first pattern-compliant string is replaced, it is exported to the wfile kernel (if the wfile does not exist, in this case ).
      • When there is no flag, replacement is used to replace the first pattern-compliant string in the Data row.
    • Delimiter: In "/pattern/replace/[flag]", "/" is considered as a delimiter. In addition to blank and newline, you can use any character as delimiter. For example, the following zookeeper command
              s#/usr#/usr1#g

      In the above command,/Verb | # | is delimiter. If "/" is used as the delimiter, sed will generate a response when "/" in pattern and replacement is used as the delimiter.

  3. Example:
    • Topic: replacing input. dat reset (if not specified in the backend, all false settings file names are input. in dat), the "1996" string is "1997", and these data rows are stored in year97.dat at the same time.
    • Description: Use the function data s to indicate that SED will replace the "1996" string with "1997 ", in addition, the flag W in s argument indicates that SED will store the received row in year97.dat.
    • Sed command column:
      sed -e 's/1996/1997/w year97.dat' input.dat

The function D indicates the data deletion line. The command format is as follows:

[Address1 [,Address2]D

The preceding format is described as follows:

  1. Function D can work with up to two addresses.
  2. The following figure shows the dynamic conditions of SED statements:
    1. Divide the information that matches the number of addresses in the pattern space.
    2. Import the next item into pattern space.
    3. Repeat sed script.
  3. Example: Skip Section 3.3.

4.3

Function A indicates adding the information to the file. The command format is as follows:

 

[Address1]A/Information imported by the user

The preceding format is described as follows:

 

  1. Function A can work with a maximum of one address.
  2. The "/" character next to the function number A indicates the end of the row. The information imported by the user must be imported from the next line. If the data exceeds one row, you must add "/" at the end of each row "/".
  3. When the information in the pattern space is generated, sed follows the information that the user receives.
  4. Example:
    • Topic: Add "Multi-job system" after the "Unix" string. The contents of input. dat defaults are as follows:

      UNIX
    • Description: use function a to add the imported data to the data row containing the "Unix" string.
    • The SED command column is as follows:
      Sed-e '/Unix/A/multi-industry systems' input. dat

      After the preceding command is executed, the result is as follows:

      UNIX multi-job system

4.4 I

The data volume I indicates that the data is inserted into the file. The command format is as follows:

[Address1]I/Information imported by the user

The preceding format is described as follows:

  1. Function I can work with a maximum of one address.
  2. The "/" character next to the function number I represents the end of the row. The information imported by the user must be imported from the next row. If the data exceeds one row, you must add "/" at the end of each row "/".
  3. The following figure shows how sed inserts an animation into a row: Before the information in pattern space is imported, sed first outputs the information that the user inputs.
  4. Example:
    • Topic: Apply the article copyright to the Information row of the Central Research Institute, which is included in the input. dat documents, including "Dean: Li xiaozhe. The contents of the Input. dat file are as follows:

      Dean: Li xiaozhe
    • Description: using the function data, I used the article copyright to jump to the information bank of the Central Research Institute, which included "President Li xiaozhe.
    • The SED command column is as follows:
      Sed-e '/ Emy chief: Li xiaozhe/I/Article copyright at the Central Research Institute 'input. dat

      After the preceding command is executed, the output is as follows:

      Article copyright: Li xiaozhe, Chief of the Central Research Institute

4.5 c

Function C indicates the information in the change file. The format is as follows:

[Address1 [,Address2]C/Information imported by the user

The preceding format is described as follows:

  • Function C can work with up to two addresses.
  • The Function C character is followed by the "/" character to indicate the end of the line. The information that the user inputs must be imported from the next line. If the data exceeds one row, you must add "/" at the end of each row "/".
  • Sed changes: when the information in pattern space is generated, sed changes it to the information imported by the user.
  • Example: Example 2 and 3 of section 3.1.

4.6 P

The number of letters (p) indicates that the data is printed. The command format is as follows:

[Address1 [,Address2]P

The preceding format is described as follows:

  1. Function data P can work with up to two addresses.
  2. The following figure shows how sed copies a copy of pattern space to mark the output.
  3. Example: Internal content of section 3.4.

4.7 L

The number of letters (L). In addition to listing the nonprinting character in the information in ASCII notation, the value is equal to the number of letters (P. For example, Mark ^ [in input. dat values as ASCII characters.

 

The Great ^[ is a movie starring Steve McQueen.

After the sed-e 'l' input. dat command is executed, the result is as follows:

The Great /003 is a movie starring Steve McQueen.The Great     is a movie starring Steve McQueen.

The second row above is expected to be sed's auto-generated (please refer to our solution [].

4.8 R

Function data R indicates that the data is stored in the file. The command format is as follows:

[Address1]RIts region name is anonymous.

The preceding format is described as follows:

  1. Function data R can work with a maximum of one address.
  2. In the instruction, there is only one space between the function data R and Its alias.
  3. The following figure shows the situation of SED internal operations: After the pattern space content is generated, sed releases its internal content and content. When it does not exist, sed tries to execute other commands without generating any warning messages.
  4. Example: Example 3 of section 3.1.

4.9 million

The number of letters is W, indicating that the file is stored in it. The command format is as follows:

[Address1 [,Address2]WIts region name is anonymous.

The preceding format is described as follows:

  1. The number of letters can work with up to two addresses.
  2. In the instruction, there is only one space between the number of letters w and its nickname.
  3. Sed sends the information about the action, for example, to upload the content in pattern space to it. When the data volume is imported, it will replace (overwrite) the original data in the case. In addition, when it does not exist, sed will re-generate it.
  4. Example 2: Skip section 3.1.

4.10 y

Letter Number y indicates the character in the Response item. The command format is as follows:

[Address1 [, Address2] y/Xyz.../ABC .../

The preceding format is described as follows:

  1. The function number can be used with a maximum of two addresses.
  2. In the command,/ABC.../xyz.../(X, Y, Z, a, B, c represents some characters) is the argument of Y. The characters ABC... And xyz... must be the same.
  3. Sed when the row is canceled, convert the character in pattern space to X, B to Y, and C to Z ....
  4. Example:
    • Topic: change the "small letter" in the input. dat example to "large. The contents of input. dat defaults are as follows:

      Sodd's Second Law:        Sooner or later, the worst possible set of         circumstances is bound to occur.
    • Description: Use the letter number y to indicate sed as the letter size.
    • The SED command column is as follows:
       sed -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' input.dat

      The result of running the above command is as follows:

      SODD'S SECOND LAW:        SOONER OR LATER, THE WORST POSSIBLE SET OF         CIRCUMSTANCES IS BOUND TO OCCUR.

4.11!

Number of letters! This parameter indicates the number of rows that do not exist. When there are the following commands,

[Address1 [,Address2]!Function data

Indicates the number of rows that do not match the specified address. For example, in addition to the "1996" string and all the data rows, the following command is used to complete the operation:

sed -e '/1996/!d' input.dat

4.12 n

N indicates the next row of data. The command format is as follows:

[Address1 [,Address2]N

The preceding format is described as follows:

  1. The number of functions N can be used with a maximum of two addresses.
  2. The following figure shows the conditions when sed splits the row into the next row:
    1. Outputs the information in pattern space.
    2. Route the next item to pattern space.
    3. Indicates the next batch operation command.
  3. Examples (can be compared with the examples in [section4.18 ):
    • Topic: outputs the data of the Input. dat dataset. The contents of the Input. dat file are as follows:

      TheUNIXOperationSystem
    • Description: On the command Column
      1. Grant the control right of the resource (subject to [section2.5]) to the command by selecting-n.
      2. Replace the data row (odd data row) in pattern space with the function data N ).
      3. Extract the data (even data) in pattern space using the function data p.

      Finally, the entire batch shows only the even data in the original batch.

    • The SED command column is as follows:
      sed -n -e 'n' -e 'p' infro.dat

      After the preceding command is executed, the result is as follows:

      UNIXSystem

4.13 Q

Q indicates the SED hop. The command format is as follows:

[Address1]Q

 

The preceding format is described as follows:

  1. Function data Q can work with a maximum of one address.
  2. When sed performs a hop, it stops loading the pattern space data, and stops sending the data to the standard output data at the same time.
  3. Example:
    • Topic: Specifies the delimiter command in script_file for a file, unless it encounters a "Linux" string.
    • Note: regardless of the commands in script_file, you only need to use the commands/Linux/Q in the Command column, q: When sed Encounters "Linux", he will perform a jump.
    • The SED command column is as follows:
      sed -e '/Linux/q' -f script_file  input.dat

Number of rows = indicates the number of rows of the printed data. The command format is as follows:

[Address1,[Address2]=

The preceding format is described as follows:

  1. Number of functions = the maximum number of two addresses.
  2. When a row is created, the row data is first exported before the row data is generated.
  3. Example:
    • Topic: print the number of data rows in the input. dat file. The contents of input. dat are as follows:

      The UNIXOperating System
    • Description: The row number of the data is printed using the function data =.
    • The SED command column is as follows:
       sed -e '=' input.dat

      After the preceding command is executed, the result is as follows:

      1The UNIX2Operating System

4.15 #

In the script file, the text after the number of letters # Is an annotation. When the annotation text exceeds multiple lines, the lines must be separated.

N indicates that the next item is added to the pattern space. The command format is as follows:

[Address1,[Address2]N

The preceding format is described as follows:

  1. The number of functions N can be used with a maximum of two addresses.
  2. When sed completes a row, the next row of data is added to and added to the pattern space. The row length is separated by embedded newline character. In addition, when the parameter is replaced, the line character can be/N to match.
  3. Example:
    • Topic: Merge the following two rows. The contents of input. dat are as follows:

      The UNIXOperating System
    • Description: Use the function data N to place the two rows in pattern space, in the use of the function data number S // N // separate the two rows of data;/N is replaced by a blank space, so that the two rows of data become one row of data.
    • The SED command column is as follows:
       sed -e 'N' -e 's//n/ /' input.dat

      After the preceding command is executed, the result of the command output is as follows:

      The UNIX Operating System

4.17 d

The number D indicates the first row of data in the pattern space. The command format is as follows:

[Address1,Address2]D

The preceding format is described as follows:

  1. Function D can be used with a maximum of two addresses.
  2. The comparison between function D and function D is as follows:
    1. When there is only one row in pattern space, D and D act the same.
    2. When there are multiple rows in pattern Space
      1. D indicates that only the first row of information in pattern space is deleted; D indicates that all rows are divided.
      2. D indicates that the next item is not added to the pattern space after the row is deleted, and the remaining item is re-written into the SED script; d. Write the SED script in the next row.
  3. Example: The second example in section 3.3.

4.18 P

The number of functions P indicates the first row of information in pattern space. The command format is as follows:

[Address1,Address2]P

The preceding format is described as follows:

  1. Function data P can be used with a maximum of two addresses.
  2. P and P are the same except for the data rows in the pattern space.
  3. Examples (can be used with examples in [section4.12 ):
    • Topic: extract the odd data row in the input. dat file. The contents of the Input. dat file are as follows:

      TheUNIXSystem
    • Description: On the command Column
      1. Grant the control right of the resource (subject to [section2.5]) to the command by selecting-n.
      2. Add the even data row to the odd data row in pattern space using the function number n.
      3. Extract the first line (odd number row) in pattern space using the function data p.

      After the odd data rows are displayed, the remaining data rows (even rows) in the pattern space are displayed. Finally, only the original odd data row information is displayed.

    • Sed command column:
      sed -n -e 'N' -e 'P' infro.dat

      After the preceding command is executed, the result is as follows:

      TheSystem

4.19 H

H indicates storing the information of pattern space to hold space. The command format is as follows:

[Address1,[Address2]H

The preceding format is described as follows:

  1. Function H can be used with a maximum of two addresses.
  2. When sed blocks row-based storage, the original data in the hold space is discarded.
  3. When all sed rows end, the hold space content is automatically cleared.
  4. Example: Skip section 3.4.

4.20 H

The only difference between function H and function H is that when sed reads row H, the data is overwritten to hold the original data in space, while H, the information is "add (append)" after the original information of hold space. For more information, see section 3.2.

4.21 GB

The function g indicates the action opposite to the function H. It indicates that the hold space content is put back into the pattern space. The command format is as follows:

[Address1,Address2]G

 

  1. The function g can be used with a maximum of two addresses.
  2. When SED was taken back to the action, the original data in pattern space was discarded (overwrite.
  3. Example: Skip section 3.4.

4.22 GB

The only difference between the function g and the function g is that when sed reads a row G, the original data in the pattern space (overwrite) is discarded, while G, the information is "add (append)" after the original information of pattern space. For example, refer to section 3.2 for example 1.

4.23 x

X indicates the information in the hold space and pattern space. The command format is as follows:

[Address1,[Address2]X

Function data x mostly works with the function data of other processing hold space. For example, replace 1st rows of data in input. dat bytes with 3rd rows of data. At this time, the function H and X are used together. Among them, the data is stored in the hold space with the function data h; when the 1st rows of data appear in pattern space, returns the content of the hold space and pattern space based on X. In this way, 3rd rows of data are replaced by 1st rows of data. The command column is as follows:

sed -e '1h' -e '3x' input.dat

4.24 B,: Label

Function number: the function of similar basic commands in sed script to function B. Here, the number of functions: Create a tag; the number of functions B refers to the Command Branch of the next line to the mark place line. Number of letters: matches with B in the script file as follows:

... Zookeeper command M1: Remember zookeeper command m2... [address1, [address2] B [remember]

When sed tries to [address1, [address2] B [remember] the command, for example, the information in pattern space matches the number of addresses, as a result, sed will branch the location of the next row to the mark set by: ([14, that is to say, the "commandid command m2 "... rows. In addition, if no message is recorded after the number B in the command, sed splits the next line of command branch to the end of the script file, this allows sed script to have a similar case statement structure in C statement.
Example:

  • Topic: Re-print the start Letter of the input. dat row 40 times. The contents of input. dat defaults are as follows:

     

    ABC
  • Note: The following Code uses commands B P1 and P1 to construct a loop to add letters to a row. At the same time, 40 letters appear, it also uses command B to jump out of the circle. The following uses the first row of information "A" as an example to describe how to add 39 "a" to the same row in the example:
    1. Use the command S/A/AA/( 4.1 4.1) to convert "A" to "AA ".
    2. Construct a loop using commands B P1 and: P1, which aims to make the above-mentioned Dynamic Response line reversed. Every row in a circle, the "A" on the Data row will have one more. For example, the first cycle changes to AA, and the second cycle changes to aaa "....
    3. Use the command [ABC]/{40/}/B (Decoding [15]) as the condition for stopping the labeling. When the data row has 40 A records, the number of data records B jumps to the last row of the command to stop reading this row.

    In the same way, the same way for other data rows.

  • The SED command column is as follows:
    sed -e '{:p1/A/s/A/AA//B/s/B/BB//C/s/C/CC//[ABC]/{40/}/bb p1}' input.dat

4.25 TB

Basically, the function of function T is similar to function B, except before the branch of function t, the replacement command before the replacement command is successfully executed. In the script file, the conditions are as follows:

... Please refer command M1: Remember please refer to command m2... S /... /... /[address1, [address2] T [] command m3

In this case, unlike function B, when function data is t branch, the previous replacement command is checked for success and No. If the command succeeds, branch is added. If the command fails, branch is not performed. If the command fails, the next batch operation is executed, for example, M3.
Example:

  • Topic: replace A1 in input. dat into C1, C1 into B1, and B1 into A1. The content of input. dat plugin is as follows:

    Generation B1 A1 B1 C1 A1 C1
  • Description: input. in dat flood, all the data rows only need to be modified for one operation, but in order to avoid the data being replaced for multiple times, therefore, the function data t is used to form a similar C statement structure in sed script, after each row of data is replaced with a token, the function data cannot be used for the token immediately.
  • Sed command column:
    sed -e '{s/A1/C1/ts/C1/B1/ts/B1/A1/t}' input.dat
4.16 n 4.14 = 4.2 d

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.