Text processing stream editor sed command usage

sed is a stream editor, it is in the very text processing tool that perfectly fit a regular expression to use, extraordinary. Handling, storing the row currently being processed in a temporary buffer, called a "model space" (pattern space), followed by treatment with the contents of the buffer sed command, the processing is completed, the contents of the buffer sent to the screen. Then the next line, which is repeated until the end of the file. File contents not changed, unless you use redirection to store the output. Sed is mainly used to automatically edit one or more documents; simplify repeated operation of the document; write conversion procedures.

1. sed options, commands, replacement tags

  1.1 sed command format
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)

1.2 sed options

  1. -e option to specify a script to handle text input file;
  2. -f option to specify a script file to handle text input file;
  3. -i directly modify the contents of the file read;
  4. -n quiet mode, displays only the processing result;

1.3 sed command

  1. A \ inserted below the current line of text.
  2. I \ inserted in the current line of text above.
  3. c \ The selected line to the new text.
  4. d delete, delete the selected row.
  5. The first line D delete the template block.
  6. s replace the specified character
  7. SUMMARY h copy template block into a buffer memory.
  8. SUMMARY additional die plate into H buffer memory.
  9. g obtaining the content of the memory buffer, and to replace the current die plate in the text.
  10. G to obtain the contents of memory buffer and appended to the current text of the mold plates.
  11. l list can not print a list of characters.
  12. N reading a next input row, the row with the new process the next command instead of the first command.
  13. N append a next input line to the back plate and the mold insert a new line between the two, to change the current line number.
  14. P line print template block.
  15. The first row P (upper case) mode printing plate.
  16. q to quit Sed.
  17. b lable branches to the script with a place mark, if a branch to the branch does not exist, the end of the script.
  18. r file read from the file line.
  19. t label if branch, starting from the last row, once the conditions met or T, t command will cause a branch to a label with a command, or the end of the script.
  20. Error branch label T from the last line, once an error occurs or T, t command, the command will cause a branch to the label with, or to the end of the script.
  21. w file write and append template block to the end of the file.
  22. W file additional written and the first line of the template block to the end of file.
  23. ! Shows the effect occurs for all subsequent commands are not selected rows.
  24. = Print the current line number.
  25. # Comment extended to the next newline before.

1.4 sed substitution tags

  1. g expressed its full replacement within the line.
  2. p represents the print line.
  3. w represents the writing of a document row.
  4. x represents an interchangeable mold blocks of text and the text buffer.
  5. y represents the translation of a character to another character (but not for regular expressions)
  6. \ 1 substring matching tag
  7. & Matched string tag

1.5 sed yuan Character Set

  1. ^ Matches the start of the line, such as: / ^ sed / matches all lines beginning with sed.
  2. $ Matches the end of the line, such as: / sed $ / matches all ending sed line.
  3. Matching a newline with any character, such as: / sd / s followed by a match any character, and finally D.
  4. * Matches zero or more characters, such as: / * sed / matching lines after all template is followed by one or more spaces of sed.
  5. Characters in [] matches a specified range, such as / [Ss] ed / matching and sed Sed.
  6. [^] Matches a character not in the specified range, such as: / [^ A-RT-Z] ed / matching does not contain a beginning and AR TZ letters, the row immediately ed.
  7. \ (.. \) matching substring, holds the matching character, such as s / \ (love \) able / \ 1rs, loveable is replaced lovers.
  8. Save Search & character to replace other characters, such as s / love / ** & ** /, love this into ** love **.
  9. \ <Matches the beginning of the word, such as: / \ <love / match contains words that begin with love line.
  10. \> Matches the end of a word, such as / love \> / include matching with the word love at the end of the line.
  11. x \ {m \} repeated characters x, m times, such as: / 0 \ {5 \} / matching comprises five rows 0.
  12. X \ {m, \} is repeated characters x, at least m times, such as: / 0 \ {5 \} / matching at least five rows 0.
  13. X \ {m, n \} repeated characters x, at least m, more than n times, such as: / 0 \ {5,10 \} / match lines 5 to 10 0.

2. sed usage examples

 

2.1 replace operation: s command

Replacement string text:
# sed 's/book/books/' file
-n option and p commands used with the express print only those lines replace happened:
# sed -n 's/test/TEST/p' file
Edit files directly option -i first book, will match each line of the file file replace books:
# sed -i 's/book/books/g' file

2.2 g fully replace the mark

Suffix / g marker replaces all matches in each row:
# sed 's/book/books/g' file
When starting from the first replacement needs to match N, may be used / Ng:
# echo sksksksksksk | sed 's/sk/SK/2g'
skSKSKSKSKSK

# echo sksksksksksk | sed 's/sk/SK/3g'
skskSKSKSKSK

# echo sksksksksksk | sed 's/sk/SK/4g'
skskskSKSKSK

2.3 delimiter

The above command character / sed used as a delimiter, the delimiter may be used any of:
# sed 's:test:TEXT:g'
# sed 's|test|TEXT|g'
When delimiter appears in the internal style, you need to be escaped:
# sed 's/\/bin/\/usr\/local\/bin/g'

2.4 deletion: d command

