Latest web development tutorials

Linux sed command

Linux command Daquan Linux command Daquan

Linux sed command is to use the script to process text files.

sed script can be in accordance with the instructions to handle, edit text files.

Sed is mainly used to automatically edit one or more files; simplify repeated operation of the document; written conversion procedures.

grammar

sed [-hnV][-e<script>][-f<script文件>][文本文件]

Parameter Description:

  • -e <script> or --expression = <script> specified in the script with the option to process the input text file.
  • -f <script file> or --file = <script file> option to specify the script file to process the input text file.
  • -h or --help displays help.
  • -n or --quiet or --silent script shows the results after treatment only.
  • -V Or --version display version information.

Action Description:

  • a: new, can take back a string, and those strings will appear (currently the next line) on a new line -
  • c: replace, rear c can then string these strings can replace n1, n2 line between!
  • d: delete, delete because it is ah, so usually not accept any later d pound;
  • i: insert, i can take back the string, and those strings will be (present on one line) on a new line;
  • p: print, that will be a selection of print data. P will usually run with the parameter sed -n ~
  • s: substitution can be substituted work directly miles! Usually this s action can be used with regular expressions! For example 1,20s / old / new / g is it!

Examples

After the fourth line testfile file to add a line, and outputs the results to standard output, enter the following command at the command prompt:

sed -e 4a\newLine testfile 

First check testfile contents are as follows:

$ cat testfile #查看testfile 中的内容  
HELLO LINUX!  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test 

After using sed command, the output results are as follows:

$ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串  
HELLO LINUX! #testfile文件原有的内容  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test  
newline 

To add behavior units / Remove

The contents of / etc / passwd lists and print line numbers, while the first set 2-5 rows deleted!

[root@www ~]# nl /etc/passwd | sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....(后面省略).....

sed action as '2,5d', that is to remove the d! Because the lines 2-5 to remove him, so no data is displayed on the 2-5 line Lo ~ Also, take note, is supposed to be released sed -e fishes, no -e will do it! Also note that the back sed take action, make sure to '' two single quotes around Oh!

Just delete the second row

nl /etc/passwd | sed '2d' 

To remove the third to the last line

nl /etc/passwd | sed '3,$d' 

After the second row (that is, added to the third row) plus "drink tea? 'Words!

[root@www ~]# nl /etc/passwd | sed '2a drink tea'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
drink tea
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....

But if it is to be in front of the second row

nl /etc/passwd | sed '2i drink tea' 

If you want to increase by more than two rows behind the second row to join two lines, such as "Drink tea or ....." and "drink beer?"

[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ......
drink beer ?
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....

Must be a backslash "\" to add a new row between each row of Oh! So, the above example, we can find the first row in the final surface, there \ exist.

And to replace the units of display

The contents of 2-5 row substitution to "No 2-5 number" mean?

[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
.....(后面省略).....

Through this method we are able to replace the entire row of data!

The first line lists only 5-7 / etc / passwd file within

[root@www ~]# nl /etc/passwd | sed -n '5,7p'
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
</p><p>
可以透过这个 sed 的以行为单位的显示功能, 就能够将某一个文件内的某些行号选择出来显示。
</p>
<h3>数据的搜寻并显示</h3>
<p>搜索 /etc/passwd有root关键字的行</p>
<pre>
nl /etc/passwd | sed '/root/p'
1  root:x:0:0:root:/root:/bin/bash
1  root:x:0:0:root:/root:/bin/bash
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3  bin:x:2:2:bin:/bin:/bin/sh
4  sys:x:3:3:sys:/dev:/bin/sh
5  sync:x:4:65534:sync:/bin:/bin/sync
....下面忽略 

If the root is found, in addition to all output lines, the output will match the line.

When using -n will print all lines that template.

nl /etc/passwd | sed -n '/root/p'
1  root:x:0:0:root:/root:/bin/bash

Search and delete data

Deleted / etc / passwd line contains the root of all other line output

nl /etc/passwd | sed  '/root/d'
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3  bin:x:2:2:bin:/bin:/bin/sh
....下面忽略
#第一行的匹配root已经删除了

Search data and execute commands

Find a matching pattern eastern line, the search / etc / passwd, find the root of the corresponding row, behind the implementation of the braces in a set of commands, separated by semicolons between each command, here to bash replace blueshell, re-export it Row:

nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p}'
1  root:x:0:0:root:/root:/bin/blueshell

Q is the last exit.

Search and replace data

In addition to the entire line processing mode, sed can also use some of the data in units of search and replace. Vi quite substantially similar to sed search and replaced! He is a bit like this:

sed 's/要被取代的字串/新的字串/g'

The first observation of the original information, use / sbin / ifconfig query IP

[root@www ~]# /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
.....(以下省略).....

ip This machine is 192.168.1.100.

The front part of the IP to be deleted

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

Next it is to delete part of the follow-up, namely: 192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.255.0

The part of the IP to be deleted later

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.1.100

Multi-Edit

A sed command to delete the / etc / passwd third line to the end of the data, and the bash replace blueshell

nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
1  root:x:0:0:root:/root:/bin/blueshell
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh

-e represents multi-point editing, the first edit command deletes / etc / passwd to the data at the end of the third line, the second command searches bash replaced blueshell.

Directly modify the contents of the file (dangerous movements)

sed can directly modify the contents of the file, do not use the pipe command or data streams redirect! However, since this action will be to directly modify the original file, so you do not just take the system configuration to test! We still use the downloaded file to test regular_express.txt see it!

Use sed to each end of the line if it is within regular_express.txt. Is replaced!

[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt

Use sed directly into the last row in regular_express.txt "# This is a test."

[root@www ~]# sed -i '$a # This is a test' regular_express.txt

Since the $ represents the last line, and a new action is, so the final document to add "# This is a test!"

sed the "-i" option to directly modify the contents of the file, which is very helpful! For example, if you have a million lines of files you want to add some text line 100, this time using vim may be mad! Because the file is too big! How to do that? Sed on the use ah! Through sed directly modify / replace function, you do not even use vim to amend!

Linux command Daquan Linux command Daquan