shell中的其他文本处理工具(cut ,sort,test等)
1…cut 命令(多用于字符截取)
cut -d # 指定分隔符
cut -f 1,7 # 指定截取的列,第一列和第七列
cut -f 1-7 # 截取一到七列
cut -c 1,4 # 指定截取的字符位置,第一个和第四个
cut -c 1-4 # 截取第一到第四个
[[email protected] mnt]# cut -d ":" -f 1,7 /etc/passwd
[[email protected] mnt]# cut -d ":" -f 1-4 /etc/passwd
[[email protected] mnt]# cut -c 1-4 /etc/fstab
[[email protected] mnt]# cut -c 1,4 /etc/fstab
练习:截取自己本机的IP
[[email protected] mnt]# ifconfig eth0 | grep "inet "| cut -d " " -f10
172.25.254.178
2.sort命令和uniq命令的使用(通常两个联合使用)
(sort多用于字符排序)
sort -n #纯数字排序
sort -r #倒序
sort -u #去掉重复数字
sort -o #输出到指定文件中
sort -t #指定分隔符
sort -k #指定要排序的列
uniq -u #显示唯一的行
uniq -d #显示重复的行
uniq -c #每行显示一次并统计重复次数’
[[email protected] mnt]# vim file
13:32
453:234
342:3
546:21
23:42
56:67
45:32
13:12
[[email protected] mnt]# sort file # 默认排序第一列
[[email protected] mnt]# sort -n file # 纯数字排序
[[email protected] mnt]# sort -u file # 去掉重复的列
[[email protected] mnt]# sort -urn file
[[email protected] mnt]# sort -t : -k 2 file # 以“:”为分隔符,把第二列按照第一个字符的大小进行排序
[[email protected] mnt]# sort -nt : -k 2 file # 以“:”为分隔符,指定第二列按照纯数字排序
[[email protected] mnt]# sort -n file | uniq -c # 每行显示一次并统计重复次数并排序
[[email protected] mnt]# sort -n file | uniq -d # 显示重复的行并进行排序
[[email protected] mnt]# sort -n file | uniq -u # 显示不重复的行并进行排序
练习:排序/mnt下文件大小
[[email protected] mnt]# ll /mnt
[[email protected] mnt]# ll | awk '{print $5,$9}' | sort -n
3…test命令
test命令和 [] 等同
test "$1" == "s2" 等同 [ "$1" == "$2" ]
[ "$1" = "$2" ] # 判断$1是否等于$2
[ "$1" != "$2" ] # 判断$1是否不等于$2
[ "$1" -eq "$2" ] # 判断$1是否等于$2
[ "$1" -ne "$2" ] # 判断$1是否不等于$2,-ne 表示not equal
[ "$1" -le "$2" ] # 判断$1是否小于等于$2,-le表示less equal
[ "$1" -lt "$2" ] # 判断$1是否小于$2,-lt表示less than
[ "$1" -ge "$2" ] # 判断$1是否大于等于$2,-ge表示great equal
[ "$1" -gt "$2" ] # 判断$1是大于$2,-gt 表示great than
[ "$1" -ne "$2" -a "$1" -gt "$2" ] # 判断$1是否不等于且大于$2
[ "$1" -ne "$2" -o "$1" -gt "$2" ] # 判断$1是否不等于或者大于$2
[ -z "$2" ] #判断$2是否为空
[ -n "$2" ] # 判断$2是否不为空
[ -e "file2" ] # 判断文件是否存在
[ -f "file2" ] # 判断文件是否为普通文件
[ -L "file2" ] #判断文件是否为软链接
[ -S "file2" ] #判断文件是否为套接字
[ -b "file2" ] #判断文件是否为块设备
[ -d "file2" ] #判断设备是否为文件
[ -c "file2" ] #判断是否为字符设备
[ "file1" -ef "file2" ] #判断file1是否与file2互为硬链接
[ "file1" -nt "file2" ] #判断file1与file2哪个出现的晚
[ "file1" -ot "file2" ] #判断file1与file2哪个出现的早
[[email protected] mnt]# ln /mnt/file /mnt/file1 # 建立硬链接
[[email protected] mnt]# ls -li /mnt # 查看节点是否相同
[[email protected] mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes || echo no # 判断/mnt/file和/mnt/file1互为硬链接
yes
[[email protected] mnt]# [ "/mnt/file" -ef "/etc/passwd" ]&& echo yes || echo no # 判断/mnt/file和/etc/passwd互为硬链接
no
练习1:判断输入的数字是否在10以内
[[email protected] mnt]# vim digit.sh
10 #!/bin/bash
11 [ -z "$1" ]&& {
12 echo Error
13 exit 1
14 }
15 [ "$1" -ne 0 -a "$1" -lt "10" ]&& {
16 echo $1 is in 1~10
17 }|| {
18 echo $1 is out of 1~10
19 }
[[email protected] mnt]# sh digit.sh 11
11 is out of 1~10
[[email protected] mnt]# sh digit.sh
Error
[[email protected] mnt]# sh digit.sh 2
2 is in 1~10
练习2:判断文件的类型
[[email protected] mnt]# vim file_type.sh
10 #!/bin/bash
11 [ -z "$1" ] && {
12 echo "please input a file name after script"
13 exit 1
14 }
15 [ -e "$1" ] || {
16 echo "this file is not exist"
17 exit 0
18 }
19 [ -L "$1" ] && {
20 echo "$1 is a link"
21 exit 0
22 }
23 [ -S "$1" ] && {
24 echo"$1 is a socket"
25 exit 0
26 }
27 [ -f "$1" ] && {
28 echo "$1 is commer file"
29 exit 0
30 }
31 [ -d "$1" ] && {
32 echo "$1 is a directory"
33 exit 0
34 }
35 [ -b "$1" ] && {
36 echo "$1 is a block"
37 exit 0
38 }
39 [ -c "$1" ] && {
40 echo "$1 is a character device"
41 exit 0
[[email protected] mnt]# sh file_type.sh /etc/system-release
/etc/system-release is a link
[[email protected] mnt]# sh file_type.sh /etc/passwd
/etc/passwd is commer file
[[email protected] mnt]# sh file_type.sh /dev/vdb
/dev/vdb is a block
[[email protected] mnt]# sh file_type.sh lala
this file is not exist