Linux sed command (daily one learns)

Source: Internet
Author: User

1th SED Foundation 1.1 sed options

-R supports extended regular

-N cancels the default output

-I direct modification does not output to the desktop

-i.bak Backup

The writing format of 1.2 sed

Sed ' conditional command '

Sed ' pattern command '

Sed ' mode command '

1.3 sed command execution process

1. Read the contents of the file 1th line

2. Whether the conditions are met

1). The command that satisfies the condition executes the corresponding P s D

2). Not satisfied continue to 1th step

3. To the last line of the file

Chapter 2nd use of the SED command

Sed add and delete to change the no I option will only change the contents of the output to not really modify, plus I option will directly modify the file content

2.1 Sed increased CAI

C Replace replacement

A append Append

I insert Inserts

2.1.1 Append 12306,xiao,ufo to the third line of the Person.txt file

View Person.txt File

[email protected] lx]# cat Person.txt101,zeq,ceo102,zhang,cto103,love,coo104,yy,cfo105,faker,cio110,ahao,coco

Append 12306,xiao,ufo to the third line

[[Email protected] lx]# sed ' 3a12306,xiao,ufo ' person.txt      a append, 3a added 101,zeq,ceo102,zhang,cto103,love under the third line, Coo12306,xiao,ufo104,yy,cfo105,faker,cio110,ahao,coco
2.1.2 inserting 12306,xiao,ufo on the third line of the Person.txt file
[Email protected] lx]# sed ' 3i12306,xiao,ufo ' person.txt101,zeq,ceo102,zhang,cto12306,xiao,ufo103,love,coo104,yy, Cfo105,faker,cio110,ahao,coco
2.1.3 Replace the third line of the Person.txt file with 12306,xiao,ufo
[roo[email protected] lx]# sed ' 3c12306,xiao,ufo ' person.txt101,zeq,ceo102,zhang,cto12306,xiao,ufo104,yy,cfo105, Faker,cio110,ahao,coco
2.1.4 added in last line of file

12306,xiao,ufo

12580,tao,xo

[[Email protected] lx]# sed  ' $a 12306,xiao,ufo\n12580,tao,xo ' person.txt    $ last line   \ n Enter 101,zeq,ceo102,zhang , Cto103,love,coo104,yy,cfo105,faker,cio110,ahao,coco12306,xiao,ufo12580,tao,xo
2.2 sed Delete d 2.2.1 Delete Row 2nd to line 5th
[[Email protected] lx]# sed ' 2,5d ' Person.txt101,zeq,ceo110,ahao,coco
2.2.2 Delete the line from YY to the end of the file
[[Email protected] lx]# sed   '/yy/, $d ' person.txt             $ file End 101,zeq,ceo102,zhang,cto103,love,coo
2.2.3 Delete rows that contain Ahao
[[Email protected] lx]# sed '/ahao/d ' Person.txt101,zeq,ceo102,zhang,cto103,love,coo104,yy,cfo105,faker,cio
2.2.4 Delete rows that do not contain Ahao
[[Email protected] lx]# sed '/ahao/!d ' person.txt             ! Take the reverse, exclude 110,ahao,coco
2.3 sed replaces ' s## #g ' 2.3.1 Replace all numbers
[[Email protected] lx]# sed ' s#[0-9]# #g '  Person.txt,zeq,ceo,zhang,cto,love,coo,yy,cfo,faker,cio,ahao,coco
2.3.2 Replace the first digit in each line
[[Email protected] lx]# sed ' s#[0-9]## '  person.txt       sed ' s#[0-9]## ' without G, default is match to the first number of each line       sed ' s#[0-9]## ' Match the second digit of each line 01,zeq,ceo02,zhang,cto03,love,coo04,yy,cfo05,faker,cio10,ahao,coco
2.3.3 The IP address after the reference is taken
[Email protected] ~]# ifconfig eth0 |sed-rn ' 2s#^.*r: (. *)  BC.*#\1#GP ' 10.0.0.200
[[email protected] ~]# ifconfig eth0 |awk-f ' [:]+ ' Nr==2{print $4   } ' 10.0.0.200
[[email protected] ~]# ifconfig eth0 |awk-f ' addr:|  Bc ' nr==2{print $   ' 10.0.0.200
[Email protected] ~]# ifconfig eth0 |awk-f ' [^0-9.] + '   nr==2{print $ ' 10.0.0.200
2.4 sed 2.4.1 Display the 5th line of the file
[Email protected] ~]# sed-n 5p Person.txt105,faker,cio
2.4.2 Show the file line 2nd to line 5th
[Email protected] ~]# sed-n ' 2,5p ' Person.txt102,zhang,cto103,love,coo104,yy,cfo105,faker,cio
2.4.3 display the last line of the file
[Email protected] ~]# sed-n ' $p ' Person.txt110,ahao,coco
2.4.4 displays the lines in the file that contain Zeq
[Email protected] ~]# sed-n '/zeq/p ' Person.txt101,zeq,ceo
2.4.5 display a line from a file that contains 101 lines to 105
[Email protected] ~]# sed-n '/101/,/105/p ' Person.txt101,zeq,ceo102,zhang,cto103,love,coo104,yy,cfo105,faker,cio
2.4.6 Special wording: Displays the 1th and 4 lines and 5 lines of the file
[Email protected] ~]# sed-n ' 1p; 4p; 5p ' Person.txt101,zeq,ceo104,yy,cfo105,faker,cio
2.4.7 Show a regular line
[[email protected] ~]# seq |sed-n ' 1~2p ' 13579
Using variables in 2.5 sed commands
[Email protected] ~]# X=zeq               set x variable to zeq[[email protected] ~]# y=zeq1             set y variable to zeq1[[email protected] ~]# sed ' s# $x # $y #g '  Person.txt101,zeq1,ceo102,zhang,cto103,love,coo104,yy,cfo105,faker,cio110,ahao,coco12306,xiao. Ufo
2.5.1 Replace the contents of each line in the file Person.txt file with the corresponding line number
The                     for loop in the shell script used here for n in {1..7} does     Sed-i.bak "${n}s#.*# $n #g" Person.txtdone
2.5.2 Batch rename: Delete html_ in file name

Touch zeq_html_{01..10}.jpg

Method 1  command-line format [[email protected] data]# ls *.jpg|sed-r "s# (. *) (_.*) #mv & Zeq\2#g" |bash
Method 2 for  loop for I on {01..10}domv zeq_${i}.jpg  zeq_html_${i}.jpg Done
Method 3  renamerename  html_  ""    *.jpg        rename   modify who    changed what    file

Linux sed command (daily one learns)

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.