Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063584
  • 博文数量: 264
  • 博客积分: 7225
  • 博客等级: 少将
  • 技术积分: 5096
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-17 08:53
文章分类

全部博文(264)

文章存档

2011年(33)

2010年(52)

2009年(152)

2008年(27)

我的朋友

分类: LINUX

2011-06-21 22:00:00

Linux下文本处理命令的使用 2010-08-27 12:02:21
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一、查看文件的部分截取

1head:显示文件的开头几行,默认显示前10行;

         head  [n  行数] 文件名

---------------------------------------------------------------------------------

[root@localhost ~]# head -n 3 /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

---------------------------------------------------------------------------------

2tail:显示文件的最后几行,默认显示后10行;

         tail  [选项]  文件名

         -n:确定显示的行数; tail  [n  行数] 文件名

         -f:可以一直不断的查看某个文件的更新; tail  -f  文件名  通常用来查看系统日志;调试服务、make程序时使用;直到按Ctrl-c为止。

---------------------------------------------------------------------------------

[root@localhost ~]# tail -f /var/log/messages

Aug 23 11:27:55 localhost syslogd 1.4.1: restart.

Aug 23 12:46:49 localhost NET[7159]: /sbin/dhclient-script : updated /etc/resolv.conf

…………后面会根据系统的情况持续更新显示,直到按Ctrl - c

---------------------------------------------------------------------------------

 

思考:查看/etc/passwd5-10行的如何打命令呢?

 

三、抽取文本命令:

1、正则表达式:

        [0-9]  [a-z]  [A-Z] 表示一个集合;

        [abc]:匹配列表里的任何一个字符

        [^abc]:匹配列表以外的字符

        ^abc:匹配以abc开头

        abc$:匹配以abc结尾的

 

2grep:显示文件或标准输入中匹配的文本内容

Ø  下面我们看一下grep和正规表达式一起使用的案例:

         1[abc]:

---------------------------------------------------------------------------------

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  install.log  install.log.syslog

[root@localhost ~]# ls |grep '[ai]n'

anaconda-ks.cfg

install.log

install.log.syslog

---------------------------------------------------------------------------------

         2 [^abc]

---------------------------------------------------------------------------------

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  install.log  install.log.syslog  test

[root@localhost ~]# ls |grep '[^i]n'

anaconda-ks.cfg

---------------------------------------------------------------------------------

         3^abc

---------------------------------------------------------------------------------

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  install.log  install.log.syslog  test

[root@localhost ~]# ls |grep '^in'

install.log

install.log.syslog

---------------------------------------------------------------------------------

         4abc$

---------------------------------------------------------------------------------

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  install.log  install.log.syslog  test

[root@localhost ~]# ls|grep 'log$'

install.log

install.log.syslog

---------------------------------------------------------------------------------

Ø  grep命令选项:

-i :搜索匹配的关键词时忽略大小写;

-n :显示匹配的行的行号;

-v :过滤掉匹配关键字的行,显示不匹配的;

---------------------------------------------------------------------------------

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  install.log  install.log.syslog

[root@localhost ~]# ls |grep -v ^i

anaconda-ks.cfg

Desktop

---------------------------------------------------------------------------------

3cut:显示文件或者标准输入数据的指定的列

         cut d区分分割的定界符 f 要显示的列的编码 文件名

         -d:指定区分的定界符,默认为TAB

         -f:指定要显示的列的编码

---------------------------------------------------------------------------------

[root@server ~]# cut -d: -f1 /etc/passwd

root

Bin

daemon

……下面省略

---------------------------------------------------------------------------------

三、文本分析处理工具:

1wc文本统计:

         wc  [选项]  目标文件

---------------------------------------------------------------------------------

[root@server ~]# wc  /etc/passwd

  35          54              1589       /etc/passwd

行数    单次总数   字节总数

---------------------------------------------------------------------------------

         -l:只统计行数

         -w:只统计单次总数

         -c:只统计字节数

         -m:只统计字符总数,包含不显示的;

2diff:比较文件:

diff  文件1  文件2 

---------------------------------------------------------------------------------

[root@server ~]# diff install.log install.log1

9c9

< 安装 nash-5.1.19.6-54.i386

---

> nash-5.1.19.6-54.i386

---------------------------------------------------------------------------------

diff u 文件1  文件2 >补丁文件名   比较文件,然后把不同写到补丁文件中

---------------------------------------------------------------------------------

[root@localhost ~]# cat test test1

this is a test

where are yourhoume?

this is a exam

where are yourtown?

[root@localhost ~]# diff -u test test1 >test.patch

[root@localhost ~]# cat test.patch

--- test        2010-08-26 15:17:31.000000000 +0800

+++ test1       2010-08-26 15:17:56.000000000 +0800

@@ -1,2 +1,2 @@

-this is a test

-where are yourhoume?

+this is a exam

+where are yourtown?

[root@localhost ~]# ls

anaconda-ks.cfg  Desktop  install.log  test  test1  test.patch

---------------------------------------------------------------------------------

3patch:应用文件在其他文件中的改变

    patch [-b] 目标文件名 .patch的比较文件

    .patch的文件:由diff命令比较创建

-b:备份目标文件;

---------------------------------------------------------------------------------

[root@localhost ~]# cat test test1

this is a test

where are yourhoume?

this is a exam

where are yourtown?

[root@localhost ~]# patch -b ./test test.patch

patching file ./test

[root@localhost ~]# cat test

this is a exam

where are yourtown?

---------------------------------------------------------------------------------

4sort:整理文本命令:

          sort  [选项]  文件

          -r :执行反方向整理(有上之下)

---------------------------------------------------------------------------------

[root@server ~]# grep bash  /etc/passwd|sort

op:x:501:501::/home/op:/bin/bash

redhat:x:500:500::/home/redhat:/bin/bash

root:x:0:0:root:/root:/bin/bash

[root@server ~]# grep bash  /etc/passwd|sort -r

root:x:0:0:root:/root:/bin/bash

redhat:x:500:500::/home/redhat:/bin/bash

op:x:501:501::/home/op:/bin/bash

---------------------------------------------------------------------------------

          -n:按照数字大小整理

          -u:删除输出中的重复行;

          -t 符号:使用符号作为字段的定界符;

          -k 列数:按照使用的定界符分割的字段的第 列数 来整理;

---------------------------------------------------------------------------------

[root@server ~]# sort -t : -k 3 -r /etc/passwd

nobody:x:99:99:Nobody:/:/sbin/nologin

news:x:9:13:news:/etc/news:

sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

 

……后面省略

---------------------------------------------------------------------------------

5tr:把某个集合内的字符换成另外一个集合中的相应的字符

 tr [a-z] [A-Z] <目标文件 >新文件名

目标文件里的小写字母替换成大写然后不存成新文件

---------------------------------------------------------------------------------

[root@localhost ~]# tr '[a-z]' '[A-Z]' an.bak

[root@localhost ~]# cat an.bak

# THE FOLLOWING IS THE PARTITION INFORMATION YOU REQUESTED

# NOTE THAT ANY PARTITIONS YOU DELETED ARE NOT EXPRESSED

# HERE SO UNLESS YOU CLEAR ALL PARTITIONS FIRST, THIS IS

# NOT GUARANTEED TO WORK

阅读(613) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~