Sed learning Summary

Source: Internet
Author: User

When I first came into contact with SED, this tool originated from the Linux kernel. It felt very complicated. It was full of things such as // and I couldn't understand it. Now I am working on Android. I want to see his compilation process, just like those. some content is inserted into the MK file to show that the file is running ,. there are many MK files. It is impossible for me to insert a row in the first place. So I thought of sed. Reading sed & awk on the Internet seems to be a good book. All Chinese characters look uncomfortable, And the handwriting is fuzzy. I read the original English version, but it may not be long. Otherwise, I cannot afford it.

Sed I personally think it is mainly used to insert a bash script. I have never used a script, and I have never done any typographical content. Let's see if I insert a row at the top of all. mk files:

#! /Bin/bash

While ($ #> 0 ))
Do
Sed-I '1i/
$ (Info this is the file '"$1" 'In in)' $1
Sed-I '$/
$ (Info this is the file '"$1" 'end)' $1
Shift
Done

Return 0

 

Run the following command:

$./Insert. Sh 'Find.-Name *. mk'

 

Let's take a look. I have at least 100 files modified. I can't change them if I need to manually modify them one by one.

Bash content I will not introduce. Let's take a look at sed. The-I option specifies to modify the source file. In fact, an output is saved internally and the output overwrites the source file. Single quotes prevent Bash from parsing characters, such as $, and $1 needs to be parsed. It uses the format of double quotes in single quotes. 1 indicates the address and the first line. Similarly, $ indicates the last line. I insert a row before the row of the current row, and a insert a row at the end of the row of the current row.

 

In short, sed uses the Address followed by the command. The difference is that there may be two addresses. in this format, one-address, two-Address indicates all content between two addresses. Some commands cannot use this format. Multiple commands at the same address are enclosed by {} and written by a branch, as follows:

Address {

Command1

Command2

Command3

}

There is another way. For example, if you want to imitate grep to find a row containing the "hello" character string in the file and print the row number and content, you can do this:

Sed-n'/Hello/{=; p} 'file

Among them, the-n option prevents other rows from being output, = the output row number, and P prints the row content. In my GNU sed version, the two commands can be placed in one line. POSIX does not seem to support these commands. This example is meaningless. It seems more comfortable to use grep directly.

 

The address is usually expressed in a regular expression and placed. Of course, you can use numbers to specify the rows. For example, in the script above, 1 indicates the first row and $ indicates the last row. However, the application estimation is not large.

 

Generally, the SED execution flow is a line of input, matching the address. If the address is met, the command is executed. Of course, there are statements that change this process, such as the N command.

 

Next, paste all sed commands in sed & awk. I modified the order:

=[ Address] =

Write to standard output the line number of addressed line.

P[ Address1[, Address2] P

Print the addressed line (s). Note that this can result in duplicateoutput unless default output is suppressed by using "# N" orthe
-NCommand-line option. Typically used before commands that change flowcontrol (D,
N,B) And might prevent the current line from beingoutput.

A simple print command prints the row number and content without any modification. It only outputs the output. The sed-N option will block general output, which requires P to print the content.
I[ Address1] I/

Text

InsertTextBefore each line matchedAddress. (See
AFordetails onText.)

A[ Address] A/

Text

AppendTextFollowing each line matched
Address. IfTextGoes over more than one line, newlinesmust be "hidden" by preceding them with a backslash.TextWill be terminated by the firstnewline that is not hidden in this
Way.TextIs not available in the pattern spaceand Subsequent commands cannot be applied to it. The results of thiscommand are sent to standard output when the list of editing commandsis finished, regardless of what happens
The current line in thepattern space.

C[ Address1[, Address2] C/

Text

Replace (Change) the lines selected by the addressText. When a range of lines is specified, all lines as a group are replaced by a single copyText. The newline following each lineText
Must be escaped by a backslash, effectthe last line. The contents of the pattern space are, in effect, deleted and no subsequent editing commands can be applied to it (orText).

These three commands change the content of the pattern space (that is, processing each line of input in this space), but modify (insert I/A and change C) contents cannot be modified.
L[ Address1[, Address2] L

List the contents of the pattern space, showing nonprinting charactersas ASCII codes. Long lines are wrapped.

I have never used this command, but you can use this command to display a few spaces, on the premise that you remember the ASCII code.
D[ Address1[, Address2] D

Delete line (s) from pattern space. Thus, the line is not passed to standardoutput. A new line of input is read and editing resumes with firstcommand in script.

N[ Address1[, Address2] N

Read next line of input into pattern space. Current line is sent tostandard output. New line becomes current line and increments linecounter. Control passes to command following
NInstead of resuming at the top of the script.

Q [ Address] Q

Quit whenAddressIs encountered. theaddressed line is first written to output (if default output is notsuppressed), along with any text appended to it by previousAOr
RCommands.

The use of these three commands can be replaced by the B command, the D and N commands are used for some combinations, and the D modifies the process, N command does not modify the process (that is, execute the next line), but the content of the mode space is modified.
S[ Address1[, Address2] S/ Pattern/ Replacement/[ Flags]

SubstituteReplacementForPatternOn each addressed line. If patternaddresses are used, the pattern
//Represents thelast pattern address specified. The following flags can be specified:

N

ReplaceNTh instance/Pattern/On each addressed line.NIs any number in the range 1 to 512, andthe default is 1.

G

Replace all instances/Pattern/On eachaddressed line, not just the first instance.

P

Print the line if a successful substitution is done. If severalsuccessful substitutions are done, multiple copies of the line will beprinted.

W File

Write the lineFileIf a replacementwas done. A maximum of 10 different
FilesCan be opened.

Y[ Address1[, Address2] Y/ ABC/ XYZ/

Transform each character by position in stringABCTo its equivalent in stringXYZ.

Both of the above are replacement commands. Replacement of the Y command is character-by-character. For example, Y/ABC/XYZ/replaces all a with X, B with Y, and C with Z. The S command is used in many cases. The empty mode mentioned above is like this. For example:
/Hello/S // HAI/g
This line indicates that if the current row contains the string "hello", we will change all the "hello" strings of this row to the "Hai" string.
The/followed by S is a delimiter. You can use another delimiter, for example, S! Hello/ME! HAI/you! Here Delimiter is !, Of course, this is the case where a large number of characters exist in the regular expression. You can also keep escaping them and change them to S/Hello/ME/Hai // you /, this looks complicated.
In replacement, metacharacters can be used to replace the matched content in pattern. For example, if your pattern is El * O and the matched content is helllllllo, replace it with this string.
/N, n is a number used to replace the content between/(and/) in pattern. The first is/1, the second is/2, and so on. There is another situation:
S/[Tab] //
/2
The [Tab] above indicates that you type the tab key. Converts the second tab to a new line. This is better: S/T/n/2, because I cannot directly press the tab key on the command line. I do not know the row of the script.
R[ Address] R
File

Read contentsFileAnd append after thecontents of the pattern space. Exactly one space must be putRAnd the filename.

W  [ Address1[, Address2] W
File

Append contents of pattern spaceFile. This action occurs when the command is encountered rather than whenthe pattern space is output. Exactly one space must separateWAnd the filename.
A maximum of 10 differentfiles can be opened in a script. this command will create the file ifit does not exist; if the file exists, its contents will beoverwritten each time the script is executed. multiple write commandsthat direct output to the same file
Append to the end of the file.

Reading and Writing files is meaningless.
: : Label

Label a line in the script for the transfer of controlBOr
T.LabelMay contain up to seven characters. (The POSIX standard says that an implementation can allow longerlabels if it wishes to. gnu sed allows labels to be of any length .)

B[ Address1[, Address2] B[ Label]

Transfer control unconditionally (Branch):LabelElsewhere inscript. That is, the command followingLabelIs the next command applied to thecurrent line. If
NoLabelIs specified, control falls through to the end of the script, so no more commandsare applied to the current line.

T[ Address1[, Address2] T
[ Label]

Test if successful substitutions have been made on addressed lines, and if so, branch to line marked:Label. (See
BAnd:.) If label is notspecified, control falls through to bottom of script.

Obviously, the label is used to assist the B and T commands and changes the execution process. The T command is used to check whether the replacement is successful, so it is generally followed by the S command. If the previous command line is successful, the jump is executed.
N[ Address1[, Address2] N

Append next input line to contents of pattern space; the new line isseparated from the previous contents of the pattern space by a newline. (this command is designed to allow pattern matches within SS twolines. using/N to match the embedded newline,
You can matchpatterns into SS multiple lines .)

D[ Address1[, Address2] D

Delete first part (up to embedded newline) of Multiline pattern space createdby
NCommand and resume editing with First Command inscript. If this command empties the pattern space, then a new lineof input is read, as if
DCommand had been executed.

P[ Address1[, Address2] P

Print first part (up to embedded newline) of Multiline pattern spacecreated
NCommand. SamePIf
NHas not been applied to a line.

The combination of these commands can achieve some good results. Example:
/^ $ /{
N
/^/N $/d
}
Change multiple empty rows to one empty row.
/UNIX$/{        N        //nSystem/{        s// Operating &/        P        D        }}
It is used to change the UNIX/nsystem string to Unix operating/nsystem.
G[ Address1[, Address2] G

Copy (get) Contents of hold space (seeHOrHCommand) into the pattern space, wiping outprevious contents.

G[ Address1[, Address2] G

Append newline followed by contents of hold space (seeHOr
HCommand) to contents ofthe pattern space. If hold space is empty, a newline is stillappended to the pattern space.

H[ Address1[, Address2] H

Copy pattern space into hold space, a special temporary buffer. Previous contents of hold space are wiped out.

H[ Address1[, Address2] H

Append newline and contents of pattern space to contents of the holdspace. Even if hold space is empty, this command still appends thenewline first.

X[ Address1[, Address2] X

Exchange contents of the pattern space with the contents of the holdspace.

These commands mainly use hold space, which is a space for processing the current content. Hold space is similar to a repository. At the beginning, the hold space is empty. The command here is a test of flexibility and has a great effect. If it is used badly, it will be useless.

Paste a piece of code to enjoy the following:

#! /bin/sh# phrase -- search for words across lines# $1 = search string; remaining args = filenamessearch=$1shiftfor file dosed '/'"$search"'/bNhs/.*/n///'"$search"'/bgs/ */n/ //'"$search"'/{gb}gD' $file done

This is a bash script. You can guess what the script is for. For example, a phrase has three lines or more.

 

Okay. There are so many things in summary, and so are sed. Record the awk when you are free.

 

16:47:10

 

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.