Linux sed command details + How to replace the line break "\ n" (many interviews asked) __linux

Source: Internet
Author: User
Tags first row
Sed
SED is a powerful text-processing tool
You can use a regular match to insert the text to delete changes, and so on
When SED is processed, one line is processed at a time, each time the current processing is stored in a temporary buffer, the output buffer content is processed to the screen, then the next line is read into the buffer, so repeats until the end.


1. command format and Parameters
sed [-NEFR] [action] File
Parameters:
-N Quiet mode, at the time of sed processing, all data from stdin will be output to the terminal, plus-n will only output the line to be processed
-e Direct SED action edit on command column
-F writes the action of SED directly in the file
The-R SED action supports extended regular expressions (default is just the underlying regular)
-I directly modify the contents of the file (use caution, especially when doing exercises with system files)


Action:
A append: increase, increase on the next line in the current row
C: Replace the line between N1 and N2
D Delete: deleting
I insert, the current line is inserted on the previous line
P print, often with-n use
s substitution, s/old/new/g


2, the basic use of detailed
(1) Add a row after the first line

[root@localhost ~]# nl file.txt | SED "1a Add text"
     1  wtmp begins Mon Feb 14:26:08 2014
Add text
     2  192.168.0.1
     3  162.12.0.123 4 This are the last line  
(2) Add a row before the first row
[root@localhost ~]# nl file.txt | Sed "1i Add text"
add text
     1  wtmp begins Mon Feb 14:26:08 2014
     2  192.168.0.1
     3  162.12.0.123 4 This are the last line  
(3) Delete lines 2nd, 3
[root@localhost ~]# nl file.txt | Sed "2,3d"
     1  wtmp begins Mon Feb 14:26:08 2014
     4 This  are the last line
(4) print 2nd, 3 lines
[Root@localhost ~]# sed-n "2,3p" file.txt 
192.168.0.1

The point here is to use-n as much as possible, otherwise this will happen.
[Root@localhost ~]# sed "2,3p" file.txt 
wtmp begins Mon Feb
-14:26:08 2014 192.168.0.1 192.168.0.1 162.12.0.123 162.12.0.123 This are the last line

(5) Change 168 to 169
Look at the source file first
[Root@localhost ~]# cat file.txt 
wtmp begins Mon Feb-14:26:08 2014 192.168.0.1 162.12.0.123 this
is the
After processing
[Root@localhost ~]# sed "s/168/169/g" file.txt 
wtmp begins Mon Feb
14:26:08 2014 162.12.0.123 this are the last line

(6) Inserting multiple lines
[root@localhost ~]# nl file.txt | Sed "2afirst\nsecond" file.txt 
wtmp begins Mon Feb-14:26:08 2014 192.168.0.1-A-
second
162.12.0.123 this are the last line

(7) match the data and then do the operation
You only need to add a regular match on the above basis
Sed "/matching pattern/handling mode" file.txt
Sed "/^root/d" file.txt to start with root deletion
For example
Matches the begin and deletes the row
[root@localhost ~]# nl file.txt | Sed "/begin/d"
     2  192.168.0.1
     3  162.12.0.123
     4  This are the last line
Match 123 and replace the 123 row 162 with 172
[root@localhost ~]# nl file.txt | Sed "/123/{s/162/172/g;q}"
     1  wtmp begins Mon Feb 14:26:08 2014
     2  192.168.0.1
     3  172.12.0.123 4 This are the last line  
Here the braces {} can execute multiple commands, separated by, Q is exit
(8) Continuous edit-E
Deletes the second row and replaces last with new
<pre name= "code" class= "plain" >[root@localhost ~]# nl file.txt | Sed-e "2d"-E "s/last/new/"
     1  wtmp begins Mon Feb-14:26:08 2014
     3  162.12.0.123 4  This is the N EW Line

(9) directly modify the file, remember not to modify system files
[Root@localhost ~]# sed-i "/begin/{s/24/25/g}" file.txt 
[root@localhost ~]# cat file.txt 
wtmp begins Mon 25 Feb 14:26:08 2014
192.168.0.1
162.12.0.123 This are the last line


三、一个 A more interesting example
How to replace \ n is to classify all rows as one line

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

The second way

[root@localhost ~]# tr "\ n" "" < File.txt wtmp begins Mon Feb 14:26:08-2014 192.168.0.1 162.12.0.123 this is 
th E Last Linen


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.