Text Processing Flow editor sed command usage

Source: Internet
Author: User
Tags string tags

Sed is a flow editor, which is a very useful tool in text processing, and it works perfectly with regular expressions. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output. SED is used to automatically edit one or more files, to simplify the repetitive operation of files, to write conversion programs, and so on.
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 option

-e handles the input text file with the script specified in the option;
-F processes the input text file with the script file specified in the option;
-I directly modify the contents of the file read;
-N Quiet mode, showing only the results after processing;

1.3 sed command

A inserts text below the current line.
I inserts text above the current line.
C To change the selected line to the new text.
D deletes, deletes the selected row.
D deletes the first line of the template block.
s replaces the specified character
H copies the contents of the template block into an in-memory buffer.
H Append the contents of the template block to the buffer in memory.
G Gets the contents of the memory buffer and replaces the text in the current template block.
G gets the contents of the memory buffer and appends it to the back of the current template block text.
The L list cannot print a list of characters.
N reads the next input line and processes the new row with the next command instead of the first command.
N appends the next input line to the template block and embeds a new line between the two, changing the current line number.
P Prints the line of the template block.
P (uppercase) prints the first line of the template block.
Q Exit sed.
b lable branches to the mark in the script and branches to the end of the script if the branch does not exist.
R file reads rows from file.
The T label if branch, starting at the last line, once the condition is met or the t,t command, will cause branching to the labeled command, or to the end of the script.
The T label Error branch, starting at the last line, will cause an error or T,T command to branch to the labeled command, or to the end of the script.
W file writes and appends the template block to the end of file.
W file writes and appends the first line of the template block to the end of the file.
! Indicates that the following command has effect on all rows that are not selected.
= Print the current line number.
# Extend the annotation before the next line break.

1.4 SED Replacement Tag

G means full replacement in line.
P represents the print line.
W indicates that the line is written to a file.
X represents the interchange of text in the template block and the text in the buffer.
Y means translating a character into another character (but not for regular expressions)
1 substring matching tag
& Matched String token

1.5 sed meta character set

^ matches the start of a line, such as:/^sed/matches all lines beginning with sed.
$ matches the end of the line, such as:/sed$/matches all rows ending with sed.
. Matches any character that is not a newline, such as:/s.d/matches S followed by an arbitrary character, and finally D.
* Match 0 or more characters, such as:/*sed/match all the templates are one or more spaces followed by the SED line.
[] matches a specified range of characters, such as/[ss]ed/to match sed and sed.
[^] matches a character that is not in the specified range, such as:/[^a-rt-z]ed/matches the beginning of a letter that does not contain a-r and t-z, followed by the line of Ed.
(..) Matches a substring, saving matching characters, such as s/(Love) able/1rs,loveable replaced by lovers.
& Save Search characters are used to replace other characters, such as S/love/**&**/,love this into **love**.
< matches the beginning of a word, such as:/<love/matches a line that contains a word that begins with love.
> matches the end of a word, such as/love>/matches a line that contains a word ending with love.
X{m} repeats characters x,m times, such as:/0{5}/matches rows containing 5 0.
X{m,} Repeat character x, at least m times, such as:/0{5,}/matches at least 5 0 rows.
X{m,n} repeats the character x, at least m times, not more than n times, such as:/0{5,10}/matches a row of 5~10 0.

2. SED usage Example


2.1 Replacement operation: s command

To replace a string in text:


# sed ' s/book/books/' file

The-n option is used with the P command to print only those lines that have been replaced:


# sed-n ' s/test/test/p ' file

The direct Edit File option-I will match the first book in each line in the file to be replaced with books:


# sed-i ' s/book/books/g ' file
2.2 Comprehensive replacement Mark G

Using the suffix/g tag replaces all matches in each row:


# sed ' s/book/books/g ' file

You can use/ng when you want to start a replacement from the nth place:


# 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 Delimiters

The characters in the above command are used as delimiters in sed or any delimiter can be used:


# sed ' s:test:text:g '
# sed ' s|test| Text|g '

When delimiters appear inside the style, they need to be escaped:


# sed ' s//bin//usr/local/bin/g '
2.4 Delete operation: D command

To delete a blank line:


# sed '/^$/d ' file

Delete the 2nd line of the file:


# sed ' 2d ' file

Delete the 2nd line of the file to the end of all lines:


# sed ' 2, $d ' file

Delete file last line:


# sed ' $d ' file

Delete all rows in the file that start with test:


# sed '/^test/' d file
2.5 Matched String Tags &

The regular expression w+ matches every word, using [and] replacing it,& corresponding to the words that were previously matched:


