seq、tr、sort、uniq、cut、数组的基本用法

1.seq打印数字命令

1.1 seq用途及格式

seq命令 用途:打印出一串有序的数字 格式:seq [选项] 数字范围
-s:指定分隔符
-w:指定同等带宽输出

比如:

[root@liupeng ~]# seq -s " " 90 100
90 91 92 93 94 95 96 97 98 99 100
[root@liupeng ~]# seq -s "*" 90 100
90*91*92*93*94*95*96*97*98*99*100
[root@liupeng ~]# seq -s "*" -w 90 100
090*091*092*093*094*095*096*097*098*099*100
[root@liupeng ~]# seq 1 2 10
1
3
5
7
9
[root@liupeng ~]# 

1.2 数组和seq命令的应用

选择双色球小脚本执行情况:

[root@liupeng liupeng]# sh shuangseqiu.sh 
欢迎购买福彩双色球,按任意键机选一注
红球:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
蓝球:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
你的幸运号码是: 红球:14 17 22 28 20 7 蓝球:1 
[root@liupeng liupeng]# 

脚本:

#!/bin/bash
echo "欢迎购买福彩双色球,按任意键机选一注"
redball=($(seq 32))
echo -e "\033[31m红球:\n$(echo ${redball[@]})\033[0m"
blueball=($(seq 16))
echo -e "\033[34m蓝球:\n$(echo ${blueball[@]})\033[0m"
blen=${#blueball[@]}
for i in $(seq 6)
do
rlen=${#redball[@]}
rnum=$((RANDOM % rlen))
rb[x++]=${redball[rnum]}
unset redball[rnum]
redball=(${redball[@]})
done
bnum=$((RANDOM % blen))
bb=${blueball[bnum]}
read -n1 -s
echo -e "你的幸运号码是: \033[31m红球:${rb[@]}\033[0m \033[34m蓝球:$bb\033[0m "

2. tr命令

2.1 格式及使用方法

字符转换工具,只能对stdin操作,不能直接对文件操作
使用tr转换字符:

格式:tr set1 set2

2.2 作用

2.2.1 替换字符tr 'abc' 'efg'

用set2中的字符替换掉set1中同一位置的字符(并没动源文件)

[root@liupeng liupeng]# echo 123456|tr 345 abc
12abc6
[root@liupeng liupeng]# tr '[a-z]' '[A-Z]' < /etc/hosts
127.0.0.1   LOCALHOST LOCALHOST.LOCALDOMAIN LOCALHOST4 LOCALHOST4.LOCALDOMAIN4
::1         LOCALHOST LOCALHOST.LOCALDOMAIN LOCALHOST6 LOCALHOST6.LOCALDOMAIN6
[root@liupeng liupeng]# echo $PATH | tr ':' '\n' 
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin
/usr/local/python3.6.4/bin
[root@liupeng liupeng]# 

2.2.2 删除字符-d

格式:tr -d set

将stdin中数据流删除与set相同的字符。

[root@liupeng liupeng]# echo 123456 | tr -d 345 
126
[root@liupeng liupeng]# df -h | tr -d %
文件系统	      容量  已用  可用 已用 挂载点
/dev/mapper/vg_liupeng-lv_root
                       18G   13G  4.2G  75 /
tmpfs                 491M  100K  491M   1 /dev/shm
/dev/sda1             485M   33M  427M   8 /boot
/dev/sr0              4.0G  4.0G     0 100 /media/CentOS_6.3_Final
[root@liupeng liupeng]# df -h 
文件系统	      容量  已用  可用 已用%% 挂载点
/dev/mapper/vg_liupeng-lv_root
                       18G   13G  4.2G  75% /
tmpfs                 491M  100K  491M   1% /dev/shm
/dev/sda1             485M   33M  427M   8% /boot
/dev/sr0              4.0G  4.0G     0 100% /media/CentOS_6.3_Final
[root@liupeng liupeng]# 

2.2.3 压缩字符-s

①直接压缩

格式:tr -s SET

将连续相同的字符压缩成一个字符

[root@liupeng liupeng]# echo 112233444555666 | tr -s 345
1122345666
[root@liupeng liupeng]# ifconfig | tr -s ' '
eth1 Link encap:Ethernet HWaddr 00:50:56:38:27:FF 
 inet addr:192.168.28.129 Bcast:192.168.28.255 Mask:255.255.255.0
 inet6 addr: fe80::250:56ff:fe38:27ff/64 Scope:Link
 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
 RX packets:7104 errors:0 dropped:0 overruns:0 frame:0
 TX packets:1100 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000 
 RX bytes:492156 (480.6 KiB) TX bytes:126577 (123.6 KiB)

lo Link encap:Local Loopback 
 inet addr:127.0.0.1 Mask:255.0.0.0
 inet6 addr: ::1/128 Scope:Host
 UP LOOPBACK RUNNING MTU:16436 Metric:1
 RX packets:16 errors:0 dropped:0 overruns:0 frame:0
 TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:0 
 RX bytes:960 (960.0 b) TX bytes:960 (960.0 b)


[root@liupeng liupeng]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:50:56:38:27:FF  
          inet addr:192.168.28.129  Bcast:192.168.28.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fe38:27ff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7183 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1115 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:497782 (486.1 KiB)  TX bytes:129411 (126.3 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

[root@liupeng liupeng]# 

②先替换再压缩

格式:tr -s SET1 SET2

先替换为SET2再压缩:

[root@liupeng liupeng]# echo 112233444555666 | tr -s 345 abc
1122abc666
[root@liupeng liupeng]# who | tr -s ' '  '\n' 
root
tty1
2020-03-30
06:56
(:0)
root
pts/0
2020-03-30
06:56
(:0.0)
root
pts/1
2020-03-30
06:57
(192.168.28.1)
[root@liupeng liupeng]# who
root     tty1         2020-03-30 06:56 (:0)
root     pts/0        2020-03-30 06:56 (:0.0)
root     pts/1        2020-03-30 06:57 (192.168.28.1)
[root@liupeng liupeng]# 

3. sort 排序命令

默认按每行的第一个字符排序。

3.1 用法

-n:按整数进行排序
-r:递减排序
-k:指定哪一列为排序键 : cat tt.txt | sort -n -k4
-t:指定字段分割符(默认是空格) : sort -t: -n -k3 /etc/passwd

4. uniq去重命令

删除经过排序后的数据的重复记录 ,通常和sort连用 。

sort -n 文件 | uniq

数据的实例统计:

-c:统计特定记录出现的次数
-u:只显示唯一的行
-d:只显示重复的行

5. cut提取命令

5.1 作用及格式

作用:从文本文件或者文本流中提取文本列
格式:cut -选项 提取范围 文本文件

-c:从指定提取范围中提取字符
-f:从指定提取范围中提取字段

提取范围:

n:第n项 n-:第n项到行尾
-m:行首到第m项 n,m:第n项和第m项 n-m:第n项到第m项

[root@liupeng liupeng]# who
root     tty1         2020-03-30 06:56 (:0)
root     pts/0        2020-03-30 06:56 (:0.0)
root     pts/1        2020-03-30 06:57 (192.168.28.1)
[root@liupeng liupeng]# who | cut -c 20-40
   2020-03-30 06:56 (
   2020-03-30 06:56 (
   2020-03-30 06:57 (
[root@liupeng liupeng]# 
[root@liupeng liupeng]# who
root     tty1         2020-03-30 06:56 (:0)
root     pts/0        2020-03-30 06:56 (:0.0)
root     pts/1        2020-03-30 06:57 (192.168.28.1)
[root@liupeng liupeng]# who | cut -d' ' -f1,6
root tty1
root pts/0
root pts/1
[root@liupeng liupeng]# who | cut -d ' ' -f1,6
root tty1
root pts/0
root pts/1
[root@liupeng liupeng]# 

6.数组的用法

6.1 利用循环为数组赋值

[root@liupeng liupeng]# cat gate.sh 
#!/bin/bash
for i in tom jerry lee mike
do
names[x++]=$i;
done
echo ${names[@]}
[root@liupeng liupeng]# bash gate.sh 
tom jerry lee mike
[root@liupeng liupeng]# 

6.2 数组的几个常见用法

${name[@]} --》显示数组全部元素
${#name[@]} --》显示数组元素个数
${!name[@]} --》显示数组下标
${name[user]} --》显示下标为user的数组元素是啥

实例:

[root@liupeng liupeng]# declare -A name
[root@liupeng liupeng]# name=([user]=aa [pwd]=bb [age]=cc)
[root@liupeng liupeng]# echo ${name[@]}
bb aa cc
[root@liupeng liupeng]# echo ${#name[@]}  
3
[root@liupeng liupeng]# echo ${!name[@]} 
pwd user age
[root@liupeng liupeng]# echo ${name[user]}  
aa
[root@liupeng liupeng]# 

7. 练习

7.1 练习一

1.截取出本机上所有网卡的ip地址和子网掩码、默认网关如下所示:
示例:

eth0 ip address is 172.16.1.1/16
eth1 ip address is 173.168.1.1/16
default gateway is 173.168.20.20

得到eth0和eth1的名字:

[root@liupeng ~]# ip add | grep "mtu" | awk '{print $2}' | tr -d :
lo
eth1
[root@liupeng ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:38:27:ff brd ff:ff:ff:ff:ff:ff
    inet 192.168.28.129/24 brd 192.168.28.255 scope global eth1
    inet6 fe80::250:56ff:fe38:27ff/64 scope link 
       valid_lft forever preferred_lft forever
[root@liupeng ~]# 

得到eth0的ip地址:

[root@liupeng ~]# ifconfig lo | grep "inet addr" | tr -s ':' ' ' | cut -d' ' -f4
127.0.0.1
[root@liupeng ~]# ifconfig lo
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

[root@liupeng ~]# 

得到eth1的ip地址:

[root@liupeng ~]# ifconfig eth1 | grep "inet addr" | tr -s ':' ' ' | cut -d' ' -f4
192.168.28.129
[root@liupeng ~]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:50:56:38:27:FF  
          inet addr:192.168.28.129  Bcast:192.168.28.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fe38:27ff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23090 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3343 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1583418 (1.5 MiB)  TX bytes:356493 (348.1 KiB)

[root@liupeng ~]# 

得到默认网关:

[root@liupeng ~]# route -n | grep "^192" | awk '{print $2}'
0.0.0.0
[root@liupeng ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.28.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
0.0.0.0         192.168.28.2    0.0.0.0         UG    0      0        0 eth1
[root@liupeng ~]#

综上:

[root@liupeng liupeng]# sh get_ip.sh 
lo ip address is  127.0.0.1.
eth1 ip address is 192.168.28.129.
default gateway is  0.0.0.0.
[root@liupeng liupeng]# 
[root@liupeng liupeng]# cat get_ip.sh 
#!/bin/bash
lo=$(ip add | grep "mtu" | awk '{print $2}' | tr -d : | head -1)
eth1=$(ip add | grep "mtu" | awk '{print $2}' | tr -d :|tail -1)
lo_ip=$(ifconfig lo | grep "inet addr" | tr -s ':' ' ' | cut -d' ' -f4)
eth1_ip=$(ifconfig eth1 | grep "inet addr" | tr -s ':' ' ' | cut -d' ' -f4)
gateway=$(route -n | grep "^192" | awk '{print $2}')

echo "$lo ip address is  $lo_ip."
echo "$eth1 ip address is $eth1_ip."
echo "default gateway is  $gateway."
[root@liupeng liupeng]# 

7.2 练习二

1.只显示/boot目录下所有对象的实际空间,并按由小到大排序;
2.统计/etc/passwd中每种shell的被使用次数;
3.用fdisk -l命令只显示出分区和文件系统的类型;
4.统计/etc/passwd文件中sbin这个单词出现多少次;
5.用find命令查找/根目录下所有包含特殊权限的对象,并只显示出对象的权限和文件名称,不能显示错误提示;
6.列出前5位占MEM最多的进程的命令;
7.只显示网卡eth1的IP地址。

答案:
1.只显示/boot目录下所有对象的实际空间,并按由小到大排序;

[root@liupeng liupeng]# find /boot -exec ls -l {} \; | tr -s ' ' | cut -d' ' -f5 |grep -v ^$ |sort -n
11
11
63
63
166
512
512
801
801
1024
1024
1024
1024
1341
1341
11364
11364
11748
11748
11756
11756
11956
11956
12024
12024
12288
12620
12620
13268
13268
13380
13380
13964
13964
14412
14412
101820
101820
125976
125976
179157
179157
249106
249106
2341856
2341856
3986608
3986608
16180806
16180806
[root@liupeng liupeng]# 

2.统计/etc/passwd中每种shell的被使用次数;

[root@liupeng liupeng]# cat /etc/passwd | cut -d: -f7 | sort | tail -n +2 | uniq -c
      1 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     30 /sbin/nologin
      1 /sbin/shutdown
[root@liupeng liupeng]# 

3.用fdisk -l命令只显示出分区和文件系统的类型;

[root@liupeng liupeng]# fdisk -l|grep ^/dev|tr -s '*' ' '|cut -d' ' -f1,6-
/dev/sda1 Linux
/dev/sda2 Linux LVM
[root@liupeng liupeng]# 

4.统计/etc/passwd文件中sbin这个单词出现多少次;

[root@liupeng liupeng]# cat /etc/passwd|tr -s ':/' '\n'|sort|uniq -c|grep sbin
     36 sbin
[root@liupeng liupeng]# 

5.用find命令查找/根目录下所有包含特殊权限的对象,并只显示出对象的权限和文件名称,不能显示错误提示;

[root@liupeng liupeng]# find / -perm +7000 -exec ls -ld {} \; 2>/dev/null |tr -s ' ' | cut -d' ' -f1,9 | cut -c 2-10,12-
rwxr-xr-t /var/cache/gdm
rwxrwx--T /var/lib/gdm
rwxr-x--T /var/lib/gdm/.gconf.mandatory
rw-r----T /var/lib/gdm/.gconf.mandatory/%gconf-tree.xml
rw-r----T /var/lib/gdm/.gconf.path
rwxrwx--T /var/gdm
rwxrwx--T /var/log/gdm
rwxrwxrwt /var/tmp
rwxrwx--T /var/spool/cups/tmp
rws--x--x /usr/libexec/pt_chown
rwsr-xr-x/usr/libexec/pulse/proximity-helper
rwsr-xr-x/usr/libexec/polkit-1/polkit-agent-helper-1
rwsr-xr-x /usr/libexec/openssh/ssh-keysign
rwsr-xr-x/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
rwx--s--x/usr/libexec/utempter/utempter
rwx--s--x/usr/lib64/vte/gnome-pty-helper
rwsr-xr-x/usr/lib64/nspluginwrapper/plugin-config
r-s--x--- /usr/sbin/suexec
rwsr-xr-x /usr/sbin/usernetctl
rwxr-sr-x /usr/sbin/postdrop
rwxr-sr-x /usr/sbin/postqueue
rwx--s--x/usr/sbin/lockdev
rws--x--x/usr/sbin/userhelper
rwxr-sr-x /usr/bin/ssh-agent
rws--x--x /usr/bin/chfn
rws--x--x /usr/bin/chsh
--s--x--x /usr/bin/sudo
rwsr-xr-x /usr/bin/gpasswd
rwx--s--x/usr/bin/locate
rwsr-xr-x /usr/bin/crontab
rwsr-xr-x /usr/bin/newgrp
rwsr-xr-x /usr/bin/chage
--s--x--x /usr/bin/sudoedit
rwsr-xr-x/usr/bin/pkexec
rwsr-xr-x /usr/bin/passwd
rwsr-xr-x /usr/bin/at
--s--x--- /usr/bin/staprun
rws--x--x/usr/bin/Xorg
rwxr-sr-x /usr/bin/write
r-xr-sr-x/usr/bin/wall
rwsr-xr-x/usr/bin/ksu
rwsr-x---/lib64/dbus-1/dbus-daemon-launch-helper
rwxrwxrwt/dev/shm
rwsr-xr-x /sbin/unix_chkpwd
rwsr-xr-x /sbin/mount.nfs
rwsr-xr-x /sbin/pam_timestamp_check
rwxr-sr-x/sbin/netreport
rwsr-x---/bin/fusermount
rwsr-xr-x /bin/mount
rwsr-xr-x /bin/umount
rwsr-xr-x /bin/su
rwsr-xr-x /bin/ping6
rwsr-xr-x /bin/ping
rwxrwxrwt /tmp
rwxrwxrwt/tmp/.X11-unix
rwxrwxrwt/tmp/.ICE-unix
[root@liupeng liupeng]# 

6.列出前5位占MEM最多的进程的命令;

[root@liupeng liupeng]# ps aux|tr -s ' '|tail -n +2|sort -k4 -rn|head -5
root 1853 0.0 3.0 133612 30876 tty1 Ss+ 06:53 0:01 /usr/bin/Xorg :0 -nr -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-egXc4G/database -nolisten tcp vt1
root 2131 0.0 2.5 556896 25172 ? S 06:56 0:01 nautilus
root 2164 0.0 1.8 401852 18116 ? S 06:56 0:00 /usr/bin/gnote --panel-applet --oaf-activate-iid=OAFIID:GnoteApplet_Factory --oaf-ior-fd=22
root 2121 0.0 1.6 348168 16624 ? S 06:56 0:01 gnome-panel
root 2422 0.0 1.5 344472 16052 ? Sl 06:56 0:00 gnome-terminal
[root@liupeng liupeng]# 

7.只显示网卡eth1的IP地址。

[root@liupeng liupeng]# ifconfig eth1 | grep "inet addr" | tr -s ':' ' ' |cut -d' ' -f4
192.168.28.129
[root@liupeng liupeng]# 

7.3 练习三

编写脚本统计指定目录下面文件占用空间大小超过10M的文件的数目。(使用1个位置参数指定目录的绝对路径)

[root@liupeng liupeng]# sh test.sh /boot
超过10M的文件数目: 1
[root@liupeng liupeng]# sh test.sh /usr/share/
超过10M的文件数目: 7
[root@liupeng liupeng]# sh test.sh /boot
超过10M的文件数目: 1
[root@liupeng liupeng]# 

答案:

[root@liupeng liupeng]# cat test.sh 
#!/bin/bash
num=$(find $1 -size +10240k -type f -exec ls -l {} \; 2> /dev/null |wc -l )
echo "超过10M的文件数目: $num"

[root@liupeng liupeng]# 

7.4 练习四

把/etc/passwd下面的所有用户截取出来放在一个数组里。

答案:

[root@liupeng liupeng]# user_name=($(cat /etc/passwd|awk -F: '{print $1}'))
[root@liupeng liupeng]# echo ${user_name[@]}
root bin daemon adm lp sync shutdown halt mail uucp operator games gopher ftp nobody dbus usbmuxd avahi-autoipd vcsa rpc rtkit abrt haldaemon ntp apache saslauth postfix avahi rpcuser nfsnobody pulse gdm sshd tcpdump mysql
[root@liupeng liupeng]# 

原创不易,喜欢本文的话,欢迎右下角扫码wx打赏我O(∩_∩)O~
-----------END-----------

posted @ 2020-03-30 20:41  斯言甚善  阅读(850)  评论(0编辑  收藏  举报