About the SED for text processing tools

Source: Internet
Author: User
Tags line editor

Tools for working with the text sed

Line Editor:
Sed is a stream editor that processes a single line of content at a time, and when processed, stores the currently processed rows in a temporary buffer, which we call "pattern space"
The SED command is then used to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen, followed by the following line, which repeats until the end of the file.
The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program, etc.


There is also a pattern called holding space: SED works by reading one line of text to ' Mode space ', processing is done in the pattern space, the processing result is output to the standard output device, and the next row is processed after a row in the pattern space, so there may be other processing for the processed rows, so the processed rows can be processed first ' Import ' To keep space, if need to follow-up processing, in the secondary ' import ' mode space processing, concept: Mode space is like the kitchen, to keep space as a refrigerator, kitchen for processing production, refrigerator responsible for the preservation of some semi-finished materials.

Keep the space to support some advanced commands to use in the hold space: specifically, the bottom: Advanced editing command options

SED usage
sed [options] can be followed by multiple options ' script ' [source file] can be followed by multiple text
Script: Address bound edit command, indicating what to do with the text
Common options:
Sed-n Automatic printing of content in non-output mode space
For example ~]# sed-n ' 2p '/etc/passwd added the-N option to only display the content defined in the content P display mode space

SED-E scropt1-e scropt2-e Scropt2 can be edited in multiple points
Sed-f/path/to/script_file reads the script from the specified file and runs
Sed-r supports the use of extended regular expressions

Sed-i Modifying the source file directly
For example, the option I sed-i.bak ' 10d ' F1 to the source file operation when the backup file

Address delimitation: extracted area
Processing the full text without giving an address
To single address:
# represents a number, the specified row
For example ~]# sed-n ' 1p ' F1 only shows the 1th line in the text

$ last line
For example ~]# sed-n ' $p ' F1 show only the last line in the text

/mode/each row to which the pattern can be matched;
For example, ~]# sed-n '/root/p '/etc/passwd matches only search content with keywords

Address range:
#1, #2 from line # # to # #
For example, ~]# sed-n ' 1,4p '/etc/passwd only displays the contents of lines 1th through 4th of the text

#,+n start with # lines, up to n rows down
For example, ~]# sed-n ' 4,8p ' F1 only displays the contents of line 4th in the text to the 8th line of the text

#,/mode/Starting from # line, to the end of the first line to which the pattern is matched, all the contents of the line in the middle
For example ~]# sed-n ' 2,/root/p '/etc/passwd

/mode 1/,/mode 2/starts with the line that matches the first chant pattern 1,

To the end of the first line that is matched to the pattern 2, the contents of all the rows in the middle

~ step, specify the starting line, and the step size. Count lines 2-2 even rows
For example ~]# sed-n ' 1~2p ' file1 to show 1 odd lines, 1,3,5 .....
For example ~]# sed-n ' 2~2p ' file1 only shows 2 even lines, 2,4,6 ...

Edit command:
D: Delete the line matching the pattern space
For example ~]# sed ' 4d ' f1 delete line 4th matches, not delete source file contents, output delete

P: Display the contents of the mode space
For example ~]# sed-n ' 4p ' F1 mate-n option does not output text content for automatic printing, p only shows what the 4th line matches

A \text: Append text after line, support multi-line append using \ n newline character
For example ~]# sed '/^1/a\xiaomage ' file1 I start with the number 1 line, append the content to 1 after the line

I \text: Insert text in front of line, support multi-line insertion with \ n newline character
For example ~]# sed '/^1/i\xiaomage ' file1 Likewise, use I at the beginning of the line with the number 1, append the contents to the line before 1

C \text: Replacing a line of behavior or multiple lines of text
For example, ~]# sed '/^1/c\xiaomage ' file1 the line with the character 1, replacing it with the xiaomage content

W/path/to/somefile save pattern matching rows to the specified file
For example ~]# sed '/^1/w/root/f1 ' file1 the character starts with a 1 save under the specified path and is named

R/path/from/somefile reads the text from the specified file to the line in the pattern space

= Print line numbers for lines in pattern space
! Matching row-fetching anti-processing in pattern space
s/Regular expression/replacement position/
s///find replacement, support using other separators [email protected]@@,s####
Replace tag
G in-line global substitution
P shows the row that replaced the successful
W W/path/to/somefile Save the successful row to the specified file

Advanced Editing Commands:
H overlay the contents of the pattern space into the hold space
H Append the contents of the pattern space to the hold space
G Remove data from the hold space to cover the pattern space
G append content from hold space to pattern space
X swap the content in the pattern space with the content in the hold space
N reads the next line of matching rows to the pattern space
N append the next line of the matched row to the pattern space
D Delete rows in the pattern space
D Delete the beginning of a multiline pattern space
Note: Command function can be used! Reverse, divide well can be used to separate scripts


Exercise 1
1 Remove all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace
~]# sed-r ' [email protected]^[[:space:]][email protected]@ '/etc/grub2.cfg
(Command options,-R use extended expressions, find replacements, delimiters, lines that begin with white space + matches at least once, followed by paths)

2 Remove all # and white-space characters from the beginning of the line in the/etc/fstab file that begin with #, followed by at least one white-space character
~]# sed ' [email protected]^#[[:space:]]\[email protected]@ '/etc/fstab
(command option,-R use extended expression, find replacement delimiter, ^ start with # whitespace matches the preceding character at least once)

3 Add # At the beginning of each line of/root/install.log
~] # sed-r ' [email protected] (. *) @#\[email protected] '/root/install.log
(Command options,-R use extended expressions, s to find substitution separators to group.) * Any character \1#g the matched character within a group)
~] # sed ' [email protected]^@#@ '/root/install.log
(Command options, S-Find replace with ^ start with # characters, followed by path)

4 Add # to the beginning of lines in the/etc/fstab file that do not begin with #
~] # sed ' [email protected]^[^#]@#&@ '/etc/fstab
(the command option, S-Find replaces the beginning ^[^ does not add # #, followed by the path)

5 processing the/etc/fstab path, using the SED command to remove its directory name and base name
~] #echo/etc/sysconfig/|sed-r ' s#[^/]+/?$## ' directory name
~] #echo "/ETC/FST/SD" | Sed-r '[email protected](. */) ([^/]+/?) [email protected]\[email protected]'

6 using SED to remove the IPv4 address of this machine in the Ifconfig command
~] #ifconfig |sed-n 2p |sed-e ' s/.*addr://'-e ' s/b.*//'

7 Statistics of all RPM files in the package directory on the CentOS installation CD-ROM the number of repetitions of the second-to-last field
~]# ls *.rpm |rev |cut-d.-f2 |rev|sort |uniq-c

This article from "Pony Brother Linux system operation and Maintenance" blog, reproduced please contact the author!

About the SED for text processing tools

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.