Sed(MeaningStream EditorIs a common command line program in Unix/linux. SedDocumentOr text in the stringAfter a series of editing commands are converted to another format output, that is, text replacement. Sed is usually used to match the text of one or more regular expressions for processing.
Enter sed -- help to view help information:
amosli@amosli-pc:~/learn/$ -- [OPTION]... {script-only--no-other-script} [input--n, --quiet, ---e script, --expression=-f script-, --=script----follow--i[SUFFIX], ---place[= place (makes backup -l N, --line-length=-wrap length the `l ---r, --regexp--s, ---u, -------e, --expression, -f, or -- option is given, -option argument is taken as the the standard input is read.
Syntax:
[OPTION]... {script-only--no-other-script} [input-]...
Parameter-instance:
Test File: test.txt
amosli@amosli-pc:~/learn/sed$ cat test.txt hi,this is sed command test filelinux world is so amazingyou will like it!
1. Simple text replacement
Common formats:
pattern/replace_string | pattern/replace_string
Here, pattern is the pattern, and replace_string is the replacement word. That is, replace_string with the pattern.
For example, convert "will" to uppercase ".
amosli@amosli-pc:~/learn/$ test.txt hi,this is command test !
Use standard input:
amosli@amosli-pc:~/learn/$ |
2.-I parameter: Apply the replacement result to the original file.
By default, sed does not modify the original file because the formula is as follows: sed editor processes the file (or input) row by row and sends the result to the screen. The specific process is as follows:Sed stores the currently processed rows in a temporary cache.(Also known as mode space), and then process the rows in the temporary buffer, and then send the row to the screen.Sed deletes a row from the temporary buffer after processing.And then read the next line for processing and display. After processing the last line of the input file, sed stops running. Sed stores each row in a temporary buffer and edits the copy. Therefore, the original file is not modified.
amosli@amosli-pc:~/learn/$ -i test.txt amosli@amosli-pc:~/learn/$ command test !
English prompt:
-i[SUFFIX], ---place[= place (makes backup extension supplied)
3. About the suffix/g
/G indicates to replace all text that meets the conditions by default.
amosli@amosli-pc:~/learn/$ |
/Ng indicates replacement starting from the nth compliant condition.
amosli@amosli-pc:~/learn/$ |
4. About the character '/'
The character '/' plays a separator in the sed command,'|' And ':' have the same effect as '/' and can be used as delimiters.
For example:
amosli@amosli-pc:~/learn/$ | -pc:~/learn/$ |
When the delimiter appears inside the style, you can use the prefix \ to escape it, for example:
Amosli @ amosli-pc :~ /Learn/sed $Echo "te/st1 te/st2 te/st3 te/st4" | sed "s/test/TEST/g"
Te/st1 te/st2 te/st3 te/st4
In this way, the replacement will become invalid. Use the prefix \ for escape.
amosli@amosli-pc:~/learn/$ |
5. Remove blank rows
amosli@amosli-pc:~/learn/$ test.txt hi,this is command test !
/Pattern/d will remove rows matching the style
^ $ The end of a row is marked with a hyphen (-) to indicate a blank row.
6. & indicates the matched string
amosli@amosli-pc:~/learn/$ this is a test |
The \ w \ + Regular Expression matches each word and corresponds to the previously matched word. Replace the regular expression.
7. substring matching character mouth \ 1
amosli@amosli-pc:~/learn/$ this is digit a number | a number
This command replaces digit 7 with 7, \ (pattern \) is used to match the substring, and the pattern is included in the () escaped using the lifeline. For the first substring to be matched, the corresponding mark is \ 1, the second substring to be matched is \ 2, and so on.
8. Combine multiple expressions
amosli@amosli-pc:~/learn/$ command test !-pc:~/learn/$ test.txt | | !
You can also use:
amosli@amosli-pc:~/learn/$ test.txt | !
Format:
|
9. Reference
Define variables:
amosli@amosli-pc:~/learn/$ txt=hello
Reference variable:
amosli@amosli-pc:~/learn/$ |
10. Optional parameters:
| Command |
Function |
| A \ |
Add one or more rows after the current row. In addition to the last row, the end of each row must be continued "\". |
| C \ |
Replace the text in the current line with the new text after this symbol. In addition to the last row, you must use "\" to continue the row at the end of each line. |
| I \ |
Insert text before the current row. In addition to the last row, you must use "\" to continue the row at the end of each line. |
| D |
Delete row |
| H |
Copy the content in the mode space to the temporary buffer. |
| H |
Append the content in the mode space to the temporary buffer. |
| G |
Copy the content in the temporary buffer to the mode space to overwrite the original content. |
| G |
Append the content of the temporary buffer to the mode space, and append the content to the end of the original content. |
| L |
List non-printable characters |
| P |
Print rows |
| N |
Read the next input line and process it from the next command instead of the first command. |
| Q |
End or exit sed |
| R |
Read input lines from files |
| ! |
Apply commands to all rows other than the selected row |
| S |
Replace another string |
| G |
Global replacement within a row |
| |
|
| W |
Write the selected row to a file |
| X |
Swap content of the temporary buffer and mode space |
| Y |
Replace the character with another character (the y command cannot be used for the regular expression) |
Other options:
--- F specifies the sed script file name
Metacharacter summary:
| Metacharacters |
Function |
Example |
| ^ |
First line Separator |
/^ My/match all rows starting with my |
| $ |
Line tail Locator |
/My $/match all rows ending with my |
| . |
Match a single character except line breaks |
/M .. y/match with the letter m, followed by two arbitrary characters, followed by the letter y line |
| * |
Matches zero or multiple leading characters |
/My */match the row containing the letter m followed by zero or multiple y letters |
| [] |
Match any character in the specified character group |
/[Mm] y/match the rows that contain My or my |
| [^] |
Match any character that is not in the specified character group |
/[^ Mm] y/matches y, but the character before y is not a line of M or m |
| \(..\) |
Save matched characters |
1, 20 s/\ (you \) self/\ 1r/mark the mode between metacharacters and save it as tag 1. Then you can reference it with \ 1. A maximum of nine tags can be defined, starting from the left and the first tag on the left. In this example, 1st to 20th rows are processed, and you are saved as tag 1. If you find youself, replace it with your. |
| & |
Save the search string to reference it in the replacement string |
S/my/** & **/Symbol & represents the search string. My will be replaced with ** my ** |
| \ < |
First-word Locator |
/\ <My/match rows that contain words starting with my |
| \> |
Suffix |
/My \>/match rows that contain words ending with my |
| X \ {m \} |
M x in a row |
/9 \ {5 \}/match rows containing 5 consecutive 9 |
| X \ {m ,\} |
At least m x |
/9 \ {5, \}/match rows containing at least five consecutive 9 |
| X \ {m, n \} |
At least m, but no more than n x |
/9 \ {5, 7 \}/match rows containing 5 to 7 consecutive 9 |