Delete blank lines:
# sed '/^$/d' file
Second line delete file:
# sed '2d' file
Line 2 delete all the files to the end of the line:
# sed '2,$d' file
Delete the last line of the file:
# sed '$d' file
Delete all files in the beginning of the line is the test:
# sed '/^test/'d file

2.5 & matched string tag

Regular expression \ w \ + match each word, using [&] Alternatively it, & previously corresponding to the matched word:
# echo this is a test line | sed 's/\w\+/[&]/g'
[this] [is] [a] [test] [line]
All 192.168.0.1 is replaced with the beginning of the line will add to its own localhost:
# sed 's/^192.168.0.1/&localhost/' file
192.168.0.1localhost

2.6 substring matching flag \ 1

Match a given pattern part:
# echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number
Command digit 7, is replaced 7. Pattern matching substring to 7, \ (.. \) for matching substring, for matching to the first substring is marked as  \ 1 , and so on to the second match result is \ 2 , E.g:
# echo aaa BBB | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
BBB aaa
love is marked as 1, all loveable Lovers will be replaced, and printed out:
# sed -n 's/\(love\)able/\1rs/p' file

2.7 combine multiple expressions

# sed '表达式' | sed '表达式'
等价于:
# sed '表达式; 表达式'

2.8 references

sed expressions can be used to refer to single quotes, but if variable string expression contains an internal, you need to use double quotes.
test=hello
echo hello WORLD | sed "s/$test/HELLO"
HELLO WORLD

2.9 selected range of rows:, (comma)

All rows in the template test and check the print range is determined by:
# sed -n '/test/,/check/p' file
Print start from line 5 to all the lines between the lines in a first test begins comprising:
# sed -n '5,/^test/p' file
For the test line between the template and west, with each end of the line replacement string aaa bbb:
# sed '/test/,/west/s/$/aaa bbb/' file

More than 2.10 point editing: e command

-e option allows to perform multiple commands on the same line:
# sed -e '1,5d' -e 's/test/check/' file
Sed above command to delete the first expression of the 1-5 line, with a second test command substitution check. Command execution order of an impact on the results. If both commands are replaced with command, then the first Replace command will affect the results of the second replace command. And -e command is equivalent --expression:
# sed --expression='s/test/check/' --expression='/love/d' file

2.11 from the file reads: r command

the contents of the file is read in, displayed on the back of the line and test matches, if the match multi-line, the file's contents will be displayed below all matching rows:
# sed '/test/r file' filename

2.12 written to the file: w command

In the example, all lines containing test is written in the file:
# sed -n '/test/w file' example

2.13 append (lower line): a \ command

This is a test line will be appended to the beginning of the line to test:
# sed '/^test/a\this is a test line' file
After the line 2 test.conf file into this is a test line:
# sed -i '2a\this is a test line' test.conf

Insert 2.14 (row): i \ command

This is a test line will be appended to the front of the line to the beginning of the test:
# sed '/^test/i\this is a test line' file
Before row 5 test.conf file into this is a test line:
# sed -i '5i\this is a test line' test.conf

Next 2.15: n Command

If the test is a match, the match line to the next line, replacing this line aa, becomes bb, and print the row, then continue:
# sed '/test/{ n; s/aa/bb/; }' file

2.16 Deformation: y command

Abcde lines all within 1 to 10 converted to uppercase, note that regular expression metacharacters can not use this command:
# sed '1,10y/abcde/ABCDE/' file

2.17 Exit: q command

After printing line 10, exit sed
# sed '10q' file

2.18 holding and obtain: h commands and command G

Sed when working with files, each row is stored in a temporary buffer called the pattern space unless the row is deleted or output is canceled, otherwise all the rows are processed will be printed on the screen. Then the pattern space is emptied and stored in a new line waiting to be processed.
# sed -e '/test/h' -e '$G' file
In this example, the line after the test match is found, will be deposited into the pattern space, h command to copy it and deposited into a special buffer called buffer zone maintained. The second statement means that, after reaching the last row, G instruction fetch buffer holding line, and then put it back in the pattern space, and has now been added to the end of the pattern space exists in the row. In this example, it is added to the last row. In short, any line contains test is copied and appended to the end of the file.

2.19 maintain and exchange: h x command and the command

Swap model space and maintain the contents of the buffer. That is the line that contains test and check exchange:
# sed -e '/test/h' -e '/check/x' file

2.20 script scriptfile

sed sed script is a list of commands, start Sed -f option to boot script file name. Sed scripts for command input is very picky, you can not have any gaps or text at the end of the command, if there are multiple commands in a row, separated by semicolons. # Behavior to the beginning of the comment line, and can not span.
# sed [options] -f scriptfile file(s)

2.21 print odd-numbered lines or even-numbered lines

method 1:
# sed -n 'p;n' test.txt  #奇数行
# sed -n 'n;p' test.txt  #偶数行
Method 2:
# sed -n '1~2p' test.txt  #奇数行
# sed -n '2~2p' test.txt  #偶数行

2.22 print the next line of the character string matching

# grep -A 1 SCC URFILE
# sed -n '/SCC/{n;p}' URFILE
# awk '/SCC/{getline; print}' URFILE
Transfer from: http: //www.1987.name/306.html

Reproduced in: https: //my.oschina.net/766/blog/210876

Guess you like

Origin blog.csdn.net/weixin_34352005/article/details/91547681