当前位置:网站首页>shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
2022-07-28 05:28:00 【渔火鳅】
shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
一、sort命令
以行为单位对文件内容进行排序,也可以根据不同的数据类型来排序
1、语法格式
sort [选项] 参数
cat file | sort 选项
2、常用选项

3、命令配置及示例
sort -n testfile2
sort -t ‘:’ -k 3 -n /etc/passwd
du -a | sort -nr -0 du.txt

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mHomdKBa-1658074073763)(https://s2.loli.net/2022/07/18/EkQ92HS3egGvjbW.png)]
二、uniq命令
用于报告或者忽略文件中连续的重复行,常与sort命令结合使用
1、语法格式
uniq [选项] 参数
cat file | uniq 选项
2、常用选项
3、命令配置及示例
uniq testfile3
sort -n testfile3 | uniq -c
三、tr命令
常用来对来自标准输入的字符进行替换、压缩和删除
1、语法格式
tr [选项] [参数]
2、常用选项

3、参数
字符集1
指定要转换或删除的原字符集。当执行转换操作时,必须使用参数“字符集2”指定转换的目标字符集。但执行删除操作时,不需要参数“字符集2”;
字符集2
指定要转换成的目标字符集
4、命令配置及示例
echo “abc” | tr ‘a-z’ ‘A-Z’
echo -e “abc\ncabcdab” | tr -c “ab\n” “0”
echo -e “abc\ncabcdab” | tr -c “ab” “0”
echo ‘hello world’ | tr -d ‘od’
echo “thissss is a text linnnnnnne.” | tr -s ‘sn’
删除空行
echo -e “aa\n\n\n\n\nbb” | tr -s “\n”
cat testfile5 | tr -s “\n”
把路径变量中的冒号":“,替换成换行符”\n"
echo $PATH | tr -s “:” “\n”
echo -e “aa\n\n\n\n\nbb” | tr -s "\n” “:”
删除Windows文件“造成”的"^M’字符:
cat abc.txt | tr -s “\r” “\n” > new_ file
或
cat abc.txt | tr -d “\r” > new_file
Linux中遇到换行符(“\n”)会进行回车+换行的操作,回车符反而只会作为控制字符(“^M”)显示,不发生回车的操作。而windows中 要回车符
+换行符(“\r\n”)才会回车+换行,缺少一个控制符或者顺序不对都不能正确的另起一行。
cat -v abc.txt
dos2unix abc.txt
四、数组排序
echo ${array[*]} | tr ’ ’ ‘\n’ | sort -n > file
a=0
for i in ( c a t f i l e ) d o a r r a y [ (cat file) do array[ (catfile)doarray[a]=KaTeX parse error: Expected 'EOF', got '#' at position 11: i let a++ #̲array+=(i)
done

五、cut命令
显示行中指定的部分,删除文件中的指定字段
1、语法格式
cut 选项 参数
cat file | cut 选项
2、常用选项

3、命令配置及示例
cut -d ‘:’ -f 1 /etc/passwd
grep ‘/bin/bash’ /etc/passwd|cut -d ‘:’ -f 1-4,6,7
#以-分隔的开始字段和结束字段指定字段的范围
grep ‘/bin/bash’ /etc/passwd|cut -d ‘:’ --complement -f 2
#排除第二个字段
cut -d ‘:’ -f1,7 --output-delimiter=’ ’ /etc/passwd
#输出分隔符使用空格分隔
i=123456789
echo $i| cut -b 1-3 #起始位置从1开始
123
echo ${i:0:3} #起始位置从0开始
expr substr $i 1 3 #起始位置从1开始
六、eval命令
前加上eval时,shell就会在执行命令之前扫描它两次.eval命令将首先会先扫描命令行进行所有的置换,然后再执行该命令。该命令适用于那些一次扫描无法实现其功能的变量。该命令对变量进行两次扫描

、eval命令
前加上eval时,shell就会在执行命令之前扫描它两次.eval命令将首先会先扫描命令行进行所有的置换,然后再执行该命令。该命令适用于那些一次扫描无法实现其功能的变量。该命令对变量进行两次扫描
[外链图片转存中…(img-oOSVIXFn-1658074073766)]

边栏推荐
猜你喜欢

单元测试框架Jest搭配TypeScript的安装与配置

Analysis of the semaphore source code of AQS

思寒漫谈测试人职业发展

Cocos2d-x learning notes Tile Map tiledmap

How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with

DHCP原理与配置

Explain in detail

Build php7 private warehouse
![[untitled]](/img/54/660667e528729cc87796d972dc0b17.png)
[untitled]

技术分享 | 接口测试价值与体系
随机推荐
Build php7 private warehouse
Tcp/ip five layer model
Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?
Which brand of air conduction earphones is good and highly praised
Explain in detail
FTP服务
数组转链表
Yapi vulnerability hanging horse program chongfu.sh processing
Si Han talks about the professional development of testers
@Postconstruct annotations and useful examples
Scratch command
[C language] string library function introduction and simulation
MySQL主从
MySQL常用命令
Lancher deployment practice
Vmware workstation configuration net mode
Test interview questions collection (II) | test tools (with answers)
Brief analysis of order transaction
Test interview questions collection (I) | common required questions and procedures of software testing (with answers)
HDU-1159-CommonSubsequence(LCS最长公共子序列)
