Linux sed Command specific explanation + How to replace line break "\n" (very many interviews asked)

Source: Internet
Author: User

Sed
SED is a powerful text-processing tool
Be able to use regular matching. Insert and delete changes to text
When SED is processed, one line is processed at a time, and each time the current processing is stored in a temporary buffer. After processing the output buffer content to the screen, and then read the next line into the buffer, so repeatedly, until the end.




1. command format and number of references
sed [-NEFR] [action] File
Number of references:
-N Quiet mode. At the time of sed processing. All data from stdin will be output to the terminal. Plus-n will just output which line of processing
-E performs SED action editing directly on the command column
-F Write the SED action directly in the file
-R SED action supports extended regular expression (default is only the underlying regular)
-I directly change the contents of the file (use caution, especially when doing exercises with system files)


Action:
A append: Add. Add on the next line of the current row
C : Instead, replace the line between N1 and N2
D Delete: delete
I Insert, the top row of the line is inserted now
P Print. Often used with-n
S instead, s/old/new/g


2, the basic use method specific explanation
(1) Add a line after the first line

[[email protected] ~]# NL file.txt | SED "1a Add text"     1  wtmp begins Mon Feb 14:26:08 2014add text     2  192.168.0.1     3  162.12.0.123 4 The last line  
(2) Add a row before the first line
[[email protected] ~]# NL file.txt | Sed "1i Add text" Add text     1  wtmp begins Mon Feb 14:26:08     2  192.168.0.1     3  162.12.0.123
   4 the last line  
(3) Delete 2nd, 3 lines
[[Email protected]lhost ~]# nl file.txt | Sed "2,3d"     1  wtmp begins Mon Feb 14:26:08     4 This was the last line  
(4) print 2nd, 3 lines
[Email protected] ~]# sed-n "2,3p" file.txt 192.168.0.1162.12.0.123

The point here is to try to use-N. Otherwise, this will happen.
[[Email protected] ~]# sed "2,3p" file.txt wtmp begins Mon Feb 24 14:26:08 2014192.168.0.1192.168.0.1162.12.0.123162.12.0. 123this is the last line

(5) Change 168 to 169
First look at the source file
[email protected] ~]# cat file.txt wtmp begins Mon Feb 14:26:08 2014192.168.0.1162.12.0.123this
After processing
[Email protected] ~]# sed "s/168/169/g" file.txt wtmp begins Mon Feb 14:26:08 2014192.169.0.1162.12.0.123this is the L AST Line

(6) Inserting multiple lines
[[email protected] ~]# NL file.txt | Sed "2afirst\nsecond" file.txt wtmp begins Mon Feb 14:26:08 2014192.168.0.1firstsecond162.12.0.123this

(7) match the data and do the operation
Only need to add a regular match on the basis above
Sed "/matching mode/method of processing" file.txt
Sed "/^root/d" file.txt to start with root delete
Like what
Match begin, and delete a row
[[email protected] ~]# NL file.txt | Sed "/begin/d"     2  192.168.0.1     3  162.12.0.123     4  This was the last line
Match 123, and replace the 123 line 162 with 172.
[[email protected] ~]# NL file.txt | Sed "/123/{s/162/172/g;q}"     1  wtmp begins Mon Feb 14:26:08     2  192.168.0.1     3  172.12.0.123 4 The last line  
Here the curly braces {} can run multiple commands, separated by, Q is exited
(8) Continuous editing-e
Delete the second row, and the match replaces the last with the new
<pre name= "code" class= "plain" >[[email protected] ~]# nl file.txt | Sed-e "2d"-E "s/last/new/"     1  wtmp begins Mon Feb 14:26:08     3  162.12.0.123 4 this  I s the new Line

(9) Change the file directly, remember not to change the system files
[Email protected] ~]# sed-i "/begin/{s/24/25/g}" file.txt [[email protected] ~]# cat file.txt wtmp begins Mon Feb 25 14: 26:08 2014192.168.0.1162.12.0.123this is the last line


interesting examples of a city
How to replace \ nthe all rows into one line

The first way
[[Email protected] ~]# sed ': A; n;$!ba;s/\n//g ' file.txt wtmp begins Mon Feb-14:26:08 192.168.0.1 162.12.0.123

A different way

[[Email protected] ~]# TR "\ n" "" < File.txt wtmp begins Mon Feb 14:26:08 "192.168.0.1 162.12.0.123 This is the Last Linen


Linux sed Command specific explanation + How to replace line break &quot;\n&quot; (very many interviews asked)

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.