Monday, April 21, 2014

删除匹配行

sed '/test/'d example-----删除example文件所有包含test的行
 sed s/[[:space:]]//g  filename   删除所有空格

Wednesday, April 9, 2014

创建任意大小的文件And分割任意大小的文件(转)

[Shell学习笔记] 创建任意大小的文件And分割任意大小的文件

创建任意大小的文件

创建特定大小的文件最简单的方法就是利用dd命令,dd命令会克隆给定的内容,然后将一摸一样的副本写入到输出,stdin、设备文件、普通文件等都可以作为输入,stdout、设备文件、普通文件也都可以作为输出。

dd命令选项

  • bs=字节数   将ibs(输入)与obs(输出)设成指定的字节数
  • cbs=字节数   转换时,每次只转换指定的字节数
  • conv=关键字   指定文件转换的方式
  • count=区块数   仅读取指定的区块数
  • ibs=字节数   每次读取的字节数
  • obs=字节数   每次输出的字节数
  • if=文件   输入的文件
  • of=文件   输出到文件
  • seek=区块数   一开始输出时,跳过指定的区块数
  • skip=区块数   一开始读取时,跳过指定的区块数

dd命令示例

[root@localhost text]# dd if=/dev/zero of=sun.txt bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.006107 seconds, 172 MB/s

[root@localhost text]# du -sh sun.txt 
1.1M    sun.txt
该命令创建了一个1M大小的文件sun.txt,其中参数解释:
  • if 代表输入文件。如果不指定if,默认就会从stdin中读取输入。
  • of 代表输出文件。如果不指定of,默认就会将stdout作为默认输出。
  • bs 代表字节为单位的块大小。
  • count 代表被复制的块数。
  • /dev/zero 是一个字符设备,会不断返回0值字节(\0)。
块大小可以使用的计量单位表
单元大小代码
字节(1B)c
字节(2B)w
块(512B)b
千字节(1024B)k
兆字节(1024KB)M
吉字节(1024MB)G
以上命令可以看出dd命令来测试内存操作速度:
1048576 bytes (1.0 MB) copied, 0.006107 seconds, 172 MB/s

分割任意大小的文件

有时需要将文件分割成更小的片段,比如为提高可读性,生成日志等。

split命令

split命令可以将一个大文件分割成很多个小文件,常用命令选项:
  • -b  值为每一输出档案的大小,单位为 byte。
  • -C  每一输出档中,单行的最大 byte 数。
  • -d  使用数字作为后缀。
  • -l  值为每一输出档的列数大小。
示例
生成一个大小为100KB的测试文件:
[root@localhost split]# dd if=/dev/zero bs=100k count=1 of=date.file
1+0 records in
1+0 records out
102400 bytes (102 kB) copied, 0.00043 seconds, 238 MB/s
使用split命令将上面创建的date.file文件分割成大小为10KB的小文件:
[root@localhost split]# split -b 10k date.file 
[root@localhost split]# ls
date.file  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj
文件被分割成多个带有字母的后缀文件,如果想用数字后缀可使用-d参数,同时可以使用-a length来指定后缀的长度:
[root@localhost split]# split -b 10k date.file -d -a 3
[root@localhost split]# ls
date.file  x000  x001  x002  x003  x004  x005  x006  x007  x008  x009
为分割后的文件指定文件名的前缀:
[root@localhost split]# split -b 10k date.file -d -a 3 split_file
[root@localhost split]# ls
date.file  split_file000  split_file001  split_file002  split_file003  split_file004  split_file005  split_file006  split_file007  split_file008  split_file009
使用-l选项根据文件的行数来分割文件,例如把文件分割成每个包含10行的小文件:
split -l 10 date.file

csplit命令

csplit命令是split的一个变体,split只能够根据文件大小或行数来分割,但csplit能够根据文件本身特点来分割文件。
示例测试文件 server.log
cat server.log
SERVER-1
[con] 10.10.10.1 suc
[con] 10.10.10.2 fai
[dis] 10.10.10.3 pen
[con] 10.10.10.4 suc
SERVER-2
[con] 10.10.10.5 suc
[con] 10.10.10.6 fai
[dis] 10.10.10.7 pen
[con] 10.10.10.8 suc
SERVER-3
[con] 10.10.10.9 suc
[con] 10.10.10.10 fai
[dis] 10.10.10.11 pen
[con] 10.10.10.12 suc
需要将server.log分割成server1.log、server2.log、server3.log,这些文件的内容分别取自原文件中不同的SERVER部分:
[root@localhost split]# csplit server.log /SERVER/ -n2 -s {*} -f server -b "%02d.log"; rm server00.log
[root@localhost split]# ls
server01.log  server02.log  server03.log  server.log
命令详细说明:
  • /[正则表达式]/  匹配文本样式,比如/SERVER/,从第一行到包含SERVER的匹配行。
  • {*}  表示根据匹配重复执行分割,直到文件尾停止,使用{整数}的形式指定分割执行的次数。
  • -s  静默模式,不打印其他信息。
  • -n  指定分割后的文件名后缀的数字个数。比如01、02、03等。
  • -f  指定分割后的文件名前缀。
  • -b  指定后缀格式。比如%02d.log,类似于C语言中的printf参数格式。
  • rm server00.log  是删除第一个文件,因为分割后的的第一个文件没有内容,匹配的单词就位于文件的第一行中。

Thursday, April 3, 2014

grep 多次匹配

grep 同时满足多个关键字和满足任意关键字
① grep -E "word1|word2|word3"   file.txt
   满足任意条件(word1、word2和word3之一)将匹配。
② grep word1 file.txt | grep word2 |grep word3
   必须同时满足三个条件(word1、word2和word3)才匹配。

Wednesday, April 2, 2014

打印匹配行,以及显示匹配行下面的的几行(转)

grep -A 5 pattern yourfile   显示匹配pattern的行以及后面五行。


-A 后面跟着你想要显示几行,

grep手册中的解释:
Context Line Control
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.

-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
Places a line containing a group separator (--) between
contiguous groups of matches. With the -o or --only-matching
option, this has no effect and a warning is given.

-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a
group separator (--) between contiguous groups of matches. With
the -o or --only-matching option, this has no effect and a
warning is given.


 
简单翻译就是,-A -B -C 后面都跟阿拉伯数字,-A是显示匹配后和它后面的n行。-B是显示匹配行和它前面的n行。-C是匹配行和它前后各n行。总体来说,-C覆盖面最大。用它保险些。

Tuesday, April 1, 2014