当前位置:网站首页>shell脚本常用命令(四)
shell脚本常用命令(四)
2022-06-27 17:52:00 【iNBC】
xargs
Unix命令可以从标准输入( stdin)或命令行参数中接收数据。
xargs命令应该紧跟在管道操作符之后。它使用标准输入作为主要的数据源,将从stdin中读取的数据作为指定命令的参数并执行该命令。下面的命令将在一组C语言源码文件中搜索字符
串main:
[[email protected] shell_learning]$ ls *.c | xargs grep main
int main(int argc, char const *argv[])
xargs将多行输入转换成单行输出
[[email protected] 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"
[[email protected] 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选项可以限制每次调用命令时用到的参数个数。
[[email protected] 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"
[[email protected] 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选项可以为输入数据指定自定义的分隔符。
实例:
[[email protected] shell_learning]$ cat output.txt | xargs -d X
123 456 789 123 456 789
[[email protected] shell_learning]$ cat output.txt | xargs -d X -n 3
123 456 789
123 456 789
dd命令
实例:
该命令会创建一个内容全部为零的1*10MB大小的文件123。
[[email protected] 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表示需要被复制的块数。- 字节(
1B)C- 字(
2B)w- 块(
512B)B- 千字节(
1024B)K- 兆字节(
1024KB)M- 吉字节(
1024MB)G
tr 进行转换
tr只能通过stdin(标准输入)接收输入(无法通过命令行参数接收)。其调用格式如下:
tr [options] set1 set2
set1和set2是字符类或字符组。如果两个字符组的长度不相等,那么set2会不断复制其最后一个字符,直到长度与set1相同。如果set2的长度大于set1,那么在set2中超出set1长度的那部分字符则全部被忽略。
实例:
[[email protected] nbc]$ echo "123 456 789" | tr "123" "000"
000 456 789
[[email protected] nbc]$ echo "123 456 789" | tr "123" "02"
022 456 789
[[email protected] nbc]$ echo "123 456 789" | tr "123" "00222"
002 456 789
[[email protected] nbc]$ echo "abcdefg123" | tr "a-z0-9" "A-Z0"
ABCDEFG000
用tr压缩字符
tr -s '[需要被压缩的一组字符]'
如果你习惯在点号后面放置两个空格,你需要在不删除重复字母的情况下去掉多余的空格:
[[email protected] nbc]$ echo "GNU is not UNIX. Recursive right ?" | tr -s ' '
GNU is not UNIX. Recursive right ?
[[email protected] 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
[[email protected] nbc]# cat lines.txt | tr -s '\n'
line1
line2
line3
line4
用tr删除字符
-d选项,可以通过指定需要被删除的字符集合,将出现在stdin中的特定字符清
除掉。
用法:
tr -d '[set1]'
实例:
[[email protected] nbc]$ echo "Hello 123 world 456" | tr -d '0-9'
Hello world
字符组补集
tr可以将不同的字符类作为集合使用,所支持的字符类如下所示。
alnum:字母和数字。alpha:字母。cntrl:控制(非打印)字符。digit:数字。graph:图形字符。lower:小写字母。punct:标点符号。space:空白字符。upper:大写字母。xdigit:十六进制字符。
可以按照下面的方式选择所需的字符类:
tr [:class:] [:class:]
实例:
[[email protected] nbc]$ echo "Hello 123 world 456" | tr '[:lower:]' '[:upper:]'
HELLO 123 WORLD 456
校验和与核实
Unix和Linux支持多种校验和程序,但强健性最好且使用最为广泛的校验和算法是MD5和SHA-1。 md5sum和sha1sum程序可以对数据应用对应的算法来生成校验和。下面就来看看如何从文件中生成校验和并核实该文件的完整性。
用法:
md5sum filename
68b329da9893e34099c7d8ad5cb9c940 filename
如上所示, md5sum是一个长度为32个字符的十六进制串。
实例:
[[email protected] nbc]$ md5sum lines.txt
53fd1719570a692eba410de782b9929b lines.txt
-c选项可以用来核实数据文件的完整性。
[[email protected] nbc]$ md5sum lines.txt > lines.txt.md5
[[email protected] nbc]$ md5sum -c lines.txt.md5
lines.txt: OK
如果修改了lines.txt文件的内的任何一个部分,都会校验失败。如下代码所示:
[[email protected] shell_learning]$ md5sum -c lines.txt.md5
lines.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
行排序(sort、uniq)
可以按照下面的方式排序一个或者一组文件:
[[email protected] shell_learning]$ cat A.txt
gold
orange
silver
iron
steel
apple
[[email protected] shell_learning]$ cat A.txt | sort
apple
gold
iron
orange
silver
steel
按照数字顺序排序
[[email protected] shell_learning]$ cat D.txt | sort -n
0
1
2
3
4
5
7
8
9
按照逆序排序
[[email protected] shell_learning]$ cat D.txt | sort -r
9
8
7
5
4
3
2
1
0
按照月份排序(依照一月、二月、三月……)
sort -M months.txt
合并两个已排序过的文件
sort -m sorted1 sorted2
[[email protected] shell_learning]# sort A.txt B.txt | sort -m
apple
carrot
cookies
gold
gold
gold
iron
orange
orange
orange
silver
steel
找出已排序文件中不重复的行
[[email protected] shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[[email protected] shell_learning]$ sort B.txt | uniq
carrot
cookies
gold
orange
只显示唯一的行(在输入文件中没有重复出现过的行)
uniq -u sorted.txt
[[email protected] shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[[email protected] shell_learning]$ sort B.txt | uniq -u
carrot
cookies
[[email protected] shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[[email protected] shell_learning]$ sort B.txt | uniq -c
1 carrot
1 cookies
2 gold
2 orange
找出文件中重复的行
[[email protected] shell_learning]$ cat B.txt
cookies
orange
gold
carrot
gold
orange
[[email protected] shell_learning]$ sort B.txt | uniq -d
gold
orange
我们可以结合-s和-w选项来指定键:
-s指定跳过前N个字符;-w指定用于比较的最大字符数。
为了只测试指定的字符(忽略前两个字符,使用接下来的两个字符),我们使用-s 2跳过前两个字符,使用-w 2选项指定后续的两个字符。
实例:
[[email protected] shell_learning]$ cat B.txt
a:01:cookies
c:09:orange
z:01:gold
b:01:carrot
o:05:gold
r:01:orange
[[email protected] shell_learning]$ sort B.txt | uniq -s 2 -w 2
a:01:cookies
c:09:orange
o:05:gold
r:01:orange
边栏推荐
- Campus book resource sharing platform
- CMS 执行的七个阶段
- Keras deep learning practice (12) -- facial feature point detection
- Market status and development prospect forecast of global handheld ventilator industry in 2022
- 【云驻共创】高校数字化差旅建设“解决之道”
- 【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
- Comment encapsuler un appel à une bibliothèque
- Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
- 广发期货开户安全吗?
- Market status and development prospect forecast of global 3-Chloro-1,2-Propanediol industry in 2022
猜你喜欢

Camera calibration with OpenCV

New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million

Keras深度学习实战(12)——面部特征点检测

新中大冲刺科创板:年营收2.84亿 拟募资5.57亿

Rxjs mergeMap 的使用场合

laravel框架中 定时任务的实现

【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩

The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder

What is ICMP? What is the relationship between Ping and ICMP?

Redis 原理 - String
随机推荐
Market status and development prospect forecast of the global infusion needle less connector industry in 2022
可靠的分布式锁 RedLock 与 redisson 的实现
Is it safe to buy stocks and open an account on the account opening link of the securities manager? Ask the great God for help
驾驭一切的垃圾收集器 -- G1
Market status and development prospect forecast of global 3-Chloro-1,2-Propanediol industry in 2022
谈谈线程安全
Running lantern experiment based on stm32f103zet6 library function
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
一对一关系
Exporting coordinates of points in TXT format in ArcGIS
基于STM32F103ZET6库函数跑马灯实验
華大單片機KEIL報錯_WEAK的解决方案
华大单片机KEIL报错_WEAK的解决方案
一位平凡毕业生的大学四年
xctf攻防世界 MISC薪手进阶区
如何利用 RPA 实现自动化获客?
Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
在线文本按行批量反转工具
基于STM32F103ZET6库函数按键输入实验