0
点赞
收藏
分享

微信扫一扫

linux中的split操作


前言


Here, the ​​tr​​ command is used to replace ‘\n’ with the ‘+’ character, hence we form the string “1+2+3+…5+”, but at the end of the string we have an extra + operator. In order to nullify the effect of the + operator, 0 is appended.


​echo 'Splitting of files into many smaller pieces becomes essential in certain situations. Earlier,when memory was limited with devices such as floppy disks, it was crucial to split files into smaller file sizes to split files across many disks. However, nowadays we split files for other purposes, such as readability, for generating logs, sending files over e-mail, and so on. In this recipe we will see various ways of splitting files in different chunks.' >> file.txt​

  • ​-b​​ 参数指定分割块的大小
[root@server4 backup]# split -b 10k hadoop.log 
[root@server4 backup]# ll
total 316
-rw-r--r--. 1 root root 15 Aug 15 21:02 args.txt
-rwxr-xr-x. 1 root root 24 Aug 15 20:58 cecho.sh
-rw-r--r--. 1 littlelawson root 27 Aug 15 20:22 example.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 littlelawson root 530 Aug 19 11:28 file.txt
-rw-r--r--. 1 root root 139180 Aug 19 11:33 hadoop.log
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 text.pdf
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xaa
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xab
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xac
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xad
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xae
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xaf
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xag
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xah
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xai
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xaj
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xak
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xal
-rw-r--r--. 1 root root 10240 Aug 19 11:34 xam
-rw-r--r--. 1 root root 6060 Aug 19 11:34 xan

  • -d参数指定是数字前缀【默认是字符前缀】
  • -a参数指定前缀长度
    注:【前缀指的是不带字母x的剩余部分】

[root@server4 backup]# split -b 10k hadoop.log -d -a 4
[root@server4 backup]# ll
total 316
-rw-r--r--. 1 root root 15 Aug 15 21:02 args.txt
-rw-r--r--. 1 root root 139180 Aug 19 11:33 hadoop.log
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0000
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0001
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0002
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0003
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0004
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0005
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0006
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0007
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0008
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0009
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0010
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0011
-rw-r--r--. 1 root root 10240 Aug 19 11:37 x0012
-rw-r--r--. 1 root root 6060 Aug 19 11:37 x0013

The previous split files have a filename prefix x. We can also use our own filename prefix by providing a prefix filename.The last command argument for the split command is prefix.It is in the format:

split [command_args] prefix

[root@server4 backup]# split -b 10k hadoop.log -d -a 4 test
[root@server4 backup]# ll
total 316
-rw-r--r--. 1 root root 15 Aug 15 21:02 args.txt
-rwxr-xr-x. 1 root root 24 Aug 15 20:58 cecho.sh
-rw-r--r--. 1 littlelawson root 27 Aug 15 20:22 example.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 littlelawson root 530 Aug 19 11:28 file.txt
-rw-r--r--. 1 root root 139180 Aug 19 11:33 hadoop.log
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0000
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0001
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0002
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0003
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0004
...
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0011
-rw-r--r--. 1 root root 10240 Aug 19 11:42 test0012
-rw-r--r--. 1 root root 6060 Aug 19 11:42 test0013
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 text.pdf
  • ​-l​​参数指定每个分解块的行数
[root@server4 backup]# split -l 40 hadoop.log
[root@server4 backup]# ll
total 316
-rw-r--r--. 1 root root 15 Aug 15 21:02 args.txt
-rwxr-xr-x. 1 root root 24 Aug 15 20:58 cecho.sh
-rw-r--r--. 1 littlelawson root 27 Aug 15 20:22 example.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 littlelawson root 530 Aug 19 11:28 file.txt
-rw-r--r--. 1 root root 139180 Aug 19 11:33 hadoop.log
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 text.pdf
-rw-r--r--. 1 root root 17021 Aug 19 11:45 xaa
-rw-r--r--. 1 root root 5297 Aug 19 11:45 xab
-rw-r--r--. 1 root root 7073 Aug 19 11:45 xac
-rw-r--r--. 1 root root 6952 Aug 19 11:45 xad
-rw-r--r--. 1 root root 6975 Aug 19 11:45 xae
-rw-r--r--. 1 root root 7042 Aug 19 11:45 xaf
-rw-r--r--. 1 root root 6952 Aug 19 11:45 xag
-rw-r--r--. 1 root root 6976 Aug 19 11:45 xah
-rw-r--r--. 1 root root 7042 Aug 19 11:45 xai
-rw-r--r--. 1 root root 6952 Aug 19 11:45 xaj
-rw-r--r--. 1 root root 6976 Aug 19 11:45 xak
-rw-r--r--. 1 root root 7041 Aug 19 11:45 xal
-rw-r--r--. 1 root root 6952 Aug 19 11:45 xam
-rw-r--r--. 1 root root 6976 Aug 19 11:45 xan
-rw-r--r--. 1 root root 7042 Aug 19 11:45 xao
-rw-r--r--. 1 root root 18688 Aug 19 11:45 xap
-rw-r--r--. 1 root root 4397 Aug 19 11:45 xaq
-rw-r--r--. 1 root root 2826 Aug 19 11:45 xar

如何根据文件内容(稍有规律)来分割成多个子文件?

cat server.log
SERVER-1
[connection] 192.168.0.1 success
[connection] 192.168.0.2 failed
[disconnect] 192.168.0.3 pending
[connection] 192.168.0.4 success
SERVER-2
[connection] 192.168.0.1 failed
[connection] 192.168.0.2 failed
[disconnect] 192.168.0.3 success
[connection] 192.168.0.4 failed
SERVER-3
[connection] 192.168.0.1 pending
[connection] 192.168.0.2 pending
[disconnect] 192.168.0.3 pending
[connection] 192.168.0.4 failed
[root@server4 backup]# csplit server.log /SERVER/ -n 2 -s {*} -f server -b "%02d.log" 
[root@server4 backup]# ll
total 32
-rw-r--r--. 1 root root 15 Aug 15 21:02 args.txt
-rwxr-xr-x. 1 root root 24 Aug 15 20:58 cecho.sh
-rw-r--r--. 1 littlelawson root 27 Aug 15 20:22 example.txt
-rw-r--r--. 1 littlelawson root 530 Aug 19 11:28 file.txt
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 root root 0 Aug 19 11:57 server00.log
-rw-r--r--. 1 root root 140 Aug 19 11:57 server01.log
-rw-r--r--. 1 root root 138 Aug 19 11:57 server02.log
-rw-r--r--. 1 root root 140 Aug 19 11:57 server03.log
-rw-r--r--. 1 root root 418 Aug 19 11:56 server.log
[root@server4 backup]# cat server00.log
[root@server4 backup]# cat server01.log
SERVER-1
[connection] 192.168.0.1 success
[connection] 192.168.0.2 failed
[disconnect] 192.168.0.3 pending
[connection] 192.168.0.4 success
[root@server4 backup]# cat server03.log
SERVER-3
[connection] 192.168.0.1 pending
[connection] 192.168.0.2 pending
[disconnect] 192.168.0.3 pending
[connection] 192.168.0.4 failed
[root@server4 backup]# cat server02.log
SERVER-2
[connection] 192.168.0.1 failed
[connection] 192.168.0.2 failed
[disconnect] 192.168.0.3 success
[connection] 192.168.0.4 failed
  • ​-s​​:执行命令的时候不输出任何信息(slient)

如果去掉-s参数,执行过程如下:

[root@server4 backup]# csplit server.log /SERVER/ -n 2 {*} -f server -b "%02d.log" 
0
140
138
140
[root@server4 backup]# ll
total 32
-rw-r--r--. 1 root root 15 Aug 15 21:02 args.txt
-rwxr-xr-x. 1 root root 24 Aug 15 20:58 cecho.sh
-rw-r--r--. 1 littlelawson root 27 Aug 15 20:22 example.txt
-rw-r--r--. 1 littlelawson root 530 Aug 19 11:28 file.txt
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 root root 0 Aug 19 11:58 server00.log
-rw-r--r--. 1 root root 140 Aug 19 11:58 server01.log
-rw-r--r--. 1 root root 138 Aug 19 11:58 server02.log
-rw-r--r--. 1 root root 140 Aug 19 11:58 server03.log
-rw-r--r--. 1 root root 418 Aug 19 11:56 server.log

{*}:指定重复切分,直到能够符合到文件末尾的标准。

{integer}:指定继续匹配的次数

Slicing filenames based on extension.[根据文件扩展名切分文件]

How to do it!

How it works!

下面这个没有输出后缀名,而是直接输出了文件名。

[root@server4 backup]# file_jpg="sample.jpg"
[root@server4 backup]# name=${file_jpg%.*}
[root@server4 backup]# echo $name
sample
[root@server4 backup]# extension=${file_jpg#*.}
[root@server4 backup]# echo $extension
jpg

1.使用%字符提取文件名

${var%.*}


  • 按照从右往左的顺序使用通配符匹配,直到小数点。然后将其从整个字符串中移除。
  • 第一次匹配时就停止搜索,不再匹配【nongreedy】
    ${var%%.*}
  • 一直匹配到不能再匹配【greedy】

[root@server4 backup]# file_jpg="hack.fun.book.txt"
[root@server4 backup]# echo $name
sample
[root@server4 backup]# name=${file_jpg%.*}
[root@server4 backup]# echo $name
hack.fun.book
[root@server4 backup]# name=${file_jpg%%.*}
[root@server4 backup]# echo $name
hack
[root@server4 backup]#

2.使用​​${var#*.}​

从左至右的顺序,直到匹配到.结束。

再看${var#.}和${var##.}的区别【同${var%.}和${var%%.}的区别】

[root@server4 backup]# name=${file_jpg#*.}
[root@server4 backup]# echo $name
fun.book.txt
[root@server4 backup]# name=${file_jpg##*.}
[root@server4 backup]# echo $name
txt
[root@server4 backup]#



举报

相关推荐

0 条评论