(GO) linux sed command it's that simple.

Source: Internet
Author: User
Tags c hello world

The Linux sed command is so simple

Original: http://www.cnblogs.com/wangqiguo/p/6718512.html

Read Catalogue

    • Overview
    • Options for SED commands
    • Delete Row
    • New row
    • Replace Line
    • Replace a partial string instead of an entire row
    • Search and lose your travel content
    • Apply a modification to a file
    • Meta-characters in SED regularization
Back to Top overview

The SED command is a non-interactive editor that faces a character stream, which means that SED does not allow the user to interoperate with it. SED is the processing of text content by line. In the shell, it is convenient to use SED to bulk modify text content.

Options to return to the top sed command

sed [options] [action]

Options and Parameters:
-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: Action editing of SED directly in command-line mode;
-F: The action of SED is written directly in a file, and-f filename can run the SED action within filename;
-r:sed's actions support the syntax of extended formal notation. (The default is the basic formal French notation)
-I: Directly modifies the contents of the read file, not the output to the terminal.

function
A: New row, a can be followed by a string, and these strings will appear on a new line (the current next row)
C: Replace line, C can be followed by string, these strings can replace the line between N1,N2
D: Delete the row, because it is deleted, so d is usually not followed by any parameters, directly delete the line represented by the address;
I: Insert line, I can be followed by string, and these strings will appear on a new line (the current line);
P: Print, that is, print out a selected data. Normally p will run with parameter sed-n
S: Replace, can be directly to replace the work, usually this s action can be paired with the formal notation, for example, 1,20s/old/new/g is generally replaced by the matching strings rather than the whole line

The general function is preceded by an address limit, such as [address]function, which represents the line to which our action is to be manipulated. Let's take a look at some examples of how sed is used in a straightforward way.

Back to top Delete row

Test.txt content is as follows
One AA
bb
cc
dd
2e

sed ‘1,2d‘ test.xx 

Output:
cc
dd
2e

where d in 1,2d means delete, and d precedes the address of the deleted row, and a range of addresses, that is, delete lines 1th and 2nd. The representation of an address range is typically m,n for all rows between M and n rows, as well as the line m and nth rows. In the address addressing of an sed, you can use $ to represent the last line, such as m,$, which represents the operation of the M row and all subsequent rows, including the last. M, $d is to delete the M row and all the line contents behind it. Of course, we can also operate on a line, such as 2d, to simply delete line 2nd. In addition to using the numeric range m,n to represent multiline intervals, and m for a single row, we can also use regular expressions to select rows that match the criteria and manipulate them, as well as the above file:

sed ‘/2/d‘ test.txt

Output:
One AA
cc

The above command/2/is a regular expression, in the SED the expression is written in the middle of the/.../two slashes, the regular meaning is to find all the rows containing 2, do the corresponding operation, that is, delete all the rows containing 2, if we just want to delete the line that starts with 2, Just modify the regular expression to:

sed ‘/^2/d‘ test.txt

Output:
One AA
cc
2e

Back to top New row

sed ‘1a hello world‘ test.txt

Output:
One AA
Hello World
bb
cc
dd
2e

Where a command indicates that a row is appended to the specified line, 1a adds a row after the first row, adding the content after a, and if no address qualification is preceded by a, the specified string is added after all rows

sed ‘1i hello world‘ test.txt

Output:
Hello World
One AA
bb
cc
dd
2e

The command I indicates that a row is inserted before the specified line, and the inserted content is the string following it

Back to top replace line

sed ‘1c hello world‘ test.txt

Output:
Hello World
bb
cc
dd
2e

The command C replaces all the contents of the specified line, replaces it with the string that follows it, all additions, deletions, and substitution rows, the address decorations that precede the commands can specify the address space, and the regular expressions, which are applied to all rows of the selected address criteria, for example:

sed ‘/^2/c hello world‘ test.txt

Output:
One AA
Hello World
cc
Hello World
2e

Replace the line that begins with 2 with the contents of the string following the C command

Back to top replace partial string instead of whole line

In sed, in addition to the above command for the entire row of operations, but also provide a replacement command, the command on a line of some string operation, the following is a simple example, or the same text content, execute the following command:

sed ‘s/aa/AA/‘ test.txt

Output:
One AA
bb
cc
dd
2e

