SED Basic usage usage

Source: Internet
Author: User

Sed:stream Editor

Line Programmer (Full screen editor: VI)

Mode space

By default, the original file is not edited, only the data of the pattern space is processed, and when the processing is finished, the pattern space is printed to the screen;

sed [Optians] ' addresscommand ' file ...

-n silent mode, which does not display content in the mode space.

-I directly modify the original file

-E script-e script: Multiple scripts can be executed at the same time

-f/path/sed_script:

Sed-f/path/to/scripts File

-R indicates the use of extended regular expressions

Address:

1, Startline,endline

1,100 rows from 1 to 100

$: Last line

$-1: The second line to the bottom

2./regexp/Regular Expression

/^root/

3./pattern1/,/pattern2/: End of line starting from pattern 1 matching line to pattern 2

4, LineNumber: The specified line

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

I \string: Adds a new row before the specified line, with the contents of string

R FilePath: Adds the contents of the specified file to the qualifying line

W filepath: Save the content in the specified range to the specified file;

s/pattern/string/: Find and Replace, default replaces only the first string in each line that is matched to the pattern

Add modifier:

G: Global Replacement s/pattern/string/g

I: Ignore case when finding

\ (\), \1,\2

&: Reference pattern matches entire string

s###: Same as s///, delimiter can be customized, or use @,[email protected]@@ format

Sed '/oot/d '/etc/fstab: delete oot matching line, mode required//mode is generated

Sed ' 1,+2d '/etc/fstab: Delete line 1th and 22 lines ' 1d ' to delete only the first row

Sed '/^\//d ' delete/start line

Sed-n '/^\//p ' only show/start lines

Sed '/\//a \hello ' begins with a line appended with the specified content of Hello

Sed '/^\//a \ #hello \n\ #hello2 ': Append two lines \ n = newline character

Sed ' 2r/etc/issue '/etc/fstab: Insert issue file after the 2nd line of Fstab

Sed ' 1,2r/etc/issue '/etc/fstab

Sed '/oot/w/tmp/oot.txt '/etc/fstab saves rows matching oot to Oot.txt file

Sed-n '/oot/w/tmp/oot.txt '/etc/fstab turns off display mode and outputs files directly to the Oot file

Sed ' s/^\//#/' to replace the/in file with #

Sed ' s/l. E/&r/g ' Sed.txt & refers to the preceding string to find a matching string, plus the letter R

Sed ' s/\ (L.. e\)/\1r/g ' ibid., using the back-to-reference method

Sed ' s/l\ (.. e\)/l\1 ' will file in L.. The matching character of E, the character replaced by the L switch

History | Sed ' s/^[[:space:]]*//g ' will be the beginning of the deletion of a space, * representing no matter if there are multiple characters,

Sed ' s/\ (id:\) [0-9]\ (: initdefault:\)/\15\2/g ' (turns the number 5 in Id:3:initdefault: to 3)


String test:

Compares two strings for consistency, using a

= =, usage:

["$A" = = "$B"] [$A = $B]

! =: Test Whether it is unequal, not equal to true, etc as false

>

<

-N String: Tests whether the specified string is empty, empty is true, or false

-S string: Tests whether the specified string is not empty, is not empty, and the null is False

Example: #!/bin/bash

if [' id-n u $ ' = = ' id-n-G $ '];then

Echo ' Same '

Else

Echo ' Err '

Fi

#!/bin/bashif [$ = ' Q '];then echo "quitting ..." exit 0elif [$ = "Q"];then echo "quitting ..." Exit 2elif [$ = "Quit"];then echo "quitting ..." exit 3elif [$ = "Quit"];then echo "quittin G ... "Exit 4else Echo $1fi
echo "SCALE=2;111/22" | Bc
BC <<< "SCALE=2;111/22"

scale=2 reserved 2-bit accuracy

SED Basic usage usage

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.