Linux Learning Note 2 (SED command)

Source: Internet
Author: User

Original: http://blog.oldboyedu.com/commands-sed/

Syntax format
sed [options] [sed-commands] [input-file]sed [options]     [sed command]      [input file] Description:1. Note there is at least one space between the SED and the following options.  2. To avoid confusion, this article calls sed the SED software. The sed-commands (sed command) is a number of command options built into the SED software, and is called the SED command in order to differentiate itself from the previous options.  3. sed-commands can be either a single sed command or multiple sed command combinations.  4. Input-file (input file) is optional, and SED can also get input from standard inputs such as pipelines.
Basic operations
~]# Cat person.txt101, Oldboy,ceo102, Zhangyao,cto103, Alex,coo104, YY,CFO, Feixue,cio
Increase

A appends text to the specified line

I insert text before the specified line

[[Email protected] ~]# SED'2a 106,DANDAN,CSO'Person.txt101, Oldboy,ceo102, Zhangyao,cto106, DANDAN,CSO103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# SED'2i 106,DANDAN,CSO'Person.txt101, Oldboy,ceo106, DANDAN,CSO102, Zhangyao,cto103, Alex,coo104, YY,CFO the, Feixue,cio

Multi-line increase

[[Email protected] ~]# SED'2a 106,dandan,cso\n107,bingbing,cco'Person.txt101, Oldboy,ceo102, Zhangyao,cto106, DANDAN,CSO #→ 1th notation107, Bingbing,cco103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# SED'2a 106,DANDAN,CSO \>107, Bingbing,cco'Person.txt101, Oldboy,ceo102, Zhangyao,cto106, DANDAN,CSO #→ 2nd notation107, Bingbing,cco103, Alex,coo104, YY,CFO the, the feixue,cio#→sed command i is used the same way, so it is no longer listed. 

Specifies the range of addresses to execute

sed software can process single or multiple lines. If you do not specify an address range before the SED command, all rows are matched by default. Usage: n1[,n2]{sed-commands} addresses are separated by commas, and n1,n2 can be represented by numbers, regular expressions, or a combination of the two. Example:Ten{sed-commands} action on line 10thTen, -{sed-commands} for 10 to 20 rows, including 10th, 20 rowsTen, + -{Sed-commands} on 10 to 30 (Ten+ -) line operations, including 10th, 30 rows1~2{sed-commands} to 1,3,5,7,...... Row OperationsTen, ${sed-commands} action on 10 to last line ($ for last row), including line 10th/oldboy/{sed-commands} line operation to match Oldboy/oldboy/,/alex/{sed-Commands} to match the line of Oldboy to the row operation of Alex/oldboy/,${sed-commands} line-to-last row for matching Oldboy/oldboy/,Ten{sed-commands} to match the Oldboy row to line 10th operation, Note: If the first 10 rows do not match to the oldboy,sed software will display 10 lines after the matching Oldboy line, if any. 1,/alex/{sed-commands} line action on line 1th to match Alex/oldboy/,+2{Sed-commands} action on line to match Oldboy to 2 rows thereafter
By deleting

d Delete the specified row

[[Email protected] ~]# SED'D'Person.txt[[email protected]~]#[[email protected]~]# SED'2d'Person.txt101, Oldboy,ceo103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# SED'2,5d'Person.txt101, Oldboy,ceo[[email protected]~]# SED'3, $d'Person.txt101, Oldboy,ceo102, Zhangyao,cto[[email protected]~]# SED'1~2d'Person.txt102, Zhangyao,cto104, Yy,cfo[[email protected]~]# SED'1,+2d'Person.txt104, YY,CFO the, Feixue,cio[[email protected]~]# SED'/zhangyao/d'Person.txt101, Oldboy,ceo103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# SED'/oldboy/,/alex/d'Person.txt104, YY,CFO the, Feixue,cio[[email protected]~]# SED'/oldboy/,3d'Person.txt104, YY,CFO the, Feixue,cio
Change

Replace by line:

C replaces old rows with new lines

' 2c 106,DANDAN,CSO ' Person.txt 101 , Oldboy,ceo 106 , DANDAN,CSO 103 , Alex,coo 104 , YY,CFO Feixue,cio,

Text substitution:

S: use alone → replace the first matched string in each row with the ==>sed command
G: Replace all of the ==>sed command s with one of the replacement flags for each row, non-sed command
-I: Options for modifying file content ==>sed software

[[Email protected] ~]# SED's#zhangyao#oldboyedu#g'Person.txt101, Oldboy,ceo102, Oldboyedu,cto103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# Cat Person.txt101, Oldboy,ceo102, Zhangyao,cto103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# Sed-i's#zhangyao#bbb#g'Person.txt[[email protected]~]# Cat Person.txt101, Oldboy,ceo102, Bbb,cto103, Alex,coo104, YY,CFO the, Feixue,cio

Variable substitution:

[[Email protected] ~]# Cat Test.txt #→ Create a new text Aba[[email protected]~]# x=A[[email protected]~]# y=B[[email protected]~]# echo $x $ya b[[email protected]~]# sed s# $x # $y #g test.txtbbb[[email protected]~]# SED's# $x # $y #g'Test.txtaba[[email protected]~]# SED's#'$x'#'$y'#g'Test.txtbbb[[email protected]~]# SED"s# $x # $y #g"Test.txtbbb[[email protected]~]# eval sed's# $x # $y #g'test.txtbbb

Group substitution:

' s#^.*am \ ([a-z].*\) tea.*$#\1#g '  's#^.*am ([a-z].*) tea.*$#\1#g's#i (. *) (. *) teacher.#\1 \2#g'amoldboy

oldboyReplace with charactersI am oldboy teacher.

The special symbol & represents the replaced content

' 1,3s#c#--&--#g ' person.txt #→ here & equals c101, Oldboy,--c--eo      #→ to replace 1 to 3 rows of C with--c--102, Zhangyao,--c--to103, yy,--c--OO104, FEIXUE,CFO, Dandan,cio
Check

P outputs the specified content, but outputs 2 matches by default, so use N to cancel the default output

[[Email protected] ~]# SED'2p'Person.txt101, Oldboy,ceo102, Zhangyao,cto102, Zhangyao,cto103, Alex,coo104, YY,CFO the, Feixue,cio[[email protected]~]# Sed-n'2p'Person.txt102, Zhangyao,cto[[email protected]~]# Sed-n'2,3p'Person.txt102, Zhangyao,cto103, Alex,coo Description: Take the line with SED, the simplest [[email protected]~]# Sed-n'1~2p'Person.txt101, Oldboy,ceo103, Alex,coo the, Feixue,cio[[email protected]~]# Sed-n'P'Person.txt101, Oldboy,ceo102, Zhangyao,cto103, Yy,coo104, FEIXUE,CFO the, Dandan,cio

Find by string

 [[email protected] ~]# sed-n  " /cto/p   "  person.txt   102  ,zhangyao,cto[[email protected]  ~]# sed-n "  /cto/,/cfo/p   "  person.txt  102   Zhangyao,cto  103  ,alex,coo  104 , YY,CFO 
' 2,/cfo/p ' Person.txt 102 , Zhangyao,cto 103 , Alex,coo 104  '/feixue/,2p'  person.txt, feixue,cio# → Special case, the first two lines do not match to Feixue, it is backwards matched, if matched to Feixue print this line. 

Linux Learning Note 2 (SED command)

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.