当前位置:网站首页>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
边栏推荐
- The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
- 华大单片机KEIL添加ST-LINK解决方法
- Code and principle of RANSAC
- 2022年第一季度消费金融APP用户洞察——总数达4479万人
- Solution to Maxwell error (MySQL 8.x connection)
- “我让这个世界更酷”2022华清远见研发产品发布会圆满成功
- What is ssr/ssg/isr? How do I host them on AWS?
- Introduction to deep learning and neural networks
- 图扑数字孪生智慧能源一体化管控平台
- Tupu digital twin intelligent energy integrated management and control platform
猜你喜欢

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

Running lantern experiment based on stm32f103zet6 library function

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

从感知机到前馈神经网络的数学推导

Blink SQL内置函数大全

基于STM32F103ZET6库函数蜂鸣器实验

基于STM32F103ZET6库函数按键输入实验

International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding

GIS remote sensing R language learning see here

基于STM32F103ZET6库函数外部中断实验
随机推荐
C# 二维码生成、识别,去除白边、任意颜色
什么是SSR/SSG/ISR?如何在AWS上托管它们?
Substrate及波卡一周技术更新速递 20220425 - 20220501
在arcgis中以txt格式导出点的坐标
Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million
Memoirs of actual combat: breaking the border from webshell
Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
基础数据类型和复杂数据类型
Character interception triplets of data warehouse: substrb, substr, substring
通过 G1 GC Log 重新认识 G1 垃圾回收器
数据分析师太火?月入3W?用数据告诉你这个行业的真实情况
Market status and development prospect forecast of the global shuttleless air jet loom industry in 2022
网络传输是怎么工作的 -- 详解 OSI 模型
一位平凡毕业生的大学四年
Market status and development prospect forecast of phenethyl resorcinol for skin care industry in the world in 2022
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
Comprehensively analyze the zero knowledge proof: resolve the expansion problem and redefine "privacy security"
什么是 ICMP ?ping和ICMP之间有啥关系?
Blink SQL内置函数大全
maxwell 报错(连接为mysql 8.x)解决方法