Linux sed command detailed

Source: Internet
Author: User

SED is a non-interactive text editor that can edit text files and standard inputs, which can come from keyboard input, file redirection, strings, variables, or pipe text. and copy it to the buffer, and then read the command line command to edit the line number required by this command.

usage :

There are three ways to use SED:

① is used directly on the command line

sed [options] ' sed command ' input file

② the SED command to the script file, using the SED command to invoke the

sed [options]-F SED script file input file

③ writes the SED command to the script file and sets it to the executable

./sed script File Input file

One of the script files in ③ needs to start with the #! symbol.

Options commonly used in SED

-N: Do not print all rows to standard output, only those rows affected by the command are output

-E: To resolve the next string to the sed edit command, if there is only one edit command, then-E can omit

-F: Indicates that the SED script file is being invoked

-R: Regular notation that supports extended type (default only supports basic regular notation)

-I: Directly modifies the contents of the read file, rather than through the screen output.

SED command

Sed commands are usually made up of two parts. Text positioning and sed editing commands.

The text is positioned to extract a portion of the line of text, and the edit command edits the extracted rows.

Sed positioning method

SED provides two ways of locating:

① by line number, specifying a row or line number range

② using regular expressions

Positioning method:

X:X for the specified line number

X, Y: Specifies the range of line numbers from X. to Y

/pattern/: Querying for rows that contain patterns

/pattern/pattern/: Query for rows with two patterns

x,/pattern/: Between rows from x lines to patter pattern matches

x,y!: Query for rows that do not include X and Y line numbers

SED edit command

P: Print matching lines

=: Print File line number

A: Append text messages after locating line numbers

I: Inserting text information before locating line numbers

D: Delete anchor row

C: Replace the text line with new text

S: Replace pattern with replacement mode

R: Reading text from another file

W: Write text to a file

Y: Transform character

Q: Exit after first pattern match

{}: Executing a command group on the anchor row

N: Reads the next input line and processes the new row with the next command

Having said so much, let's start by experiencing:

[[email protected] 15:11 ~/stu] $ nl Test      1 First line  2 second line 3 L i n E 4 What's this                  [[email protected] 15:11 ~/stu]$ sed ' 2d ' test first linel i n ewhat is this
    

This is one of the basic ways of using SED.

Sed ' 2d ' test:2 means to navigate to the second row, D for delete, and test for the input file.

Use of option-n

[[email protected] 16:22 ~/stu] $ cat Testfirst linesecond linel i n ewhat is this [[email protected] 16:22 ~/stu]$ Sed-n ' 2,4p ' testsecond linel i n ewhatis this[  [email protected] 16:22 ~/stu]$

In addition to using the-e option, you can implement the same functionality using {} and;

[[email protected] 17:04 ~/stu]$ Cat Test | Sed-n ' 3,$ ' {D;P}Sed:-e expression #1, character 3: Missing Command-bash:p}: command not found[[email protected] 17:04 ~/stu]$ Cat Test | Sed-n ' 3,${d;p} '[[email protected] 17:05 ~/stu]$ Cat Test | Sed-n ' 3,${p;=} 'l i n e3 What's This4[[email protected] 17:05 ~/stu]$

Use of option-E

[[email protected] 16:26 ~/stu]$ cat Test |  Sed-ne ' 2,/is/p '-e ' 2,/is/=' secondline 2l n e3What's this 4[[email protected] 16:27 ~/stu]$

Use of option-f

First, create a SED script file with the following content and give the executable permission.

[[email protected] 16:45 ~/stu]$ cat Append.sed#!/bin/sed-f/this/athis is the first append linethis are the second append line[[email protected] 16:45 ~/stu]$ cat Testfirst linesecond linel i n ewhat is this[[email protected] 16:47 ~/stu]$ which sed/bin/sed[[email protected] 16:47 ~/stu]$ ll Total dosage8-rw-rw-r--.1 FUWH FUWH 84 August 11 16:45append.sed-rw-rw-r--.1 Fuwh fuwh 44 August 11 15:10Test[[email protected] 16:47 ~/stu]$ chmod u+x append.sed[[email protected] 16:48 ~/stu]$ ll Total dosage8-rwxrw-r--.1 FUWH FUWH 84 August 11 16:45append.sed-rw-rw-r--.1 Fuwh fuwh 44 August 11 15:10Test[[email protected] 16:52 ~/stu]$./append.sed Testfirst Linesecond linel i n ewhat are Thisthis is the first append linethis are the second append line [[email protected] 16:53 ~/stu]$

Replace command s

Replaces the command to replace the matched text with the new text. The command format is as follows:

s/replaced by string/new string/[replace option]

Replacement options and meanings:

G: Represents all occurrences of the replacement text

P: Combine with-N to print only replacement lines

W file name: Indicates that the output is redirected to a file

[[email protected] 17:33 ~/stu] $ sed ' s/line/row/g ' testfirst rowsecond rowl i n ewhat is this [[email protected] 17:35 ~/stu]$

Linux sed command detailed

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.