# echo this be a Test line | Sed ' s/w+/[&]/g '
[This] [IS] [A] [test] [line]

All rows that begin with 192.168.0.1 are replaced with their own localhost:


# sed ' s/^192.168.0.1/&localhost/' file
192.168.0.1localhost
2.6 Substring Matching marker 1

Matches part of a given style:


# echo this are digit 7 in a number | Sed ' s/digit ([0-9])/1/'
This is 7 in a number

Digit 7 in the command, replaced by 7. The string that the style matches to is 7, the (..) is used to match the substring, the first substring to match is labeled 1, and the second result to match is 2, for example:


# echo Triple-A BBB | Sed ' s/([a-z]+) ([a-z]+)/2 1/'
BBB AAA

Love is labeled 1, and all loveable will be replaced with lovers and printed out:


# sed-n ' s/(Love) able/1rs/p ' file
2.7 Combination of multiple expressions


# sed ' expression ' | Sed ' expression '
Equivalent to:
# sed ' expression; Expression '
2.8 References

sed expressions can be referenced using single quotes, but you need to use double quotes if the expression contains variable strings inside.


Test=hello
echo Hello World | Sed "s/$test/hello"
HELLO World
2.9 Range of selected rows:, (comma)

All rows that are within the scope determined by the template test and check are printed:


# sed-n '/test/,/check/p ' file

Print all rows from line 5th to the first one that contains the start of test:


# sed-n ' 5,/^test/p ' file

For the line between the template test and West, the end of each line is replaced with the string AAA BBB:


# sed '/test/,/west/s/$/aaa bbb/' file
2.10多 Point edit: E command

The-e option allows multiple commands to be executed on the same line:


# sed-e ' 1,5d '-e ' s/test/check/' file

The first command of the SED expression above deletes 1 to 5 rows, and the second command replaces test with check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.

The-e equivalence command is –expression:


# sed--expression= ' s/test/check/'--expression= '/love/d ' file
2.11 read from File: R command

The contents of file are read in, displayed after the row that matches the test, and if multiple rows are matched, the contents of file are displayed under all matching rows:


# sed '/test/r file ' filename
2.12 Writing to file: w command

All rows containing test in example are written to file:


# sed-n '/test/w file ' example
2.13 Append (line below): a command

Append this be a test line to the following row beginning with test:


# sed '/^test/athis is a test line ' file

After line 2nd of the test.conf file, insert this is a test lines:


# sed-i ' 2athis is a test line ' test.conf
2.14 Insert (on line): I command

Append this be a test line to the front of the row starting with test:


# sed '/^test/ithis is a test line ' file

Before line 5th of the test.conf file, insert this is a test lines:


# sed-i ' 5ithis is a test line ' test.conf
2.15 Next: N command

If test is matched, move to the next line of the matching row, replace the AA of the line, change to BB, print the line, and then continue:


# sed '/test/{n; s/aa/bb/} ' file
2.16 Variants: Y command

Turn all ABCDE in the 1~10 line to uppercase, note that the regular expression metacharacters cannot use this command:


# sed ' 1,10y/abcde/abcde/' file
2.17 Exit: Q command

Exit sed after printing line 10th


# sed ' 10q ' file
2.18 keeping and acquiring: H-command and G-command

When SED processes files, each row is saved in a temporary buffer called the pattern space, and all rows processed are printed on the screen unless the row is deleted or the output is canceled. The mode space is then emptied and stored in a new line waiting to be processed.


# sed-e '/test/h '-e ' $G ' file

In this example, when the row that matches the test is found, it is stored in the mode space, and the H command copies it and stores it in a special buffer called the retention buffer. The second statement means that when the last row is reached, the G command takes out the line that holds the buffer and then puts it back in the pattern space and appends it to the end of the line that already exists in the pattern space. In this case, append to the last line. In simple terms, any row containing test is copied and appended to the end of the file.
2.19 Keeping and interchange: H command and x command

Swap the mode space and keep the contents of the buffer. Which is to swap the line containing test and check:


# sed-e '/test/h '-e '/check/x ' file
2.20 Script ScriptFile

The SED script is a list of SED commands that boot the script file name with the-F option when SED starts. Sed is very picky about the commands entered in the script and cannot have any white space or text at the end of the command, separated by semicolons if there are multiple commands on one line. Actions that begin with # comment on rows and cannot span rows.


# sed [options]-F scriptfile file (s)
2.21 printing odd or even rows

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 matching strings


# grep-a 1 SCC urfile
# sed-n '/scc/{n;p} ' urfile
# awk '/scc/{getline print} ' Urfile

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.