Shell Scripting Learning Notes (3) Use of SED commands

Source: Internet
Author: User

This article mainly refers to "Sed&awk".


We usually use the editor, such as VI, is an interactive editor, is to modify which place, the first place to locate, usually by moving the cursor to complete. SED is a non-interactive, character stream-oriented editor, or stream editor. The SED processes the input stream directly and sends the results to the standard output. SED works by interpreting the script that specifies the action that will be performed.


Sed reads an input line from the input file each time, generates a backup of the input line, and executes the instruction action specified in the script for the backup. As a result, changes to the input rows do not affect the actual input rows. Also, the working mode of SED is one row per processing of the input stream. Suppose there is an sed script called scriptfile, which we can execute by sed-f scriptfile inputfile. The procedure is to read the first line in the input file Inputfile, and after each statement in the ScriptFile script file is processed on the line, it is read into the second line, knowing the end. This is the working process of SED.


Examples of simple applications of SED:

Sed ' s/ma/,massachusetts/' list

This directive means to replace the MA in the list file with Massachusetts, and if you want to specify multiple instructions, you can use-e, for example

Sed-e ' s/ma/,massachusetts/'-e ' s/pa/,pennsylvania/' list

or separated by semicolons.

Sed ' s/ma/,massachusetts/; s/pa/,pennsylvania/' list


Directive sed-n-E ' s/ma/,massachusetts/' list means that the row does not output the display, which is the role of-N: The automatic output of the organization input line.


Sed mainly has these three command line options:

-e means editing subsequent instructions

-F indicates the file name following the script

-N represents the automatic output of an organization's input rows.


Basic SED command


Syntax format: [Address]command

Address represents a row, which can be a line number, a line that matches a regular expression, and so on. The following command is the SED command.

You can also use braces to group multiple instructions, so that multiple directives function has been a line, in the following format:

address{

Command1

Command2

Command3

}


Comments

As with the shell, the comment uses the #


Replace

[Address] S/pattern/replacement/flags

This means that the part of the address line that matches the pattern is replaced with replacement.

Of these, flags have several values:

A number from N 1 to 512 that represents the substitution of the nth occurrence of the specified pattern in this mode.

G makes a global change to all occurrences of the pattern space, and by default only replaces the first occurrence of the situation.

P Print Mode space content

W file writes the contents of the schema space to the file files.


Delete D

For example, delete blank line:/^$/d

D in front of is what we call the Adress section, which represents these lines, here refers to the empty rows, D to delete the rows.


Append, insert, and change

[Address]a\

Text


[Address]i\

Text


[Address]c\

Text

The insert command places the provided text in the current line of the pattern space before the append command places the text after the current line, and the change command replaces the contents of the pattern space with the provided text.


List

The list command is L, which displays the contents of the pattern space and displays nonprinting characters as two-digit ASCII codes.


Transformation

[Address] Y/abc/xyz

This command converts each character in the string ABC to the equivalent character in string xyz.


Exit command

Q

This command causes SED to stop reading the new input rows and stop sending them to the output.





Shell Scripting Learning Notes (3) Use of SED commands

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.