zhyiwww
用平实的笔,记录编程路上的点点滴滴………
posts - 536,comments - 394,trackbacks - 0

注意参数-n

(1)显示某一行或某些行

显示1,10行
$ sed -n '1,10p' postgres.conf

显示第10行
$ sed -n '10p' postgres.conf

(2)显示某些匹配行
$ sed -n '/This/p' postgres.conf

我的测试:

XYZ@XYZ-desktop:~$ sed -n '1,10 p' postgresql.conf  
# -----------------------------
# PostgreSQL configuration file
# -----------------------------
#
# This file consists of lines of the form:
#
#   name = value
#
# (The "=" is optional.)  Whitespace may be used.  Comments are introduced with
# "#" anywhere on a line.  The complete list of parameter names and allowed
XYZ@XYZ-desktop:~$ sed -n '10p' postgresql.conf 
# "#" anywhere on a line.  The complete list of parameter names and allowed
XYZ@XYZ-desktop:~$ sed -n '/This/p' postgresql.conf 
# This file consists of lines of the form:
# This file is read on server startup and when the server receives a SIGHUP
# This is used when logging to stderr:
XYZ@XYZ-desktop:~$ 


下面的部分来自网上,对于上面的理解会更充分:
Relations between d, p, and !
Sed Range Command Results
--------------------------------------------------------
sed -n 1,10 p Print first 10 lines
sed -n 11,$ !p Print first 10 lines
sed 1,10 !d Print first 10 lines
sed 11,$ d Print first 10 lines
--------------------------------------------------------
sed -n 1,10 !p Print last 10 lines
sed -n 11,$ p Print last 10 lines
sed 1,10 d Print last 10 lines
sed 11,$ !d Print last 10 lines
--------------------------------------------------------
sed -n 1,10 d Nothing printed
sed -n 1,10 !d Nothing printed
sed -n 11,$ d Nothing printed
sed -n 11,$ !d Nothing printed
--------------------------------------------------------
sed 1,10 p Print first 10 lines twice,
Then next 10 lines once
sed 11,$ !p Print first 10 lines twice,
Then last 10 lines once
--------------------------------------------------------
sed 1,10 !p Print first 10 lines once,
Then last 10 lines twice
sed 11,$ p Print first 10 lines once,
then last 10 lines twice

This table shows that the following commands are identical:

sed -n '1,10 p'
sed -n '11,$ !p'
sed '1,10 !d'
sed '11,$ d'





|----------------------------------------------------------------------------------------|
                           版权声明  版权所有 @zhyiwww
            引用请注明来源 http://www.blogjava.net/zhyiwww   
|----------------------------------------------------------------------------------------|
posted on 2009-07-22 11:19 zhyiwww 阅读(12657) 评论(0)  编辑  收藏 所属分类: linux

只有注册用户登录后才能发表评论。


网站导航: