Shell Scripting Learning

Source: Internet
Author: User

Stream Editor

Simple usage of SED

sed[options] sed cmds files

Option –e Connect multiple Edit commands

-f Specifies the SED script file name

-N prevents input lines from being entered automatically

Sed ' 4, $d ' test.in the first 4 lines of the output test.in file

Sed ' 3q ' test.in exit before 3 lines output

Sed ' s/public/public ' test.in replace the public word in the file with uppercase

How to locate addresses in Sed_cmd

N Represents the nth row
$ Represents the last line
M,n Represents rows from M to n
/pattern/ Queries the rows that contain the specified pattern, such as/disk/,/[a-z]/
/pattern/, N Represents rows from pattern matching to nth row
n,/pattern/ Represents rows from nth to rows that contain a specified pattern match
/mode 1/,/mode 2/ Represents a row from the containing pattern 1 to the containing pattern 2
! Reverse selection, indicating m,n! , then take the opposite result of m,n

Sed–n ' 3,5 ' test.in//Print line 3rd to 5th

Sed–n '/hello/p ' test.in//print out lines containing hello

Sed–n '/hello/= ' test.in//print out line numbers with Hello

Sed–n '/hello/d ' test.in//delete the line containing Hello

Sed Common editing commands

The SED replace command uses the format

"Address" s/old word/new word/"GPW"

Address: If omitted, indicates that all rows are edited

G: Global substitution, default means replacing only the first word to match

P: Print the modified line

W fname: Writes the replaced line contents to the specified file

Sed–n ' S/NORTH/NORTH/GP ' test.in

Sed–n ' s/north/north/w data ' test.in

Sed ' s/[0-9][0-9]$/&.5/' datafile

-n means blocking automatic output

Shell Scripting Learning

Related Article

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.