Linux commands (12) SED

Source: Internet
Author: User
Tags first string modifier line editor

SED basic usage:
Sed:sTreamEdItor
Line Editor (full screen Editor: VI)

SED: Mode space
By default, the original file is not edited, only the data in the pattern space is processed, and after the processing is finished, the pattern space is printed to the screen;


sed [options] ' addresscommand ' file ...
-N: Silent mode, no longer displays the contents of the mode space by default
-I: Modify the original file directly
-E script-e script: Multiple scripts can be executed at the same time
-f/path/to/sed_script
Sed-f/path/to/scripts File
-r: Indicates the use of extended regular expressions

Address:
1, Startline,endline
Like 1,100.
$: Last line
2,/regexp/
/^root/
3,/pattern1/,/pattern2/
The first line that is matched by the pattern1 begins at the end of the line that is first matched to the pattern2, and all the rows in the middle
4, LineNumber
The specified row
5, StartLine, +n
Starting from Startline, n rows backward;

Command:
D: Delete the qualifying line;
P: Displays rows that match the criteria;
A \string: Appends a new line after the specified line, with the contents of string
\ n: can be used for line wrapping
I \string: Adds a new row before the specified line, with the contents of string
R file: Adds the contents of the specified file to the qualifying line
W File: Save the row in the range specified by the address to the specified file;
s/pattern/string/modifier: Find and Replace, default replaces only the first string in each line that is matched to the pattern
Add modifier
G: Global Substitution
I: Ignore character case
s///: s###, [email protected]@@
\ (\), \1, \2

L.. E:like-->liker
Love-->lover

Like-->like
Love-->love

&: Reference pattern matches entire string

Sed exercises:
1, delete the/etc/grub.conf file in the beginning of the blank character;
Sed-r ' [email protected]^[[:spapce:]][email protected]@g '/etc/grub.conf
2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;
Sed ' [email protected]\ (id:\) [0-9]\ (: initdefault:\) @\15\[email protected] '/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 ' [email protected]^#@@g '/etc/inittab
5, delete the beginning of a file # and the following white space characters, but requires the # number must be followed by a blank character;
Sed-r ' [email protected]^#[[:space:]][email protected]@g '/etc/inittab
6. Delete white space characters at the beginning of the line of the # class followed by white space characters in a file and #
Sed-r ' [Email protected]^[[: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] '
Base name:
echo "/etc/rc.d/" | Sed-r ' [Email protected]^/.*/([^/]+)/[email Protected]\[email protected] '

Linux commands (12) 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.