Linux 字符处理
管道:|
[root@localhost ~]# ls -l /etc | more
搜索文本:grep
-i
不区分大小写-v
反向匹配-c
统计包含匹配的行数-n
输出行号
现在有这样一个文件
[root@localhost home]# cat tj.txt
The cat's name is Tom, what's the mouse's name?
The mouse's NAME is Jerry.
They are good friends.
找出含有 name 的行
[root@localhost home]# grep 'name' tj.txt
The cat's name is Tom, what's the mouse's name?
输出行号
[root@localhost home]# grep -n 'name' tj.txt
1:The cat's name is Tom, what's the mouse's name?
不区分大小写
[root@localhost home]# grep -i 'name' tj.txt
The cat's name is Tom, what's the mouse's name?
The mouse's NAME is Jerry.
统计行数
[root@localhost home]# grep -c 'name' tj.txt
1
[root@localhost home]# grep -ci 'name' tj.txt
2
反向匹配
[root@localhost home]# grep -v 'name' tj.txt
The mouse's NAME is Jerry.
They are good friends.
[root@localhost home]# grep -vi 'name' tj.txt
They are good friends.
使用管道符
[root@localhost home]# cat tj.txt | grep -vi name
They are good friends.
排序:sort
-n
采取数字排序-t
指定分隔符-k
指定第几列-r
反向排序
现在有这样一个文件
[root@localhost home]# cat sort.txt
b:3
c:2
a:4
e:5
d:1
f:11
对输出内容进行排序
[root@localhost home]# cat sort.txt | sort
a:4
b:3
c:2
d:1
e:5
f:11
反向排序
[root@localhost home]# cat sort.txt | sort -r
f:11
e:5
d:1
c:2
b:3
a:4
使用 :
将其分割,然后用后面的数字排序
[root@localhost home]# cat sort.txt | sort -t ":" -k 2
d:1
f:11
c:2
b:3
a:4
e:5
发现 11 在第二个,因为默认是当做字符串处理
[root@localhost home]# cat sort.txt | sort -t ":" -k 2 -n
d:1
c:2
b:3
a:4
e:5
f:11
删除重复内容:uniq
-i
不区分大小写-c
计算重复行数
现在有这样一个文件
[root@localhost home]# cat uniq.txt
abc
123
abc
123
[root@localhost home]# cat uniq.txt | uniq
abc
123
abc
123
[root@localhost home]# cat uniq.txt | sort
123
123
abc
abc
[root@localhost home]# cat uniq.txt | sort | uniq
123
abc
[root@localhost home]# cat uniq.txt | sort | uniq -c
2 123
2 abc
可以看出,如果重复行不连续,uniq 则不具备删除效果,即 uniq 会输出若干连续相同行的第一行
截取文本:cut
cut -f 指定列 -d 分隔符
cut -c 指定列的字符
[root@localhost home]# cat /etc/passwd | cut -f 1,6 -d :
root:/root
......(略去部分内容)......
onlychina:/home/onlychina
onlychina1:/home/onlychina1
onlychina5555:/home/onlychina5555
[root@localhost home]# cat /etc/passwd | cut -c 1-5,7-10
root::0:0
......(略去部分内容)......
onlycina:
onlycina1
onlycina5
文本转换:tr
小写转大写
[root@localhost home]# cat /etc/passwd | tr [a-z] [A-Z]
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
......(略去部分内容)......
ONLYCHINA:X:1000:1000::/HOME/ONLYCHINA:/BIN/BASH
ONLYCHINA1:X:1001:1000::/HOME/ONLYCHINA1:/BIN/BASH
ONLYCHINA5555:X:5555:5555::/HOME/ONLYCHINA5555:/BIN/BASH
删除内容
[root@localhost home]# cat /etc/passwd | tr -d :
rootx00root/root/bin/bash
......(略去部分内容)......
onlychinax10001000/home/onlychina/bin/bash
onlychina1x10011000/home/onlychina1/bin/bash
onlychina5555x55555555/home/onlychina5555/bin/bash
文本合并:paste
按行进行合并
[root@localhost home]# cat a.txt
1
2
3
[root@localhost home]# cat b.txt
a
b
c
[root@localhost home]# paste a.txt b.txt
1 a
2 b
3 c
[root@localhost home]# paste -d : a.txt b.txt
1:a
2:b
3:c
分割文件:split
split -l 500 big_file.txt small_file_
按照文件行数,将大文件分割成多个小文件
split -b 64m big_bin small_bin
按照文件大小,将大文件分割成 64MB 多个小文件