What we're talking about here is the S command, and the result is that the AA in our file is replaced with AA, and we look at the S command followed by 3 slash-delimited strings, meaning s/the string to be replaced/a new string/That is, the previous AA that appears in the following AA replacement file. In fact, instead of replacing the first AA encountered in each line, we modify the contents of the file:

Test.txt
One AA
bb
cc
dd
2e
Aaff CCAA
ZZ Ggaa

sed ‘s/aa/AA/‘ test.txt

Output:
One AA
bb
cc
dd
2e
Aaff CCAA
ZZ Ggaa

You can see that the AA in the CCAA of line 6th is not replaced, that is to say, instead of just replacing the first AA string searched for each row, then if we want to replace all the qualifying strings in a row, we can use the parameter G, for example, to modify the command as follows:

sed ‘s/aa/AA/g‘ test.txt

Output:
One AA
bb
cc
dd
2e
Aaff CcAA
ZZ Ggaa

After the last slash followed by the G option, it represents a global substitution, meaning that all old strings in a row are replaced with the new string, not just the first one. As with other operations on the line, the S command can also be used for address selection, with the same address as we did before, that is, in front of s with address space qualification, for example:

sed ‘1s/aa/AA/g‘ test.txt

Output:
One AA
bb
cc
dd
2e
Aaff CCAA
ZZ Ggaa

You can see that the first line has been replaced only, and the other address-qualifying methods are also available, and we can use M,n's qualification, for example:

sed ‘5,$s/aa/AA/g‘ test.txt

Output:
One AA
bb
cc
dd
2e
Aaff CcAA
ZZ Ggaa

Represents a search substitution operation on line 5th until all rows at the end of the file, as well as the address qualification of the S command, which enables qualifying rows to be qualified using regular expressions, and then the search substitution of strings in those rows, for example:

sed ‘/^[0-9]/s/aa/AA/g‘ test.txt

Output:
One AA
bb
cc
dd
2e
Aaff CcAA
ZZ Ggaa

We added the/^[0-9]/in front of the S command, which represents all lines starting with a number, performing s operations

Another thing to note is that the string/new string/s/to be replaced is not certain, and when using the S command, we can use a different delimiter, in fact the character immediately after S is the delimiter, so it is not necessarily a/symbol. For example:

echo ‘aabbccaadd‘ | sed s#aa#AA#g

Output:
Aabbccaadd

The # symbol followed by the s command here is treated as a delimiter.

Go back to the top search and lose your travel content

SED also provides a P command to search for qualifying rows and output the contents of the line without making any other modifications, such as:

Test.txt
One AA
bb
cc
dd

sed ‘2p‘ test.txt

Output:
One AA
bb
bb
cc
dd

You can see that the second line is output, but sed seems to output all the contents of the file again, and the 2nd line is more output, in fact, the SED by default will all the standard input data again output to the standard output, we can add the-n option so that sed is only the output after processing of those rows , not all of the line content obtained from the standard input before the output, for example:

sed -n ‘2p‘ test.txt

Output:
bb

This will only output the results of the P command, and the-n option is generally used in conjunction with the P command, and the other additions, deletions, and replacements of line commands are not required by the-n option.

Back to top will modify the application to the file

All of the experiments we did did not actually modify the contents of the Test.txt file, which means that the changes we see are only output to the console, and the contents of the file Test.txt are not modified, and we can use the-i option to tell SED to directly modify the contents of the file. Instead of outputting the result of the modification to the terminal, for example:

sed -i ‘2d‘ test.txt 

After the command was run, we found that the 2nd line of Test.txt was gone.

Return to Meta-character in the top sed regular

We know that the command in SED can be preceded by an address range, which means that the corresponding action is taken on certain qualifying rows of the file, where we can use regular expressions to select the rows to be manipulated, and the syntax of SED may be somewhat different from the regular syntax of our other commands. Here we need to list the regular meta characters commonly used in SED:

$ = end of line
^ Indicates the beginning of the line
[A-z0-9] represents a range of characters
[^] denotes characters other than characters in the character set

The SED's regular in \ (\) and \{m,n\} need to be escaped
. Represents any character
* Indicates 0 or more
\+ one or more times
\? 0 or one time
\| Representation or syntax

(GO) linux sed command it's that simple.

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.