Linux Text Processing tool--sed

Source: Internet
Author: User
Tags line editor

SED: Data flow editor; Awk: Generator for report text

SED basic usage: (stream editor) stream editor
Line Editor
Full Screen Editor: Vi/vim
SED: Memory space (mode space)
By default, the source file is not edited, only the data in the pattern space is processed, and after processing is finished, the pattern space is printed to the screen

sed [options] ' addresscommannd ' file ...
-N: Silent mode, no longer displays the contents of the mode space by default
-I: Modify source files directly
-E script-e script: Multiple scripts can be executed at the same time
-F file:sed-f File Filetest.txt script file processing source files
-r: Indicates the use of extended regular expressions
History | Sed ' s/^[[:space:]]*//g ' | Cut-d '-f1

Address:
1. Startline,endline e.g:1,100
2./regexp/Regular Expression e.g:/^root/
3./pattern1/,/pattern2/pattern mode
The first line that was Pattern1 matched to the end of the line to which the first match was Pattern2, all the rows in the middle
4. linenumber; Specify rows
$: Countdown First line
$-1: The second line to the bottom
5. Startline,+n from the specified line to the back N rows

Command:
D: Delete qualifying lines e.g:sed ' 1,2d '/etc/fstab
sed '/oot/d '/etc/fstab
sed ' 1,+2d '/etc/fstab
P: Show eligible Row e.g:sed-n '/^\//p '/etc/fstab
a \string: Appends a new line after the specified line, with the contents of string e.g:sed '/^\//a \ #Hello world! '/etc/fstab
I \s Tring: Adds a new row before the specified line, with the contents of string e.g:sed '/^\//i \ #Oh, my god! '/etc/fstab
R file: Add the specified file contents to the qualifying line e.g:sed ' 2r/etc/issu E '/etc/fstab
W FILE: Save the row in the range specified by the address to the specified file e.g:sed '/oot/w/tmp/oot.txt '/etc/fstab
s/pattern/string/: Find and Replace, The default is to replace only the string
once matched to the e.g:sed ' s/oot/oot/'/etc/fstab
G: Global substitution e.g:sed ' s/oot/oot/g '/etc/fstab
I: ignore the character case br> s///:s###:[email protected]@@ e.g:sed ' [email protected]/@#@g '/etc/fstab
&: Reference pattern matches the entire string e.g: Sed ' s#. T#&er#g '/etc/fstab
e.g:sed ' s#\ (. \) oo\ (. \) #\1oo\2#g '/etc/fstab
Exercise:
1. Delete whitespace characters from the beginning of the/etc/grub.conf file;
Sed-r ' s/^[[:space:]]*//g '/etc/grub.conf

2. Replace the number 5 in the "Id:3:initdefault:" line in the/etc/inittab file;
Sed ' s/\ (id:\) [0-9]\ (: initdefault:\)/\15\2/g '/etc/inittab

3, delete the blank line in the/etc/inittab file;
Sed '/^$/d '/etc/inittab

4. Delete the # number at the beginning of the/etc/inittab file;
Sed ' s/^#//g '/etc/inittab

5. Delete the # number at the beginning of a file, but require a white-space character after the # number;
Sed-r ' s/^#[[:space:]]*//g '/etc/inittab

6. Delete the white space characters and # numbers that begin with a blank character followed by the # sign in a file
Sed-r ' s/^[[:space:]]*#//g '/etc/inittab

7. Take out a directory name for a file path
echo "/ETC/RC.D" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '

Linux Text Processing tool--sed

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.