shell脚本常用命令(四)

xargs

Unix命令可以从标准输入( stdin)或命令行参数中接收数据。

xargs命令应该紧跟在管道操作符之后。它使用标准输入作为主要的数据源,将从stdin中读取的数据作为指定命令的参数并执行该命令。下面的命令将在一组C语言源码文件中搜索字符
main

[root@localhost shell_learning]$ ls *.c | xargs grep main
int main(int argc, char const *argv[])

xargs将多行输入转换成单行输出

[root@localhost shell_learning]$ cat output.txt
123 456 789 123 456 789
123 456 789 123 456 789
123 456 789 123 456 789
var1="123"
var2="789"
[root@localhost shell_learning]$ cat output.txt | xargs
123 456 789 123 456 789 123 456 789 123 456 789 123 456 789 123 456 789 var1=123 var2=789


将单行输入转换成多行输出

  • -n选项可以限制每次调用命令时用到的参数个数。
[root@localhost shell_learning]$ cat output.txt
123 456 789 123 456 789
123 456 789 123 456 789
123 456 789 123 456 789
var1="123"
var2="789"
[root@localhost shell_learning]$ cat output.txt | xargs -n 3
123 456 789
123 456 789
123 456 789
123 456 789
123 456 789
123 456 789
var1=123 var2=789

定义一个用来分隔参数的分隔符

  • -d选项可以为输入数据指定自定义的分隔符。

实例:

[root@localhost shell_learning]$ cat output.txt | xargs -d X
123 456 789 123 456 789


[root@localhost shell_learning]$ cat output.txt | xargs -d X -n 3
123 456 789
123 456 789

dd命令

实例:
该命令会创建一个内容全部为零的1*10MB大小的文件123

[root@localhost nbc]$ dd if=/dev/zero of=123 bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0066654 s, 1.6 GB/s
  • if表示输入文件( input file);
  • of表示输出文件( output file);
  • bs指定了以字节为单位的块大小( block size);
  • count表示需要被复制的块数。
  • 字节( 1BC
  • 字( 2Bw
  • 块( 512BB
  • 千字节( 1024BK
  • 兆字节( 1024KBM
  • 吉字节( 1024MBG

tr 进行转换

tr只能通过stdin(标准输入)接收输入(无法通过命令行参数接收)。其调用格式如下:

tr [options] set1 set2

set1set2是字符类或字符组。如果两个字符组的长度不相等,那么set2会不断复制其最后一个字符,直到长度与set1相同。如果set2的长度大于set1,那么在set2中超出set1长度的那部分字符则全部被忽略。
实例:

[root@localhost nbc]$ echo "123 456 789" | tr "123" "000"
000 456 789
[root@localhost nbc]$ echo "123 456 789" | tr "123" "02"
022 456 789
[root@localhost nbc]$ echo "123 456 789" | tr "123" "00222"
002 456 789
[root@localhost nbc]$ echo "abcdefg123" | tr "a-z0-9" "A-Z0"
ABCDEFG000

用tr压缩字符

tr -s '[需要被压缩的一组字符]'

如果你习惯在点号后面放置两个空格,你需要在不删除重复字母的情况下去掉多余的空格:

[root@localhost nbc]$ echo "GNU is not UNIX. Recursive right ?" | tr -s ' '
GNU is not UNIX. Recursive right ?

[root@localhost nbc]# cat -n lines.txt
     1  line1
     2
     3
     4  line2
     5
     6
     7
     8
     9  line3
    10
    11
    12
    13
    14
    15
    16  line4
    17
[root@localhost nbc]# cat  lines.txt | tr -s '\n'
line1
line2
line3
line4

用tr删除字符

-d选项,可以通过指定需要被删除的字符集合,将出现在stdin中的特定字符清
除掉。

用法:

tr -d '[set1]'

实例:

[root@localhost nbc]$ echo "Hello 123 world 456" | tr -d '0-9'
Hello  world

字符组补集

tr可以将不同的字符类作为集合使用,所支持的字符类如下所示。

  • alnum:字母和数字。
  • alpha:字母。
  • cntrl:控制(非打印)字符。
  • digit:数字。
  • graph:图形字符。
  • lower:小写字母。
  • print:可打印字符。
  • punct:标点符号。
  • space:空白字符。
  • upper:大写字母。
  • xdigit:十六进制字符。

可以按照下面的方式选择所需的字符类:

tr [:class:] [:class:]

实例:

[root@localhost nbc]$ echo "Hello 123 world 456" | tr '[:lower:]' '[:upper:]'
HELLO 123 WORLD 456

校验和与核实

UnixLinux支持多种校验和程序,但强健性最好且使用最为广泛的校验和算法是MD5SHA-1md5sumsha1sum程序可以对数据应用对应的算法来生成校验和。下面就来看看如何从文件中生成校验和并核实该文件的完整性。

用法:

md5sum filename
68b329da9893e34099c7d8ad5cb9c940 filename

如上所示, md5sum是一个长度为32个字符的十六进制串。

实例:

[root@localhost nbc]$ md5sum lines.txt
53fd1719570a692eba410de782b9929b  lines.txt

-c选项可以用来核实数据文件的完整性。

[root@localhost nbc]$ md5sum lines.txt > lines.txt.md5
[root@localhost nbc]$ md5sum -c lines.txt.md5
lines.txt: OK

如果修改了lines.txt文件的内的任何一个部分,都会校验失败。如下代码所示:

[root@localhost shell_learning]$ md5sum -c lines.txt.md5
lines.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

行排序(sort、uniq)

可以按照下面的方式排序一个或者一组文件:

[root@localhost shell_learning]$ cat A.txt
gold
orange
silver
iron
steel
apple
[root@localhost shell_learning]$ cat A.txt | sort
apple
gold
iron
orange
silver
steel

按照数字顺序排序

[root@localhost shell_learning]$ cat D.txt | sort -n
0
1
2
3
4
5
7
8
9


按照逆序排序

[root@localhost shell_learning]$ cat D.txt | sort -r
9
8
7
5
4
3
2
1
0

按照月份排序(依照一月、二月、三月……)

 sort -M months.txt

合并两个已排序过的文件

sort -m sorted1 sorted2
[root@localhost shell_learning]# sort  A.txt B.txt | sort -m
apple
carrot
cookies
gold
gold
gold
iron
orange
orange
orange
silver
steel

找出已排序文件中不重复的行

[root@localhost shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[root@localhost shell_learning]$ sort   B.txt | uniq
carrot
cookies
gold
orange

只显示唯一的行(在输入文件中没有重复出现过的行)

uniq -u sorted.txt
[root@localhost shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[root@localhost shell_learning]$ sort B.txt | uniq -u
carrot
cookies

[root@localhost shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[root@localhost shell_learning]$ sort B.txt | uniq -c
      1 carrot
      1 cookies
      2 gold
      2 orange

找出文件中重复的行

[root@localhost shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[root@localhost shell_learning]$ sort B.txt | uniq -d
gold
orange

我们可以结合-s和-w选项来指定键:

  • -s 指定跳过前N个字符;
  • -w 指定用于比较的最大字符数。

为了只测试指定的字符(忽略前两个字符,使用接下来的两个字符),我们使用-s 2跳过前两个字符,使用-w 2选项指定后续的两个字符。

实例:

[root@localhost shell_learning]$ cat B.txt
a:01:cookies
c:09:orange
z:01:gold
b:01:carrot
o:05:gold
r:01:orange
[root@localhost shell_learning]$ sort B.txt | uniq -s 2 -w 2
a:01:cookies
c:09:orange
o:05:gold
r:01:orange
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iNBC

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值