Long Yun-dimensional road--linux Text processing the SED of the Three Musketeers

Source: Internet
Author: User
Tags first string modifier


Sed, the abbreviation of Stream EDitor , is a Common text Processing tool in the Linux operating system, and can be easily processed by combining regular expressions with simple and convenient text. This article introduces the basic usage of SED as an example.

Basic Use Method

Basic syntax:sed [OPTION] [Addresscommand] FILE1 FILE2 ...

option{ common }

-N: Silent mode, no mode space displayed

-I: modify source files directly

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

-f/apath/to/sed_file: Reads the script from the file, executes

-R: Indicates support for extended regular expressions

Adress:

1.StartLine,Endline

such as:1,100

2./regexp/

/^root/: Matches a line starting with root

/$/: Last line

3./pattern1/,/pattern2/: From the first line to the pattern 1 to the first time by the pattern 2 Rows to match

4.LineNumber

The specified row

5.StartLine,+n

From StartLine onwards, N rows Backward

Command:

D: Delete the line that matches to

Example: deleting The first three lines of text in a/etc/fstab file

[[Email protected] tmp]# sed ' 1,+2d '/etc/fstab

P: Display rows that match the criteria, the rows that are matched to appear two times, lines that are not matched to only one time

Example: Show only lines in the /etc/fstab file that begin with #

[Email protected] tmp]# sed-n '/^#/p '/etc/fstab

A \string adds a row after the matched line

Example: inserting the hahah line after the lines beginning with # in the file /etc/fstab file

[[Email protected] tmp]# sed '/^#/a\hahaha '/etc/fstab

I \string usage with a, add new lines before qualifying lines

R FILE after the file is matched to the line, the contents of the file are read into the

Example: The first and second lines in the/etc/fstab file are read into the file /etc/passwd

[[Email protected] tmp]# sed ' 1,2r/etc/passwd '/etc/fstab

W FILE : Saves the row in the range specified by the address to the specified file

Example: Saving the first row and the second row in the Wenjiang/etc/fstab to the /tmp/fstab file

[[Email protected] tmp]# sed ' 1,2w/tmp/fstab '/etc/fstab

s/pattern/string/[ modifier ] : Find substitution, default replaces only the first string in each line that is matched to the pattern

Modifier:

G: Global substitution

I: Ignore case

Example: add D to the character that ends with o in the file test.txt

[Email protected] tmp]# sed ' [email protected]\ (o$\) @\[email protected] ' test.txt

Example: Remove all spaces at the beginning of the history command

[Email protected] tmp]# history | Sed ' [Email protected]^[[:space:]]*@@g '

Comprehensive examples

Example 1: removing whitespace characters from the beginning of a/etc/grub.conf file

[[Email protected] tmp]# sed ' [email protected]^[[:space:]]*@@ '/etc/grub.conf


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/DE/wKioL1VUxGvBEgEaAAQjuweaXhs342.jpg "title=" 1.png " Width= "height=" 317 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:317PX; "alt=" Wkiol1vuxgvbegeaaaqjuweaxhs342.jpg "/>


Example 2: Replace the /etc/inittab file " ID:3:initdefault: " the number in a row is 5

[[Email protected] tmp]# sed ' s/id:3:initdefault:\ (. *\)/id:5:initdefault "\1/g"/etc/inittab


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/DE/wKioL1VUxI_h1KJsAAD78UjnniM237.jpg "title=" 2.png " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:600px;height:90px;" alt= "Wkiol1vuxi_ H1kjsaad78ujnnim237.jpg "/>

Example 3: deleting Blank lines in a/etc/inittab file

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


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/DE/wKioL1VUxKbTDZ8nAAI1y-SNJUg468.jpg "title=" 3.png " Width= "height=" 247 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:247PX; "alt=" Wkiol1vuxkbtdz8naai1y-snjug468.jpg "/>

Example 4: Delete the # number at the beginning of the /etc/inittab file

[[Email protected] tmp]# sed ' s/^#//'/etc/inittab


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/E3/wKiom1VUw0TiBSlOAAEXA9jdrhs004.jpg "title=" 4.png " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:600px;height:150px;" alt= " Wkiom1vuw0tibsloaaexa9jdrhs004.jpg "/>

Example 5: Deleting the # number at the beginning of a file but requires a white- space character after the # number

The original file contents are as follows


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/DE/wKioL1VUxNbCRpoTAACO-D6-Uv0667.jpg "title=" 5.png " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:600px;height:88px;" alt= " Wkiol1vuxnbcrpotaaco-d6-uv0667.jpg "/>


[[Email protected] tmp]# sed ' s/#*\ ([[: space:]]\+\)/\1/' Test.txt


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/E3/wKiom1VUw3XS50H9AADDjm8jOlY872.jpg "title=" 6.png " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:600px;height:88px;" alt= " Wkiom1vuw3xs50h9aaddjm8joly872.jpg "/>

Example 6: Delete the white space character and # in the beginning of the line with thewhite space followed by the # sign in a file


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/E3/wKiom1VUw4vAdkYtAAF3p-vL-Rg864.jpg "title=" 7.png " Width= "height=" 169 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:169PX; "alt=" Wkiom1vuw4vadkytaaf3p-vl-rg864.jpg "/>

Example 7: Remove a directory name for a file path

[Email protected] tmp]# echo "/etc/yum.repo/new.repo" | Sed ' [email protected]/.*/\ (. *\)/.*@\[email protected] '


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/E3/wKiom1VUw6Xjf6WOAAHgBly3P8k414.jpg "title=" 8.png " Width= "height=" 321 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:321px; "alt=" Wkiom1vuw6xjf6woaahgbly3p8k414.jpg "/>



This article is from the "linux-Bubble" blog, make sure to keep this source http://7703592.blog.51cto.com/7693592/1651433

Long Yun-dimensional road--linux Text processing the SED of the Three Musketeers

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.