在linux的使用中,有时需要对文件进行分割处理,csplit命令,可以帮助我们灵活的处理分割文件,以下为用法说明:
用法:csplit [选项]... 文件 格式...
Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ...,
and output byte counts of each piece to standard output.如果文件为“-”,则读取标准输入。
必选参数对长短选项同时适用。
-b, --suffix-format=FORMAT use sprintf FORMAT instead of %02d
-f, --prefix=PREFIX use PREFIX instead of 'xx'
-k, --keep-files do not remove output files on errors
--suppress-matched suppress the lines matching PATTERN
-n, --digits=数位 使用指定的进制数位代替二进制
-s, --quiet, --silent 不显示输出文件的尺寸计数
-z, --elide-empty-files 删除空的输出文件
--help 显示此帮助信息并退出
--version 显示版本信息并退出Each PATTERN may be:
INTEGER copy up to but not including specified line number
/REGEXP/[OFFSET] copy up to but not including a matching line
%REGEXP%[OFFSET] skip to, but not including a matching line
{INTEGER} repeat the previous pattern specified number of times
{*} repeat the previous pattern as many times as possibleA line OFFSET is a required '+' or '-' followed by a positive integer.
用法示例:
[genesis@localhost test]$ more data.txt
hhhh
GGGG
HI01
III
HI02
HI01
1223
6666####将文件从第二行进行分割
[genesis@localhost test]$ csplit -f ss data.txt 2
5
34
[genesis@localhost test]$ ls
data.txt ss00 ss01
[genesis@localhost test]$ more ss00 ss01
::::::::::::::
ss00
::::::::::::::
hhhh
::::::::::::::
ss01
::::::::::::::
GGGG
HI01
III
HI02
HI01
1223
6666
##按照匹配模式分割
[genesis@localhost test]$ csplit -f ss data.txt /HI01/+1 /HI02/
15
4
20
[genesis@localhost test]$ ls
data.txt ss00 ss01 ss02
[genesis@localhost test]$ more ss00 ss01 ss02
::::::::::::::
ss00
::::::::::::::
hhhh
GGGG
HI01
::::::::::::::
ss01
::::::::::::::
III
::::::::::::::
ss02
::::::::::::::
HI02
HI01
1223
6666##多模式匹配分割
[genesis@localhost test]$ csplit -f ww data.txt /HI01/+1 /HI01/ /1223/
15
9
5
10
[genesis@localhost test]$ ls
data.txt ww00 ww01 ww02 ww03
[genesis@localhost test]$ more ww00 ww01 ww02 ww03
::::::::::::::
ww00
::::::::::::::
hhhh
GGGG
HI01
::::::::::::::
ww01
::::::::::::::
III
HI02
::::::::::::::
ww02
::::::::::::::
HI01
::::::::::::::
ww03
::::::::::::::
1223
6666##指定分割后文件数字位数
[genesis@localhost test]$ csplit -f ff data.txt -n 3 /HI01/
10
29
[genesis@localhost test]$ ls
data.txt ff000 ff001
[genesis@localhost test]$ more ff000 ff001
::::::::::::::
ff000
::::::::::::::
hhhh
GGGG
::::::::::::::
ff001
::::::::::::::
HI01
III
HI02
HI01
1223
6666##制定分割后文件名及后缀
[genesis@localhost test]$ csplit -f ff data.txt -b %03d.txt %HI01%
29
[genesis@localhost test]$ ls
data.txt ff000.txt
[genesis@localhost test]$ more ff000.txt
HI01
III
HI02
HI01
1223
6666