[Sed introduction:]
Sed is a good file processing tool. It is a pipeline command and can be used to add, select, replace, or delete data rows. Sed command line format: sed [-nefri] 'range command' file such as: sed '2d 'aaa.txt # Delete the second line from the aaa.txt file
[Sed workflow:]
When using the vim Screen Editor to edit a file, we need to open the file. There are two problems: 1. Opening a large file will consume a lot of memory. 2. We cannot write a script to call vim to edit the file, but sed can edit the file by writing the script.
Sed is a stream editor. When editing a file, it first reads a row of content from the file into the memory and reads the part of the memory, which is called the mode space. Then it is edited as needed, after editing, the content of the mode space will be output to the screen, and the content in it will be cleared, and then read and take a row to the mode space, so that you can avoid reading the entire file at a time.
[Sed common options:]
-N: only the content in the mode space is displayed, but not the content that has not been edited. -E: directly edit the sed action in the Command column mode.-f: Write the sed action in a file, and-f filename can execute the sed action in filename; -r: sed actions support the syntax of extended formal notation. (The default is the basic regular expression syntax)-I: directly modify the file content to be read, rather than output on the screen.
[Common sed commands:]
D: delete. Because it is delete, d is usually not followed by anything;
S: substitution, which can be directly replaced. Generally, this s action can be combined with regular notation;
A: append an object. a can be followed by a string. These strings will appear in a new row (the next row currently );
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently );
C: replace. c can be followed by strings. These strings can replace a line of content;
[Examples of Common commands:]
# Delete operation d: ---------------------------------------- -----------------------------------
Range is a range, and the delete operation can be expressed in two ways: ① write a number directly to indicate the number of rows to be operated. sed '2d 'aaa.txt must be used to delete the second line in the aaa.txt file. The regular expression must be used to separate the two backslashes. sed '/^ Tom/d' aaa.txt # trim deletes the line starting with Tom in aaa.txt * if no range is specified, full text will be manipulated. sed 'd 'aaa.txt # delete all rows in aaa.txt
1. sed '$ d' aaa.txt # Delete the last row in aaa.txt (operate on a single row)
2. sed '7, 9d' aaa.txt # Delete the seventh row to the ninth row in the aaa.txt file (operate on multiple rows)
3. sed '/aaa/,/bbb/d' aaa.txt # Delete the aaa.txt file that contains all rows between aaa and bbb (operate on multiple rows)
# Replace operation s: ---------------------------------------- -----------------------------------
Format of replacement: sed 'range s/orig/new/sign' file # If no range is specified, the full text will be operated.
1. sed's/aaa/bbb/'ccc.txt # Replace the first keyword aaa in each line in the full text of ccc.txt with bbb, because we didn't specify any Mode
2. sed's/aaa/bbb/G' ccc.txt # Replace all aaa in the full text of ccc.txt with bbb, because we have added sign: g
3. sed's/aaa/bbb/2' ccc.txt # Replace the second aaa in each line in the full text of ccc.txt with bbb, because we have added sign: 2
4. sed '1s/aaa/bbb/G' ccc.txt # Replace all aaa In the first line of ccc.txt with bbb, because range: 1 is added and sign: g is added.
5. The preceding example can be separated by other characters (only with replacement): sed's # aaa # bbb # 'ccc.txt
6. The old position supports regular expressions, but the new position does not support regular expressions, except for the characters \ n \&
Sed '1s/a/N & N/2' ccc.txt # Replace the second a in the full text of ccc.txt with NaN, which is equivalent to referencing
7. If you want to perform multiple operations in a specific range, use {} to enclose multiple operations and use; To separate multiple commands.
Sed '1 {s/a/B/g; s/B/c/} 'ccc.txt # first, replace all a in the first line of ccc.txt with B, then Replace the first keyword B in the first line with c.
8. replace a word. If you want to replace a character, you can use the y command.
Sed '1y/abc/xyz/'ccc.txt # Replace all a in the first line of ccc.txt with x, B with y, and c with z.
# Append operation: ---------------------------------------- --1. sed '/AB/a \ XXX' ccc.txt # insert a line of string xxx under the line containing AB in ccc.txt
2. sed '1a zhang \ njim 'ccc.txt # insert zhang under the first line of ccc.txt and then insert jim in the next line. \ n serves as a line feed.
# Insert operation I: ---------------------------------------- --1. sed '/AB/I \ XXX' ccc.txt # insert a line of string xxx above the line containing AB in ccc.txt
2. sed '$ a abc' ccc.txt # insert abc in the last line of ccc.txt
# Replace operation c: ---------------------------------------- --1. sed '/AB/cAB = AB' ccc.txt # Replace the entire line containing AB in ccc.txt with AB = AB
2. sed '1c AB 'ccc.txt # Replace the first line of ccc.txt with AB
# Display mode space p: ---------------------------------------- --1. sed '$ a abc P' ccc.txt # display all the content in the mode space and file on the screen
Other [sed commands:]
1. = shows the row number
Sed '/^ abc/{=}' ccc.txt # The row number of the keyword starting with abc
2. n is used to get the next row (the current row is deleted from the mode space, and the next row is read to the mode space)
Sed '/^ a/{n; s/B/B/g}' ccc.txt # match the next row starting with a, replace B with B.
3. N is used to read multi-row content to the mode space: (the current row and the next row are both in the mode space)
Sed '/^ A/{s/tb/TB/; N; s/t \ nb/TB \ n/}' ccc.txt # match rows starting with, replace tb with TB. Then, read and delete a row, and replace t of the current row and B of the next row with TB.
4. If the multi-row mode space is used, ^ is not the beginning of the line, but the beginning of the mode space; $ is not the end of the line, but the end of the mode space.
Before replacement: a B c d e after replacement: A B c d e
A B c d e
Sed 'n'; s/^ a/A/'ddd.txt # both the first and second rows will be read into the mode space, but only a of the first row will be replaced with, a In the second row will not be replaced.
Before replacement: a B c d e after replacement: a B c d e
A B c d e a B c d E
Sed 'n'; s/e $/E/'ddd.txt # The first and second rows will be read into the mode space, but only the second row of e will be replaced with E, e In the first line will not be replaced.
5. In addition to the mode space, there is also space to maintain. We can understand it as a cache. The content of the reserved space and the content of the mode space can be exchanged.
H: stores the content in the mode space to the storage space; G: stores the content in the storage space to the mode space;
Before replacement: a after replacement: B
B
Aa bb
Bb aa
Sed '/a/{h; d};/B/{G}' eee.txt # match the row containing a, save them in the reserved space, and delete them from the mode space; match the row containing B and place the reserved space content in the mode space;
6. There is no loop in the set. To achieve the loop effect, you can use the tag