shell脚本基础工具(sort,uniq,paste,sed)

shell工具


  • 01.sort工具


1. sort默认对文本进行升序排序
2. sort -r 对文本进行降序排序
3. sort -n 按照数值进行排序
4. sort -t 指定按照什么作为分割符 ,-k 制定按照第几列排序

#1.txt文件
[skin@bogon shell]$ cat 1.txt 
111 bbb
333 ccc 
222 ddd
444 eee
555 aaa


#测试sort
[skin@bogon shell]$ sort 1.txt 
111 bbb
222 ddd
333 ccc 
444 eee
555 aaa


#测试sort -r
[skin@bogon shell]$ sort -r 1.txt 
555 aaa
444 eee
333 ccc 
222 ddd
111 bbb


#测试sort -t -k
[skin@bogon shell]$ sort -r 1.txt 
555 aaa
444 eee
333 ccc 
222 ddd
111 bbb
可以看到按照第二列字母进行排序

其他参数
这里写图片描述

  • 02.uniq工具


a. 删除文件中重复行
b. uniq -c 在文本前面加上出现的次数
c. uniq -d 只显示重复行
d. uniq -u 只显示不重复的行

#1.txt文件
[skin@bogon shell]$ cat 1.txt 
aaa
bbb
ccc
aaa
ddd
ddd
eee
eee
eee
eee


#测试uniq -c
[skin@bogon shell]$ cat 1.txt  | uniq -c
      1 aaa
      1 bbb
      1 ccc
      1 aaa
      2 ddd
      4 eee

#测试uniq -d 
[skin@bogon shell]$ cat 1.txt  | uniq -d
ddd
eee

#测试uniq -u 
[skin@bogon shell]$ cat 1.txt  | uniq -u
aaa
bbb
ccc
aaa

#求两个文件的交集,并集,补集

[skin@bogon shell]$ cat 1.txt 
aaa
bbb
ccc
ddd
eee
[skin@bogon shell]$ cat 2.txt 
aaa
bbb
eee

#交集
[skin@bogon shell]$ cat 1.txt  2.txt | sort | uniq -d
aaa
bbb
eee
#并集
[skin@bogon shell]$ cat 1.txt  2.txt | sort | uniq 
aaa
bbb
ccc
ddd
eee
#补集
[skin@bogon shell]$ cat 1.txt  2.txt | sort | uniq -u
ccc
ddd


其他选项
这里写图片描述

  • 03.paste工具


1.paste用来合并文件各行
2.paste -d 指定分隔符

#两个文件
[skin@bogon shell]$ cat 1.txt 
hello
hello
hello
hello
hello
[skin@bogon shell]$ cat 2.txt 
aaa
bbb
ccc
ddd
eee


#测试paste
[skin@bogon shell]$ paste 1.txt 2.txt 
hello   aaa
hello   bbb
hello   ccc
hello   ddd
hello   eee

#测试paste -d
[skin@bogon shell]$ paste 1.txt 2.txt -d ","
hello,aaa
hello,bbb
hello,ccc
hello,ddd
hello,eee

其所有选项
这里写图片描述

  • 04.cut工具

1.cut用来提取文件的片段
2.cut -b 按照字节切分
3.cut -c 按照字符切分
4.cut -d -f :-d指定分隔符,-f指定显示那个区域

#cur -c
[skin@bogon shell]$ echo "hello world" | cut -c 1-3
hel
[skin@bogon shell]$ echo "hello world" | cut -c 1-15
hello world
#cut -b
[skin@bogon shell]$ echo "hello world" | cut -b 1-10
hello worl
[skin@bogon shell]$ echo "hello world" | cut -b 1-13
hello world

#cut -d -f
[skin@bogon shell]$ echo "hello world" | cut -d' ' -f 1
hello
[skin@bogon shell]$ echo "hello world" | cut -d' ' -f 2
world

其相关参数
这里写图片描述

  • 05.xargs工具
  • -

1.xargs : 从标准输入重建并执行命令行,一把都是通过管道作为其他命令的参数。
2.xargs -d -n :-d 按照指定分隔符切分,-n指定列数
3.xargs -i :用{}作为替换符,可以进行替换

#xargs -d -n
[skin@bogon shell]$ echo "hello,world" | xargs -d',' -n 1
hello
world

[skin@bogon shell]$ echo "hello,world" | xargs -d',' -n 2
hello world

# xargs -I
[skin@bogon shell]$ find ./*.txt | xargs -I {} ls {}
./1.txt
./2.txt
./data.txt
./phone.txt
  • 06.set工具

1.set是一种是文本处理工具中和grep一样有用的工具,可以很好地配合正则,完成一些特定工作
2.set -n 表示匹配的结果
3.下面看一些实际用的例子

[skin@bogon shell]$ cat 1.txt 
hello,one
hello,two
hello,three

#匹配以e结尾
[skin@bogon shell]$ cat 1.txt | sed -n '/e$/p'
hello,one
hello,three

#匹配以h开头的
[skin@bogon shell]$ cat 1.txt | sed -n '/^h/p'
hello,one
hello,two
hello,three

#替换所有小写h
[skin@bogon shell]$ cat 1.txt | sed '/^h/s/h/H/'  
Hello,one
Hello,two
Hello,three
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值