当前位置:网站首页>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
边栏推荐
- VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
- 华大单片机KEIL报错_WEAK的解决方案
- Market status and development prospect forecast of global active quality control air sampler industry in 2022
- Code and principle of RANSAC
- Error reported by Huada MCU Keil_ Weak's solution
- Character interception triplets of data warehouse: substrb, substr, substring
- External interrupt experiment based on stm32f103zet6 library function
- 国信证券是国企吗?在国信证券开户资金安全吗?
- How to encapsulate and call a library
- 基于STM32F103ZET6库函数按键输入实验
猜你喜欢
im即时通讯开发之双进程守护保活实践
基于STM32F103ZET6库函数跑马灯实验
Keras deep learning practice (12) -- facial feature point detection
【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
Oracle 获取月初、月末时间,获取上一月月初、月末时间
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
xctf攻防世界 MISC薪手进阶区
新中大冲刺科创板:年营收2.84亿 拟募资5.57亿
Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded
Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
随机推荐
Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
Solution of adding st-link to Huada MCU Keil
数仓的字符截取三胞胎:substrb、substr、substring
Implementation of reliable distributed locks redlock and redisson
在线文本按行批量反转工具
基础数据类型和复杂数据类型
Keras deep learning practice (12) -- facial feature point detection
循环遍历及函数基础知识
Market status and development prospect forecast of global handheld ventilator industry in 2022
Labelimg usage guide
im即时通讯开发之双进程守护保活实践
可靠的分布式锁 RedLock 与 redisson 的实现
maxwell 报错(连接为mysql 8.x)解决方法
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
一种朴素的消失点计算方法
Hi,你有一份Code Review攻略待查收!
国信证券是国企吗?在国信证券开户资金安全吗?
如何实现IM即时通讯“消息”列表卡顿优化
清华徐勇、段文晖研究组开发出高效精确的第一性原理电子结构深度学习方法与程序