Sed line Editor

Source: Internet
Author: User

sed: line editor, which does not process source files by default.

-I directly modify the source file

-escript–e Script can execute multiple scripts simultaneously

-f/path/to/sed_script

sed–f/path/to/scripts file

- R: indicates the use of extended regular expressions

History |sed ' s/[[:space:]]//' use the sed command to delete the line space

1.sed ' Source address + edit Command ' file

Address:1,100

$ last line

$-1 Countdown second line

2 mode:/ Regular expression

/^root/

3 /mode 1/,/ mode 2/

4. Specify the line

5.starline+n N rows backward from Starline

d: Delete rows that match the criteria

sed ' 1,2d '/etc/fstab Delete the first row to the second row

sed '/oot/d '/etc/fstab delete the row containing the root

sed ' 1,+10d '/etc/fstab delete the first line and the following lines

P: displays rows that match the criteria.

sed '/\//p '/etc/fstab displays lines with a slash. The same will show 2 times

- N: show only rows that match the criteria

Sed-n '/root/p '/etc/passwd

a \ "string": appends a new line after the specified line with the contents "string"

sed '/root/a \# Hello World '/etc/passwd added after the line containing the root #helloworld

sed '/root/a \# Hello world \ #hello '/etc/passwd add 2 rows later

I \string: adds a new line before the specified line. Content is string

sed '/root/i\ #hello \ n #hello '/etc/passwd add 2 lines in front

r file adds the specified file contents to the qualifying line

sed ' 10r/etc/passwd '/etc/fstab add passwd content to line /etc/fstab

sed ' $ r/etc/passwd '/etc/fstab inserted in the last line after $ passwd

sed ' 1,2r/etc/issue '/etc/fstab inserts the contents of issue after the first and second lines

W Save content in specified range to specified file

sed '/oot/w/tmp/oot.txt '/etc/fstab oot in fstab save to /tmp/oot.txt

s find and replace:s/ What to look for ( support Regular expression )/ Replace with specified content (regular expression not supported)/

sed ' s/oot/oot/'/etc/fstab find oot in fstab replaced with oot

Regular expressions are supported:

sed ' s/^\//#/'/etc/fstab finds lines in Fstab , starting with / . and put / replace #

The default is to replace only the first occurrence of the character in each row

Add modifier g: Global substitution

Sed ' s/\//#/g '/etc/fstab

I: ignore case when finding

Sed ' s/oot/ooo/i '/etc/fstab

s///: s# #: [Email protected] @

Support references \ (\), \1 \2

sed ' s#l. E#&r#g ' sed.txt will sed in L. the word reference of E is replaced with 1..er

&: represents the entire string that the reference pattern matches to

or sed ' s#\(L. E\)#\1r#g ' Sed.txt

1. delete /etc/grub.conf whitespace characters at the beginning of this file

[Email protected]~] #sed ' s/^[[:space:]]//'/etc/grub.conf

2. Replace the number 5 in the "Id:3:initdefault:" line in the /etc/inittab file

[Email protected]~] #sed ' [email protected]\ (id:\) [0-9]\ (: initdefault:\) @\15\[email protected] '/etc/inittab

3. Delete blank lines in the /etc/inittab file

[[Email protected]~] #sed ' s/^$/d '/etc/inittab

4. delete the # number at the beginning of the /etc/inittab file and require that the white space character be followed by #

[Email protected]~] #sed-R ' s/^#[[:space:]]//'/etc/inittab

5. Remove a directory name for a file path

[Email protected]~] #echo "/etc/rc.d" |sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '

6. write a script to pass three, the first is an integer, the second is an operator, the third is an integer, the operation, the exact 2 decimal places

/bin/bash

#

echo "Scale=2;$1$2$3" |BC

7. write a script. Pass three parameters to compare size

/bin/bash

#

if [$1-gt $2];then

if [$3-gt$1];then

echo "$"

Else

echo "$"

Fi

Else

If [$3-gt $];then

echo "$"

Else

echo "$"

Fi

Fi


Sed line Editor

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.