Linux Shell Basics (v) SED command (in edit)

Source: Internet
Author: User
Tags processing text

One step in writing a shell is to take a piece of text and process it.

I. SED command

Sed is a powerful streaming editor (stream editor for filtering and transforming text) that works perfectly with regular expressions, processing text line by row and displaying the results on the screen. The strength of SED is to replace specific characters in text.

Usage:
sed [options] ... {Script (if no other script)} [Input file]
sed [options] ' command ' file (s)
sed [options]-F scriptfile file (s)

Options:
-P Lookup
-D Delete
-S replacement
Sed-n '/5p/' passwd

Ii. sed Matching search usage

Sed can look for lines that contain keywords in the text of the file and display the row on the screen like grep.
Command notation:sed-n '/(matched string)/' p filename (précis-writers is-NP)

1. Search for the line containing the root keyword in the passwd file.

Sed-n '/root/' P passwd

2. Search for lines in the SSH file that begin with #

Sed-n '/^#/' p SSH

3. Search for rows of the regular ' o+t ' type

Sed-nr '/o+t/' P passwd


Note that the "+" number belongs to the extended regular, which needs to be followed by the-R option

4. Search for regular ' two o ' lines

Sed-nr '/o{2}/' P passwd

5, search the regular root or bus line ' Root|bus '

Sed-nr '/root|bus/' P passwd

6. Case-insensitive search for the line that contains the keyword

Sed-n '/nologin/' Ip passwd Note is a capital letter I, not a pipe symbol |

Expand Usage: Print the specified line

Command notation: Sed-n ' line number or line number description ' p file path Note single quote ' does not need to be followed by two//
1. Display the contents of the 2nd line of the file

Sed-n ' 2 ' p passwd

2. Display the contents of line 6th to 10 of the file

Sed-n ' 6,10 ' p passwd note the middle is a comma, not-

3. Full text display

Sed-n ' 1,$ ' p passwd note cannot be written as "Sed-n ' ^,$ ' P passwd"

Extended usage: multi-conditional filtering

The command can match multiple conditions with the-e parameter, noting that multiple conditions are or relationships, as long as one of those matches is found, and if a two rule is matched, the row will be displayed two times

Displays the first row and the row containing the root keyword

Sed-e '/root/' p-e ' 1 ' p-n passwd

Iii. sed delete a specific line usage

For a particular large file, using the SED command to delete part of the content, more than vim to open the corresponding file and then delete to be more practical. The deletion without the-I option is only the deletion on the display, does not affect the contents of the file itself, but adds the-I option to the original file, so be sure to do a backup before deleting it.

Command wording: sed '/(keyword)/' d file path
1. Delete the first line of the file

Sed ' 1 ' d 1.txt

Note that the first line has been erased.

2. Delete the line with the keyword "bus"

Sed '/bus/' d 1.txt

3. The idea of deleting documents before a specific time period
First, use the Grep-n > command to filter the number of trips through the time keyword.
Then use sed ' 1 to find the last line of line number ' d way to delete the data

Iv. sed replaces specific content usage

Command wording:
Sed ' 1, $s///g ' file path

Sed ' s/[0-9]//g ' file: Used to determine whether the input content is a number
Sed ' s/[a-za-z]//g ' file name

Extended regular more symbols than regular

V. SED exchange of String Order Usage

Command wording:
parentheses, splitting,
Sed-r ' s/(Rot) (. *) (bash)/\3\2\1 '

The sed ' s/^.*\$\/123&/' text is prefixed with 123 at the top of each line

Sed-i ' s/ot/to/g ' file to change the contents of the file, not displayed on the screen

Linux Shell Basics (v) SED command (in edit)

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.