First recognized sed (top)

Source: Internet
Author: User
Tags processing text
During the winter vacation, I flipped down the book SedandAwk101Hacks and looked at other fragmented materials. I probably gave sed and awk a quick start (in fact, sed and awk are also easy to learn ). In the past few days, I have not sorted out the knowledge points in my first class. Long space, I divided... during the winter vacation, I flipped down the book Sed and Awk 101 Hacks, and read other fragmented materials, probably get started with sed and awk (in fact, sed and awk are also easy to learn ). In the past few days, I have not sorted out the knowledge points in my first class. The length is long. I divide it into the upper and lower sections to avoid readers looking tired. Because it is a study note, it may be hard to understand or even make mistakes. In addition, it is difficult to "move" in some places, so I keep it in English in many places.) I hope to correct the mistakes.
 
Symbol conventions:
[] Optional <> required
 
Sed is short for stream editor. it is an online editor that processes a line of text at a time. Generally, the sed execution process is divided into four stages:
 
 
 
When processing text, sed reads (Read) a line of text from the input stream (which can be a file or pipeline) and stores it in pattern space. Sed has two built-in temporary storage zones: pattern space and hold space. Pattern space stores the text read from the input stream. Hold space is a "free" space where temporary data can be stored. (Attention: The data in the two temporary storage zones can be changed by the user! After reading a row to pattern space, sed processes the data in pattern space according to the given command. After processing, sed sends pattern space data to the screen by default, that is, print it! Then sed processes the next row according to the same process and repeats until the input stream ends.
 
The basic syntax format of sed is as follows:
 
Sed [options] [Input-file]
When no input is specified, sed processes the standard input stream by default.
Note: The preceding sed execution process shows that sed does not change the input file unless you use the redirected storage output or related sed commands.
 
Sed common options
-N, -- quiet, -- silent
 
By default, sed prints the content in pattern space at the end of the process. if you add any of the above options, you can cancel this default setting.
 
 
-E script, -- expression = script
 
Specify the sed command, which is generally used when multiple commands exist, as follows:
 
Sed-n-e's/cassvin/Leon/'-e 'P' example.txt
Replace cassvin (if any) in example.txt with Leon and print it out (s/pattern/dest/is the matching command, p is the printing command, which will be introduced later)
 
 
-F script-file
 
In addition to using-e to specify multiple sed commands, you can also write commands to the script file and specify it through-f script-file. As in the preceding example, commands is placed in the script file:
 
#! /Bin/sed-fs/cassvin/Leon/p copy code
Note: start sed to boot the script file with the-f option, so-f cannot be omitted and should be placed at the end of many options. for example, to disable the default print option of sed at the same time, it should be written as follows :#! /Bin/sed-nf
If it is written as follows :#! /Bin/sed-fn, the following error occurs: Can not find file n :***********
 
 
-I [suffix], -- in-place [= suffix]
 
Sed does not rewrite the content of the input file unless the above options are used. Suffix is the backup suffix. Example:
 
Sed-n-ibak's/cassvin/Leon/'example.txt
SED will be rewritten to example.txt, and cassvin in example.txt will be rewritten to Leon. Just put it easy, sedhas done a good job for us, and the secret file name is example.txt bak.
 
 
-C, -- copy
 
This option must be used with-I. After-c-I [suffix] is used, the input file will not be rewritten. sed will create a temporary file (the file name is the input file name + suffix name) to save the modified content. For example:
 
Sed-ibak-c's/cassvin/Leon/'example.txt
Example.txtwill not be rewritten, and Sedwill create example.txt bak to save the modified content.
 
 
Sed common commands
After several common sed options are introduced, we will introduce the sed command with the sed option below. Create a sample for this example:
 
101, John Doe, CEO102, Jason Smith, IT Manager103, AJ Reddy, Sysadmin104, Anand Ram, 0000105, Jane Miller, Sales Manager copy code
As mentioned above, there can be multiple commands in sed, which can be specified by-e or written to the script file. In fact, when there are multiple sed commands, they can also be separated. For example:
 
Sed-n's/John/Jason/; p 'employee.txt
Replace John in employee.txt with Jason and print it out.
In addition, you can also use the {} command to include sed, with the same effect:
 
Sed-n '{s/John/Jason/; p}' employee.txt
 
P command
 
Print and clear the content in the current pattern space, which is often used with the-n option. If no rows are printed, p prints all rows in the input stream by default.
 
 
In sed, we can directly give numbers to specify rows (or ranges ). Or use the pattern to match the specified row. Whether it's a number, a pattern, or something else, the corresponding row or row range can be specified in sed, which is called Address.
(Forgive me for stopping the sed command. Address is really important .)
As mentioned above, Address has multiple forms, numbers, pattern matching, and so on. The following is an example.
Specify by number:
 
Sed-n 'n' P' employee.txt print the n rows sed-n 'n', m P' employee.txt print n to m rows (including the n rows) sed-'N, $ p 'employee.txt prints the nth row to the end of the row, $ indicates the end of the row sed-n 'n', + m P' employee.txt prints n to m rows, + m indicates printing m + n rows of sed-n 'n' ~ based on n ~ M p 'employee.txt 1 ~ 2 indicates 1, 3, 5, etc, 2 ~ 2 indicates copying code 2, 4, 6, etc.
And so on.
Specify through pattern matching:
 
Sed-n'/John/p' employee.txt print "101, John Doe, CEO" a row of sed-n'/aj/,/Jane/p' employee.txt. the output result is as follows: 103, Raj Reddy, Sysadmin104, Anand Ram, 0000105, Jane Miller, Sales Managersed-n'/Anand Ram/, $ p 'employee.txt. the output result is as follows: 104, Anand Ram, using 105, Jane Miller, and Sales Manager
Use pattern matching and numbers together:
 
Sed-n'/John/, 2 P' employee.txt 101, John Doe, CEO102, Jason Smith, IT Managersed-n'/John/, + 2 P' employee.txt 101, john Doe, CEO102, Jason Smith, IT Manager103, AJ Reddy, Sysadmin copy code
Note that no sed-n'/John /~ 2 p 'employee.txt.
 
 
D command
 
Delete the content in the current pattern space, which can be used with Address. If no Address is specified, sed matches all rows. That is, the d command will delete all rows (of course, it does not affect the input file)
 
Sed '2 d' employee.txt delete the second row of sed '2, $ d 'employee.txt delete from the second row until the end of the line etc... other classes are the same, you can refer to the previous description about the Address copy code
 
W command
 
Syntax format: sed 'W output-file' input-file
Write the content of the current pattern space to the specified file. Note: If-n is not used, sed will print the content of pattern space to the screen.
Sed-n 'w output.txt' employee.txt is equivalent to copying employee.txt to output.txt
Similarly, w can be used in combination with Address.
W commands may not be used in many cases. In most cases, people are used to using redirection to save the modified content to a file. the following two commands are equivalent:
 
Sed-n'2, 4 w output.txt 'employee.txt sed-n' 2, 4 p 'employee.txt> output.txt copy the code
 
R command
 
Read and print the file at the specified position.
 
Sed '& r tmp.txt' employee.txt
The content of tmp.txt will be added after employee.txt
 
 
A command
 
Syntax format: sed 'a line-content' [input-file]
Attaches a row to a specified position. Note that it is append. when sed reads the specified position, it will append the new line to be inserted to the back of the current pattern space. Example:
 
Sed-n-e '2 a 202, Leon Hui, Student '-e '2 P' employee.txt 102, Jason Smith, IT Manager202, Leon Hui, Student Copy code
Other addresses are similar
 
 
I command
 
Syntax format: sed 'I line-content' [input-file]
Similar to a command. However, a is append, and I is insert, that is, insert before the specified position.
 
Sed-n-e '2 I 201, Leon Hui, Student '-e '2 P' employee.txt 201, Leon Hui, Student102, Jason Smith, IT Manager copy code
Other addresses are similar
 
 
C command
 
Syntax format: sed 'c line-content' [input-file]
C is change. Changes the row at the specified position.
 
Sed '2 c 202, Leon Hui, Student 'employee.txt 101, John Doe, CEO202, Leon Hui, Student ...... Copy code
Other addresses are similar
 
A, I, and c can also be used in combination. for details, refer to the instance:
 
Sed '/Jason/{a 204, Jack Johnson, Engineer \ I 202, Mark Smith, Sales Engineer \ c 203, Joe Mason, Sysadmin \}' employee.txt 101, John Doe, CEO202, Mark Smith, Sales Engineer203, Joe Mason, Sysadmin204, Jack Johnson, Engineer103, Raj Reddy, Sysadmin copy code
 
I command
 
Print hidden characters such as \ t and \ n. For example, name school and I are printed as name \ tschool
 
 
= Command
Print the row number. See the example:
 
Sed-n '1, 3 {=; p} 'employee.txt 1101, John Doe, CEO2102, Jason Smith, IT Manager3103, AJ Reddy, Sysadmin copy code
Other addresses are similar
The readers may have some questions about the above Command. why is it "1, 3 {=; p}"? can it be "1, 3 =; p? This is not acceptable. The latter will print the fourth and fifth line. In "1, 3 {=; p}", when sed reads the fourth row, the =; p command line in {} will not execute because 4 is not within the range of 1 and 3, that is, the fourth row will not be added or printed. But the command "1, 3 =; p" is different. 4 The = command is not executed if the value range is 1 or 3. However, since p is not bound by the condition 1 and 3 like the previous command, it prints all rows. Therefore, lines 4 and 5 are printed. In short, the second command is equivalent to: 1, 3 {=}; p
 
 
Y command
 
Character conversion. Syntax format: sed 'Y/orgin_chars/dest_chars/'input-file
Orgin_chars and dest_chars must be the same length. Otherwise, an error is returned.
Sed 'Y/a/B ': replace a in employee.txt with B
Sed 'Y/abcde/abcde' employee.txt case-sensitive conversion
 
 
Q command
 
Q command is quit. When the q command is used, sed prints the content in the current pattern space and then terminates the program. See the example:
 
Sed '2 Q' employee.txt 101, John Doe, CEO102, Jason Smith, IT Manager copy code
Similarly, q can be used with Address.
 
 
 
(Coming soon)
 
Author cassvin
Related Article

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.