Recently, I have seen a few are not very common, but more useful SED examples, all related to Pattern Space, so I think that I think the correct explanation is made, and I will share it with everyone and please refer to the discussion example. A sed g outputs an empty line below the file
Code: $ cat foo 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555 $ sed G foo 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555
Explanation: Usage The G function sed G in appends the contents of the holding area to the contents of the pattern space The former and new contents are separated by a newline The maximum number of addresses is two hold space:.. Retaining space ( Or reserved space, buffer), initially empty Pattern Space: Mode space In the above example, the empty Hold Space is attached to each line of the file, so the result is that there is more empty lines after each line. : SED '/ ^ $ / d; g' Each of the files of the file outputs a space line SED '/ ^ $ / d; g; g' In each non-air line below the file, two empty lines
Code: $ cat foo 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555 $ sed '/ ^ $ / d; G' foo 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555
Note: Sometimes there will be some blank line consisting of space characters or tabs. The front regular ^ $ can't match such a line, then Sed '/ [[: space:]] / d; g' example two Sed '/ regex / {x; p; x;}' inserted an empty line in front of all rows of regex
Code: $ cat foo 11111111111111 22222222222222 test33333333333333 44444444444444 55555555555555 $ sed '/ test / {x; p; x;}' foo 11111111111111 22222222222222 test33333333333333 44444444444444 55555555555555
Explanation: The use of X in SEDs The Exchange Function Interchanges The Contents of the Pattern Space and The Holding Area. That Maximum Number of Addresses Is Two. That is, the STTERN SPACE STTERN SPAT SED The role of the Pattern SPACE Mode space is copied to standard output. Analyze the content command of this command to keep the space and mode space to keep the space mode space x before: NULL execution: TEST / N execution: TEST / N execution: NULL P Performance: NULL execution: TEST / n Pre-execution: test / n execution: null Output A blank X execution: TEST / N is executed: NULL execution: NULL execution: test / n (Note: Test Test is short) : You can test Sed '/ test / {x; p;}' foo or sed '/ test / {p; x;}' foo, etc., see the results, experience the changes in two spaces: SED '/ Regex / G 'is the output of a space SED' / regex / {x; p; x; g;} 'is the front and the following output of all the rows of regex and below all the rows of REGEX. n; G; 'even-numbered row below the file into a blank line: $ cat foo 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555 $ sed' n; G; 'foo 11111111111111 22222222222222 33333333333333 44444444444444 55555555555555
Explanation: Use of N in the SED: Copy mode space to standard output. Replace the mode space with the next line of the input. After executing n, then output the first row to the standard output, and then the second line enters the mode space. According to the explanation of G, it will insert an empty line behind the second line, then output; then execute n to execute the third line Output to the standard output, then the fourth line enters the mode space, and inserts the blank line, and pushes the corresponding: sed 'n; n; g' Indicates the third, 6, 9, 12, ... Insert an empty Sed 'N; N; N; g' Indicates the 4th, 8th, 12th, 16ths of the file, insert an idler Sed 'N; D' indicates an even line example of the delete file. Sed '$! n; $! d' output file last 2 line, equivalent to tail -2 foo
Code: $ cat foo 111111111111111222222222222 3333333333333 44444444444444 55555555555555 $ SED '$! N; $! D' foo 44444444444445555555555555
Explanation: D Delete the data before the first newline letter / N in the mode space. N Add the next line of the input to the mode space. Sed '$! n; $! d': For the previous line of the second line of the file, N is placed in the next line of the current line to the mode space, and D will delete the content of the mode space; to the countdown At the time of the second line, the last line is attached to the second line below, and then the last line does not perform D, so the last two lines of the file are saved. Another usage of n
Code: $ sed = foo | sed N 1 11111111111111 2 22222222222222 3 33333333333333 4 44444444444444 5 55555555555555 $ sed = foo | sed 'N; s // n / /' 1 11111111111111 2 22222222222222 3 33333333333333 4 44444444444444 5 55555555555555 explained: the role of N In order to format the line number, it can be used to format the output file example five sed '1! G; h; $! D' sed -n '1! G; h; $ p' to display the line of the file, equivalent to TAC Command (some platforms don't have this command)
Code: $ cat foo 11111111111111 22222222222222 33333333333333 $ sed '! 1 G; h; $ d!' Foo 33333333333333 22222222222222 11111111111111 $ sed -n '1 G; h; $ p!' Foo 33333333333333 22222222222222 11111111111111
Explanation: SED in H Usage: h The h (Hold) Function Copies The Contents of the Pattern Space Into A Holding Area, Destroying Any Previous Contents of the Holding Area. Meaning Save the contents of the mode space to the SED in the SED D represents the delete mode space. 1! G indicates that the remaining rows perform a g command except for the first line; $! D indicates that the remaining rows perform D commands except for the last line. Look at the Sed '1! G; h; $! D' command to keep the spatial and mode space during execution of the command: The command holds the space mode space first row H; D execution: NULL execution: 1111 / n Pre-execution: 1111 / N is executed: NULL second line g; H; D execution: 1111 After execution: 2222 / n1111 / n Performance: 2222 / n After: NULL second line g; h Execution: 2222/1111 / N-executed: 3333 / N2222 / N / 1111 / N Pre-execution: 3333 / N after execution: 3333 / N2222 / N / 1111 / N (Note: That is abbreviated), this is the descending order of the file. Exterior: The command to display in the reverse order of a file in VI is: g /./ M0, means that according to the normal order of the file, put the line to the top of the file, this is just right The reverse order of the file is displayed. This is just a little understanding of model space and holding space. Personally feel that there are many things in this area to learn, please take bricks.