Linux命令练习题

1.显示当前时间, 格式:2016-06-18 10:20:30

# date "+%F %T" 
2021-03-25 10:32:48

2.显示前天是星期几

# date -d "-2 day" +%A
Tuesday

3.设置当前时间为2019-08-07 06:05:10

# date 082706052021.10
Fri Aug 27 06:05:10 CST 2021
# date "+%F %T"       
2021-08-27 06:05:11

4.显示/etc/目录下所有以I开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录.

touch Ilove9uz
touch Ilove9z
touch I9z
touch Iloveuz
mkdir Ilove99uz
[root@centos8-3-node1 etc]#ls /etc/I*[[:digit:]]*[[:lower:]] -d
/etc/I9z  /etc/Ilove99uz  /etc/Ilove9uz  /etc/Ilove9z

5.显示/etc/目录下以任意一位数字开头,且以非数字结尾的文件或目录列表


[root@centos8-3-node1 etc]#touch 3t3 4w4 5q5  5q5ls
[root@centos8-3-node1 etc]#mkdir 6666 777w
[root@centos8-3-node1 etc]#ls -d /etc/[[:digit:]]*[^[:digit:]]
/etc/5q5ls  /etc/777w

6.显示/etc目录下以非字母开头,后面跟了一个字母及其任意长度字符的文件或目录列表

[root@centos8-3-node1 etc]#touch 8w8aa
[root@centos8-3-node1 etc]#mkdir 9cccc
[root@centos8-3-node1 etc]#ls -d /etc/[^[:alpha:]][[:alpha:]]*
/etc/3t3  /etc/4w4  /etc/5q5  /etc/5q5ls  /etc/8w8aa  /etc/9cccc

7.显示/etc目录下所有以rc开头的,并且后面是0-6之间的数字,其他为任意字符的文件及其目录

#ls -d /etc/rc[0-6]*
/etc/rc0.d  /etc/rc1.d  /etc/rc2.d  /etc/rc3.d  /etc/rc4.d  /etc/rc5.d  /etc/rc6.d

8.显示/etc目录下所有.conf结尾的,且以m,n,r,p开头的文件及其目录

#ls -d /etc/[mnrp]*.conf
/etc/man_db.conf  /etc/nfs.conf       /etc/pbm2ppa.conf  /etc/request-key.conf
/etc/mke2fs.conf  /etc/nfsmount.conf  /etc/pnm2ppa.conf  /etc/resolv.conf
/etc/mtools.conf  /etc/nsswitch.conf  /etc/radvd.conf    /etc/rsyslog.conf

9.只显示/root下的隐藏文件和目录

[root@centos8-3-node1 ~]#ls .* -d
.   .bash_history  .bash_profile  .cache   .cshrc  .lesshst  .viminfo
..  .bash_logout   .bashrc        .config  .dbus   .tcshrc

10.只显示/etc下非隐藏目录

[root@centos8-3-node1 etc]#ls  */ -d

11.每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为 backupYYYY-mm-dd,备份过程可见

cp -av /etc/ /data/backup`date +%F`

12.如何创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a,/testdir/dir1/y/b

[root@centos8-3-node1 data]#
[root@centos8-3-node1 data]#mkdir -p /testdir/dir1/{x,y}/{a,b}
[root@centos8-3-node1 data]#tree -d /testdir/
/testdir/
└── dir1
    ├── x
    │   ├── a
    │   └── b
    └── y
        ├── a
        └── b

13.如何创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b

[root@centos8-3-node1 data]#mkdir -p /testdir/dir2/x/{a,b} /testdir/dir2/y
[root@centos8-3-node1 data]#tree /testdir/dir2
/testdir/dir2
├── x
│   ├── a
│   └── b
└── y

14.如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7

[root@centos8-3-node1 data]#mkdir -p /testdir/dir{3..5} /testdir/dir5/dir{6..7}
[root@centos8-3-node1 data]#tree /testdir/
/testdir/
├── dir1
│   ├── x
│   │   ├── a
│   │   └── b
│   └── y
│       ├── a
│       └── b
├── dir2
│   ├── x
│   │   ├── a
│   │   └── b
│   └── y
├── dir3
├── dir4
└── dir5
    ├── dir6
    └── dir7

15.将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

	tr 'a-z' 'A-Z' </etc/issue >>/tmp/issue.out

16.将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

	 who|tr 'a-z' 'A-Z' >/tmp/who.out

17.一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am用户名,The system version is here,please help me to check it ,thanks!操作系统版本信息

# mail -s help root <
Hello, I am `whoami`,The system version is here,please help me to check it ,thanks!
`cat /etc/redhat-release`
EOF

18.将/root/下文件列表,显示成一行,并文件名之间用空格隔开

ls /root/|tr '\n' ' '

19.处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格

echo 'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4'|tr -dc '[:digit:][:blank:]'  

20.将PATH变量每个目录显示在独立的一行

echo $PATH|tr ':' '\n'

21.将指定文件中0-9分别替代成a-j

echo 'xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4'|tr -dc '[:digit:][:blank:]'| tr '0-9' 'a-j'

22.将文件/etc/centos-release中每个单词(由字母组成)显示在独立一行,并无空行

# cat /etc/centos-release | tr " " "\n"
CentOS
Linux
release
7.9.2009
(Core)

23.设置用户下次登录必须改密码。

change -d 0 username #实际就是将/etc/shadow里面的最近一次密码修改时间改为0
passwd -e username	#设置用户密码过期,这个命令也行

24.ubuntu无交互式修改密码方式

ubuntu的passwd没有–stdin选项,需换成下面这个命令进行无交互式修改。

echo wang:123456 | chpasswd

25.当用户docker对/testdir 目录无执行权限时,意味着无法做哪些操作?

无法cd进入到目录

26.当用户mongodb对/testdir 目录无读权限时,意味着无法做哪些操作?

无法通过ls查看该目录下存在哪些文件,如果有执行权限并且知道该目录下存在哪些文件,那么可以直接输入对应文件名查看。

27.当用户redis 对/testdir 目录无写权限时,该目录下的只读文件file1是否可修改和删除?

修改看文件本身权限。无法修改
删除看文件的目录是否有w权限,这里无法删除

28.当用户zabbix对/testdir 目录有写和执行权限时,该目录下的只读文件file1是否可修改和删除?

无法修改,可以删除

29.复制/etc/fstab文件到/var/tmp下,设置文件所有者为tomcat读写权限,所属组为apps组有读写
权限,其他人无权限

 1092  cp -a /etc/fstab /var/tmp/
 1093  chown tomcat:apps fstab 
 1094  chmod 660 fstab 

30.误删除了用户git的家目录,请重建并恢复该用户家目录及相应的权限属性

cd /home/
cp -a /etc/skel/ git
chown -R git:git git/
chmod 700 git/

31.在/testdir/dir里创建的新文件自动属于webs组,组apps的成员如:tomcat能对这些新文件有读写权限,组dbs的成员如:mysql只能对新文件有读权限,其它用户(不属于webs,apps,dbs)不能访问这个文件夹

[root@centos8-3-node1 ~]#mkdir -p /testdir/dir
[root@centos8-3-node1 ~]#ll /testdir/dir -d
drwxr-xr-x 2 root root 6 Mar 29 21:11 /testdir/dir
[root@centos8-3-node1 ~]#groupadd apps
[root@centos8-3-node1 ~]#groupadd webs
[root@centos8-3-node1 ~]#groupadd dbs
[root@centos8-3-node1 ~]#useradd -G dbs mysql  
[root@centos8-3-node1 ~]#useradd -G apps tomcat
[root@centos8-3-node1 ~]#chown :webs /testdir/dir   
[root@centos8-3-node1 ~]#chmod g+s /testdir/dir   
[root@centos8-3-node1 ~]#ll -d /testdir/dir
drwxr-sr-x 2 root webs 6 Mar 29 21:11 /testdir/dir	
[root@centos8-3-node1 ~]# setfacl -R -m g:apps:rwx /testdir/dir
[root@centos8-3-node1 ~]#setfacl -R -m g:dbs:rx /testdir/dir/
[root@centos8-3-node1 ~]#chmod -R 770 /testdir/dir/ 
[root@centos8-3-node1 ~]#getfacl /testdir/dir  
getfacl: Removing leading '/' from absolute path names
# file: testdir/dir/
# owner: root
# group: webs
# flags: -s-
user::rwx
group::r-x
group:apps:rwx
group:dbs:r-x
mask::rwx
other::---

32.误将 /bin/chmod 文件的执行权限删除,如何恢复?

[root@centos8-3-node1 ~]#ll `which chmod`
-rwxr-xr-x. 1 root root 63880 Apr 27  2020 /usr/bin/chmod
[root@centos8-3-node1 ~]#chmod a-x /usr/bin/chmod 
[root@centos8-3-node1 ~]#
[root@centos8-3-node1 ~]#chmod 777 /usr/bin/chmod 
-bash: /usr/bin/chmod: Permission denied
[root@centos8-3-node1 ~]#setfacl -m u:root:rwx /usr/bin/chmod
[root@centos8-3-node1 ~]#ll /usr/bin/chmod 
-rw-rwxr--+ 1 root root 63880 Apr 27  2020 /usr/bin/chmod
[root@centos8-3-node1 ~]#chmod +x /usr/bin/chmod 
[root@centos8-3-node1 ~]#ll /usr/bin/chmod
-rwxrwxr-x+ 1 root root 63880 Apr 27  2020 /usr/bin/chmod
[root@centos8-3-node1 ~]#getfacl /usr/bin/chmod
getfacl: Removing leading '/' from absolute path names
# file: usr/bin/chmod
# owner: root
# group: root
user::rwx
user:root:rwx
group::r--
mask::rwx
other::r-x
[root@centos8-3-node1 ~]#setfacl -x u:root /usr/bin/chmod 
[root@centos8-3-node1 ~]#getfacl /usr/bin/chmod           
getfacl: Removing leading '/' from absolute path names
# file: usr/bin/chmod
# owner: root
# group: root
user::rwx
group::r--
mask::r--
other::r-x

33.在vim中设置tab缩进为4个字符

vim /etc/vimrc
set ts=4  #tabstop可简写为ts

34.复制/etc/rc.d/init.d/functions文件至/tmp目录,替换/tmp/functions文件中的/etc/sysconfig/init为/var/log

cp -a /etc/rc.d/init.d/functions /tmp/
vi /tmp/functions 
:%s#/etc/sysconfig/init#/var/log#g

35.删除/tmp/functions文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号

:%g/^#[[:space:]]+*/d 

36.统计日志访问量,IP访问次数前3名

# cat nginx.access.log-20200428 |cut -d" " -f1|sort|uniq -c|sort -nr|head -3
   5498 122.51.38.20
   2161 117.157.173.214
    953 211.159.177.120

37.查找ifconfig结果中本机的IPV4地址.

# ifconfig|head -n2 |tail -1|tr -s " "|cut -d" " -f3
10.0.0.150

38.查找分区使用率的最大分区比

# df|tail -n +2|tr -s " " | df|tail -n +2|tr -s " "|cut -d" " -f5|tr -d "%"|sort -nr|head -1
21

39.查找用户UID最大值的用户名,UID及shell类型

# cut -d: -f1,3,7 /etc/passwd|sort -t: -k2 -nr|head -1
nobody:65534:/sbin/nologin

# cat /etc/passwd | awk -F":" 'BEGIN{maxuid=0;}{maxuid<$3?maxuid=$3:maxuid=maxuid}END{ system("grep "maxuid" /etc/passwd |cut -d: -f1,3,7")}' 
nobody:65534:/sbin/nologin

40.查出/tmp的权限,以数字形式展示

# stat  /tmp |head -n4|tail -n1 |cut -d"/" -f1|cut -d"(" -f2 
1777
# stat /tmp | awk -F"[ (:/]" 'NR==4{print $4}'
1777
# stat /tmp | sed -En '4s#.*\(([0-9]+)/.*#\1#p'
1777

41.统计当前连接本机的每个远程主机IP的链接数,并按从大到小排序。

# ss -nt |tail -n +2 |sort -t" " -k5|tr -s " "|cut -d" " -f 5|cut -d: -f1|uniq -c|sort -nr
  3 10.0.0.151
  1 10.0.0.1
# ss -tn | awk -F"[ :]+" '!/^State/{ips[$6]++}END{for(ip in ips){print ips[ip],ip}}' | sort -nr
  3 10.0.0.151
  1 10.0.0.1

42.显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

# grep '^s\|^S' /proc/meminfo
# grep '^[sS]' /proc/meminfo
# grep '^(s|S)' /proc/meminfo
# awk '/^[sS]/{print $0}' /proc/meminfo 

43.显示/etc/passwd文件中不以/bin/bash结尾的行

# grep -v '/bin/bash$' /etc/passwd
# awk '!/\/bin\/bash$/{print $0}' /etc/passwd

44.显示用户rpc默认的shell程序

# cat /etc/passwd|grep 'rpc:'|cut -d: -f7
# cat /etc/passwd|grep 'rpc\>'|cut -d: -f7

45.找出/etc/passwd中的两位或三位数

cat /etc/passwd|grep  '\b[0-9]\{2,3\}\b' 

46.显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行

cat c.cfg |grep '^[[:space:]]\+[^[:space:]]'
cat /etc/grub2.cfg |grep -E "^[[:space:]]+[^[:space:]]+"

47.找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

cat a.txt | grep 'LISTEN[[:space:]]*' 

48.显示CentOS7上所有UID小于1000以内的用户名和UID

# cat /etc/passwd| cut -d: -f1,3|grep '\b[0-9]\{1,3\}\b'
# awk -F: '$3<1000{print $1,$3}' /etc/passwd 

49.添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行

# cat /etc/passwd |grep "^\(.*\).*/\1$" 
# cat /etc/passwd|grep -E "^(.*)\>.*\1$"

50.利用df和grep,取出磁盘各分区利用率,并从大到小排序

# df |grep -E "^\/dev\/sda"|grep -o '[[:digit:]]\{1,\}%'|tr -d '%'|sort -nr
# df |grep -E "^\/dev\/sda"|grep -Eo "[[:digit:]]+%"|grep -Eo "[0-9]+"|sort -nr

51.显示三个用户root、mage、wang的UID和默认shell

# grep -E "^(root|wang|mage)" /etc/passwd
root:x:0:0:root:/root:/bin/bash
mage:x:1022:1027::/home/mage:/bin/bash
wang:x:1023:1028::/home/wang:/bin/bash
rooter:x:1024:1029::/home/rooter:/bin/bash
# grep -E "^(root|wang|mage)\>" /etc/passwd
root:x:0:0:root:/root:/bin/bash
mage:x:1022:1027::/home/mage:/bin/bash
wang:x:1023:1028::/home/wang:/bin/bash

52.找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

# cat /etc/rc.d/init.d/functions |grep -E "^\w.*\>\(\)"

53.使用egrep取出/etc/rc.d/init.d/functions中其基名

# echo "/etc/rc.d/initd./functions" |grep -Eo "\w+$"
# echo "/etc/rc.d/init.d/functions" | egrep -o [^/]+$
# echo /etc/rc.d/init.d/functions | sed -En 's#.*/(.*)#\1#p'

54.使用egrep取出上面路径的目录名

# echo "/etc/rc.d/init.d/functions" |grep -Eo "^.*/" 
/etc/rc.d/init.d/
# echo /etc/rc.d/init.d/functions | sed -En 's#(.*)/.*#\1#p' 
/etc/rc.d/init.d

55.统计last命令中以root登录的每个主机IP地址登录次数

# last|grep "^root\>"|tr -s " " %|cut -d% -f3|sort|uniq -c
      2 10.0.0.1
      1 10.0.0.151
      1 tty2
# last|grep "^root\>"|grep -Eo "((1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])"|sort |uniq -c
      2 10.0.0.1
      1 10.0.0.151      

56.利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255

echo 1.9.13.199 | grep -E "\<[0-9]\>"        	  # 0-9
echo 1.66.13.199 | grep -E "\<[1-9][0-9]\>" 	  # 10-99
echo 1.66.13.199 | grep -E "\<1[0-9][0-9]\>" 	  # 100-19
echo 224.266.23.249 | grep -E "\<2[0-4][0-9]\>"   # 200-249
echo 254.266.23.259. | grep -E "\<25[0-5]\>"  	  # 250-255
 

57.显示ifconfig命令结果中所有IPv4地址

ifconfig | grep -Eo "(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"

58.将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

echo "welcome to magedu linux" |grep -o '.'|sort|uniq -c|sort -nr

59.使用grep找出两个文件的相同内容 a.txt,b.txt

root@ubuntu1804:/data# vi a.txt 
a
c 
d
2
oo
o
f
t
root@ubuntu1804:/data# cat b.txt 
a
b
c
d
1
2
34
4
3
root@ubuntu1804:/data# grep -f a.txt b.txt 
a
c
d
2

60.使用sed命令不显示/etc/fstab以#号和空行

sed -n '/^\w/p' /data/fstab  #-n不默认打印模式空间数据文件全部内容,p打印模式空间匹配的数据
sed  '/^$/d;/^#/d' /data/fstab  #不带-n,默认显示模式空间处理后的数据

61.匹配/etc/default/grub文件中GRUB_CMDLINE_LINUX,在其值后面增加
net.ifnames=0,并将源文件备份为.bak后缀

# sed -En '/^GRUB_CMDLINE_LINUX/p' /etc/default/grub  
#先将该行查出,查出该行后使用s/old/new替换的语法

#sed -En '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1 net.ifnames=0\2/p' /etc/default/grub 
GRUB_CMDLINE_LINUX="resume=UUID=a8f213bd-c09b-4dca-b9bb-84449307c75e rhgb quiet net.ifnames=0"
#如果直接在此命令基础上加-i.bak 会只将匹配的行写到文件,新文件只有匹配的一行内容


#sed -Ei.bak '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1net.ifnames=0\2/' /etc/default/grub 
#备份使用i.bak  将-n去掉,使其输出处理过后的内容,将模式后p取掉,不需要在打印一遍了。也可以将p换成g进行全局替换,但此处只改一个,空着即可。

62.sed取本机IP地址

[root@centos8-3-node1 data]#ifconfig ens33 |sed -En '2s/[^0-9]+ ([0-9.]+) .*/\1/p'
10.0.0.150

[root@centos8-3-node1 data]#ifconfig ens33 |sed -En '2s/.* ([0-9.]+) .*/\1/p'
255.255.255.0 #前面使用.*贪婪匹配,会匹配到最后一个符合其他条件的展示,这里匹配到了掩码

[root@centos8-3-node1 data]#ifconfig ens33 |sed -En '2s/.* ([0-9.]+).*/\1/p'
10.0.0.255  #这里匹配到了广播IP,广播IP后面没有空格,所以能匹配到。

[root@centos8-3-node1 data]#ifconfig ens33 | sed -n '2s/^.*inet //p'|sed -n 's/netmask.*//p'|cat -A
10.0.0.150  $    #这种方法看到后面还有空格

[root@centos8-3-node1 data]#ifconfig ens33 | sed -En '2s/(^.*inet )([0-9].*)(netmask.*)/\2/p'|cat -A
10.0.0.150  $ # 这种方法看到后面还有空格

[root@centos8-3-node1 data]#ifconfig ens33 | sed -En '2s/(^.*inet )([0-9.]+)([[:space:]]+netmask.*)/\2/p'|cat -A
10.0.0.150$   # 空格交给第三组匹配。

63.sed取基名

[root@centos8-3-node1 data]#echo /etc/sysconfig/network-scripts/|sed -En 's/.*\/(.*)\/$/\1/p'
network-scripts

64.sed取目录名

[root@centos8-3-node1 data]#echo /etc/sysconfig/network-scripts/|sed -En 's/(.*)\/(.*)\/$/\1/p'
/etc/sysconfig

65.sed取文件的前缀和后缀

[root@centos8-3-node1 data]#echo a.b.c.gz | sed -En 's#(.*)\..*$#\1#p'
a.b.c
[root@centos8-3-node1 data]#echo a.b.c.gz | sed -En 's#(.*)\.(.*)$#\2#p'
gz

66.将#开头的行删除#

sed -Ei.bak 's/^#//' /etc/fstab 

67.删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符

sed -E 's/^[[:space:]]+//' grub2.cfg 

68.删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符

sed -E 's/^#[[:space:]]+//' /etc/fstab 

69.在centos6系统/root/install.log每一行行首增加#号

# sed -Ei.bak 's/(.*)/#\1/g' install.log  

70.在/etc/fstab文件中不以#开头的行的行首增加#号

 sed -r 's/^(^#*)/#/' /etc/fstab
 sed -E 's/^([^#].*)/#\1/' /etc/fstab 
 sed -E 's/^[^#].*/#&/' /etc/fstab   

71.统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)

sed -r 's@[^[:alpha:]]+@\n@g' /etc/init.d/functions |sort |uniq -c |sort -n
grep -Eo '[[:alpha:]]+' /etc/init.d/functions |sort |uniq -c |sort -nr | less

72.将文本文件的n和n+1行合并为一行,n为奇数行

N:读取匹配到的行的下一行追加至模式空间

sed先读取一行数据,aasd不符合匹配数据,然后继续读取下一行,直到a1,将1连同后面的换行\n替换为空,将下一行a2拿到模式空间 ,模式空间现在是aa2
然后sed继续读入一行数据处理,a3 ,a3后面也有换行,sed匹配替换 成功,那么继续将a3的下一行a4读入,此时模式空间数据是
aa2
aa4
依次类推

echo -e "aasd\nsssd\nafg\nbba">a.txt
seq 100>> a.txt
sed -Ei 's/^[^a]/a&/g' a.txt
[root@centos8 data]# cat a.txt| sed -E 'N;s/[0-9]+\n//g' 
aasd
asssd
afg
abba
aa2
aa4
aa6
aa8
aa10
aa12
aa14
aa16
aa18
aa20
aa22
aa24
aa26
aa28
aa30
aa32
aa34
aa36
aa38
aa40
aa42
aa44
aa46
aa48
aa50
aa52
aa54
aa56
aa58
aa60
aa62
aa64
aa66
aa68
aa70
aa72
aa74
aa76
aa78
aa80
aa82
aa84
aa86
aa88
aa90
aa92
aa94
aa96
aa98
aa100

73.var目录下属主为root,且属组为mail的所有文件

find /var/ -user root -a -group mail -ls

74.var目录下不属于root、lp、gdm的所有文件

 find /var/ ! -user root -a ! -user lp -a ! -user gdm  -ls
  find /var/ ! \( -user root -o -user lp -o -user gdm \) -ls

75.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件

find /var -mtime -7 -a ! -user root -a ! -user postfix -type f
find /var/ -mtime -7 -a ! \( -user root -o -user postfix \) -type f 

76.查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件

 find / \( -nouser -o -nogroup \) -a -atime -7 -type f

77.查找etc目录下大于1M且类型为普通文件的所有文件

 find /etc -size +1M -type f -ls

78.查找etc目录下所有用户都没有写权限的文件

find /etc  ! -perm /222 -type f -ls

79.查找etc目录下至少有一类用户没有执行权限的文件

find /etc/  ! -perm -111 -type f -ls

80.查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件

find /etc/init.d/ -perm -113 -type f

81.编写脚本argsnum.sh,接受一个文件路径作为参数; 如果参数个数小于1,则提示用户"至少应该给一个参数",并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

#!/bin/bash
FILE_PATH=$1
if [ $# -le 0 ];then
    echo "至少应该给一个参数"
    exit 2
fi
sed -En '/^$/p' $1 |wc -l

82.编写脚本hostping.sh,接受一个主机的IPv4地址做为参数, 测试是否可连通。如果能ping通, 则提示用户“该IP地址可访问";如果不可ping通, 则提示用户“该IP地址不可访问"

#!/bin/bash
IPV4_ADDR=$1
[ -z $IPV4_ADDR ] && { echo "请输入一个ipv4地址";exit; } || { ping -c1 -W1 $1 &>/dev/null
 && echo "该IP地址可访问"||echo "该IP地址不可达"; }

83.编写脚本checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%, 就发广播警告空间将满.

#!/bin/bash
DISK_USE_RATE=`df | sed -n '2,$p'|awk '{print $5}'|tr -d '%' |sort -nr|head -1`
INODE_USE_RATE=` df -i| sed -n '2,$p'|awk '{print $5}'|tr -d '%' |sort -nr | head -1`
echo DISK_USE_RATE=$DISK_USE_RATE
echo INODE_USE_RATE=$INODE_USE_RATE

if [ "$DISK_USE_RATE" -ge 80 -o "$INODE_USE_RATE" -ge 80 ];then
    for i in `w | tail -n +3 |awk '{print $2}'`;do
        echo "Warning!Disk will be full.." >> /dev/$i
    done

fi

84.编写脚本per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

#!/bin/bash
FILE=$1
[ -z $FILE ]&&{ echo "请输入一个文件";exit; }||\
        ([ ! -r $FILE -a ! -w $FILE ] && echo "当前用户对该文件不可读且不可写" || echo ">
有权限")
     

85.编写脚本excute.sh,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限

#!/bin/bash
FILE=$1
[ -z $FILE ] && { echo "请输入一个文件";exit; } ||\
        ([[ $FILE == *.sh ]] && { chmod +x $FILE;echo "加执行权限.."; } ||echo "不是sh文件")

86.编写脚本nologin.sh和login.sh,实现禁止和允许普通用户登录系统
nologin.sh

#!/bin/bash
read -p "请输入禁止登录的用户" username
uid=`id $username|cut -d" " -f1|grep -Eo '[0-9]+'`
[ -z $uid ] && { echo "您输入的用户不存在";exit; }||\
( [ $uid -le 500 ] && { echo "系统用户无法禁止";exit; } || { usermod -s /sbin/nologin $username;echo "该用户已禁止登录"; })

login.sh

#!/bin/bash
read -p "请输入允许登录的用户" username
uid=`id $username|cut -d" " -f1|grep -Eo '[0-9]+'`
[ -z $uid ] && { echo "您输入的用户不存在";exit; }||\
( [ $uid -le 500 ] && { echo "该用户为系统用户";exit; } || { usermod -s /bin/bash $username;echo "该用户已允许登录"; })

87.1、编写脚本createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之.并设置初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,如果没有参数,就提示:请输入用户名

#!/bin/bash
read -p "请输入要创建用户的用户名:" USERNAME

id $USERNAME &>/dev/null
TMP_VB=$?
if [ -z $USERNAME ];then
    echo "请输入用户名"
    exit 1
elif [ $TMP_VB -eq 0 ];then
    echo "该用户已存在"
    exit 1
else
    useradd $USERNAME
    echo 123456 | passwd --stdin $USERNAME &>/dev/null
    passwd -e $USERNAME &>/dev/null
    echo "创建成功!该用户的UID为:`id -u $USERNAME`"
fi

88.编写脚本yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息

#!/bin/bash
read -p "Are you rich?,please input yes or no :" ANSWER

ANSWER=`echo $ANSWER|tr [A-Z] [a-z]`
case $ANSWER in
y|yes)
    echo "Yes,You are rich!"
    ;;
n|no)
    echo "Good Good Study,Day Day Up"
    ;;
*)
    echo "You wrong!"
    ;;
esac

89.编写脚本filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型

read -p "Please input file path,return the file type: " FILE_PATH

FILE_TYPE=`ls -l -d $FILE_PATH 2>/dev/null|head -c1`

case $FILE_TYPE in
-)
    echo "这是一个文本文件"
    ;;
c)
    echo "这是一个字符设备文件"
    ;;

b)
    echo "这是一个块设备文件"
    ;;
l)
    echo "这是一个链接文件"
    ;;

d)
    echo "这是一个目录文件"
    ;;
p)
    echo "这是一个管道文件"
    ;;
s)
    echo "这是一个socket文件"
    ;;
*)
    echo "其他类型文件"
    ;;
esac

90.编写脚本Checkint.sh,判断用户输入的参数是否为正整数

#!/bin/bash

if [[ $1 =~ ^[0-9]+$  ]];then
    echo "是正整数"
else
    echo "不是正整数"
fi
~    

91.编写脚本,利用变量随机生成10个随机数字,输出这个1 0数字,并显示其中的最大值和最小值


MAXNUM=0
MINNUM=32767
RANDOMNUM=0
i=0
while ((i<10));do
    RANDOMNUM=$RANDOM
    if [ $RANDOMNUM -gt $MAXNUM ];then
        MAXNUM=$RANDOMNUM
    elif [ $RANDOMNUM -lt $MINNUM ];then
        MINNUM=$RANDOMNUM
    fi
    i=$[i+1]
done
echo "最大值:$MAXNUM"
echo "最小值:$MINNUM"

92.打印国际象棋

#!/bin/bash
i=0
j=0
while ((i<8));do
    while ((j<8));do
        if [ $[j%2] -eq 1 ];then
            if [ $[$i%2] -eq 1 ];then
                echo -en "\e[47m  \e[0m" 
            else
                echo -en "\e[41m  \e[0m" 
            fi

        else
            if [ $[$i%2] -eq 1 ];then
                echo -en "\e[41m  \e[0m" 
            else
                echo -en "\e[47m  \e[0m" 
            fi
        fi
        j=$[j+1]
    done
    echo 
    j=0
    i=$[i+1]
done
for i in {1..8};do
        temp1=$[ $i % 2 ]
        for j in {1..8};do
       		temp2=$[ $j % 2 ]
	        if [ $temp1 -eq  $temp2  ];then
	                echo -e -n "\033[47m  \033[0m"
	        else
	                echo -e -n "\033[41m  \033[0m"
	        fi
        done
        echo 
done

93。#有一个大与2K的二进制文件filea.现在想从第64个字节位置开始读取,需要读取的大小是128 BytS.又有文件B,想把上面读取到的128 Bytes写到第32个字节开始的位置,替换128 Bytes,实现如下

dd if=/filea of=/fileb bs=1 count=128 skip=63 seek=31 conv=notrunc

94.创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录

[root@maple ~]# fdisk /dev/sda
[root@maple ~]# pvcreate /dev/sda{6,7}
[root@maple ~]# vgcreate testvg -s 16M /dev/sda{6,7}   
[root@maple ~]# lvcreate -n testlv -L 5G testvg
[root@maple ~]# mkfs.xfs /dev/testvg/testlv 
[root@maple ~]# mkdir /users
[root@maple ~]# mount /dev/testvg/testlv /users/

95.新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录

[root@maple ~]# useradd archlinux -d /users/archlinux
[root@maple ~]# su - archlinux
[archlinux@maple ~]$ cp -a /etc/pam.d/ .

96.扩展testlv至7G,要求archlinux用户的文件不能丢失

[root@maple ~]# lvextend -r -L +2G /dev/testvg/testlv 

97.收缩testlv至3G,要求archlinux用户的文件不能丢失

# xfs文件系统缩减
yum install -y xfsdump  #备份逻辑卷的lv
xfsdump -f data.img /users
umount /users
lvreduce -L 5G /dev/testvg/testlv #缩减
mkfs.xfs -f /dev/testvg/testlv  #强制重新格式化
mount /dev/testvg/testlv /users/ #挂载
xfsrestore -f data.img /users/	#恢复
# ext文件系统缩减
[root@maple ~]# umount /dev/testvg/testlv 
[root@maple ~]# e2fsck -f /dev/testvg/testlv 
[root@maple ~]# resize2fs /dev/testvg/testlv 5G
[root@maple ~]# lvreduce -L 5G /dev/testvg/testlv 
[root@maple ~]# mount /dev/testvg/testlv /users/

98.对testlv创建快照,并尝试基于快照备份数据,验证快照的功能

[root@maple users]# cd /users/pam.d/
[root@maple pam.d]# ll
total 92
-rw-r--r--. 1 root root 232 Jun 15  2020 config-util
-rw-r--r--. 1 root root 328 Nov  8  2019 crond
-rw-r--r--. 1 root root 701 Jun 15  2020 fingerprint-auth
-rw-r--r--. 1 root root 715 Jul 21  2020 login
-rw-r--r--. 1 root root 154 Jun 15  2020 other
-rw-r--r--. 1 root root 168 Apr  6  2020 passwd
-rw-r--r--. 1 root root 760 Jun 15  2020 password-auth
-rw-r--r--. 1 root root 155 Apr  9  2020 polkit-1
-rw-r--r--. 1 root root 398 Jun 15  2020 postlogin
-rw-r--r--. 1 root root 640 Jul 21  2020 remote
-rw-r--r--. 1 root root 143 Jul 21  2020 runuser
-rw-r--r--. 1 root root 138 Jul 21  2020 runuser-l
-rw-r--r--. 1 root root 743 Jun 15  2020 smartcard-auth
-rw-r--r--. 1 root root 727 Apr 26  2020 sshd
-rw-r--r--. 1 root root 214 Sep 17  2020 sssd-shadowutils
-rw-r--r--. 1 root root 566 Jul 21  2020 su
-rw-r--r--. 1 root root 154 May 18  2020 sudo
-rw-r--r--. 1 root root 178 May 18  2020 sudo-i
-rw-r--r--. 1 root root 137 Jul 21  2020 su-l
-rw-r--r--. 1 root root 760 Jun 15  2020 system-auth
-rw-r--r--. 1 root root 248 Sep  2  2020 systemd-user
-rw-r--r--. 1 root root  84 Jul 20  2020 vlock
-rw-r--r--. 1 root root 159 Jul 21  2020 vmtoolsd
[root@maple pam.d]# rm -rf s*
[root@maple pam.d]# touch f1.txt
[root@maple pam.d]# echo asd11111  > f1.txt
[root@maple ~]# umount /dev/testvg/testlv
[root@maple ~]# 
[root@maple ~]# lvconvert --merge /dev/testvg/testlv-snapshot 
  Merging of volume testvg/testlv-snapshot started.
  testvg/testlv: Merged: 100.00%
[root@maple ~]# mount /dev/testvg/testlv /users/
[root@maple ~]# cd /users/
[root@maple users]# ll
total 20
drwx------  2 root root 16384 Apr 12 21:31 lost+found
drwxr-xr-x. 2 root root  4096 Apr  7 08:27 pam.d
[root@maple users]# cd pam.d/
[root@maple pam.d]# ll
total 92
-rw-r--r--. 1 root root 232 Jun 15  2020 config-util
-rw-r--r--. 1 root root 328 Nov  8  2019 crond
-rw-r--r--. 1 root root 701 Jun 15  2020 fingerprint-auth
-rw-r--r--. 1 root root 715 Jul 21  2020 login
-rw-r--r--. 1 root root 154 Jun 15  2020 other
-rw-r--r--. 1 root root 168 Apr  6  2020 passwd
-rw-r--r--. 1 root root 760 Jun 15  2020 password-auth
-rw-r--r--. 1 root root 155 Apr  9  2020 polkit-1
-rw-r--r--. 1 root root 398 Jun 15  2020 postlogin
-rw-r--r--. 1 root root 640 Jul 21  2020 remote
-rw-r--r--. 1 root root 143 Jul 21  2020 runuser
-rw-r--r--. 1 root root 138 Jul 21  2020 runuser-l
-rw-r--r--. 1 root root 743 Jun 15  2020 smartcard-auth
-rw-r--r--. 1 root root 727 Apr 26  2020 sshd
-rw-r--r--. 1 root root 214 Sep 17  2020 sssd-shadowutils
-rw-r--r--. 1 root root 566 Jul 21  2020 su
-rw-r--r--. 1 root root 154 May 18  2020 sudo
-rw-r--r--. 1 root root 178 May 18  2020 sudo-i
-rw-r--r--. 1 root root 137 Jul 21  2020 su-l
-rw-r--r--. 1 root root 760 Jun 15  2020 system-auth
-rw-r--r--. 1 root root 248 Sep  2  2020 systemd-user
-rw-r--r--. 1 root root  84 Jul 20  2020 vlock
-rw-r--r--. 1 root root 159 Jul 21  2020 vmtoolsd

99.编写服务脚本/root/bin/testsrv.sh,完成如下要求
(1) 脚本可接受参数:start, stop, restart, status
(2) 如果参数非此四者之一,提示使用格式后报错退出
(3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功”
考虑:如果事先已经启动过一次,该如何处理?
(4) 如是stop:则删除/var/lock/subsys/SCRIPT_NAME, 并显示“停止完成”
考虑:如果事先已然停止过了,该如何处理?
(5) 如是restart,则先stop, 再start
考虑:如果本来没有start,如何处理?
(6) 如是status, 则如果/var/lock/subsys/SCRIPT_NAME文件存在,则显示“SCRIPT_NAME is
running…”,如果/var/lock/subsys/SCRIPT_NAME文件不存在,则显示“SCRIPT_NAME is
stopped…
实现打印绿色OK和红色FAILED

#!/bin/bash
# 
#####################################################################
#
#Author:                LiangDong
#Email:                 [email protected]
#Date:                  2021-04-17
#FileName:             printok.sh
#URL:                   https://github.com/ledrsnet
#Description:          
#Copyright (C):         2021 All rights reserved
#
#####################################################################
RED="\e[1;31m"
GREEN="\e[1;32m"
END="\e[0m"
WEIGHT=100
ALIGN="-" # - align left; "" align right


getOkFailed(){
local tempvar=`echo $2|tr 'A-Z' 'a-z'`
local length=${#1}
case $tempvar in
ok)
    if [ $length -ge 100 ]; then
        echo "$1"
        printf "%${ALIGN}${WEIGHT}s" && printf "[$GREEN  OK  $END]"
    else
        printf "%${ALIGN}${WEIGHT}s" "$1" && printf "[$GREEN  OK  $END]"
    fi
    echo
    ;;

failed)
    if [ $length -ge 100 ]; then
        echo "$1"
        printf "%${ALIGN}${WEIGHT}s" && printf "[${RED}FAILED$END]"
    else
        printf "%${ALIGN}${WEIGHT}s" "$1" && printf "[${RED}FAILED$END]"
    fi
    echo
    ;;
*)
    echo "Usage: `basename $0` ok|failed,return string with color."
    ;;
esac
}
#!/bin/bash
# 
#####################################################################
#
#Author:                LiangDong
#Email:                 [email protected]
#Date:                  2021-04-17
#FileName:             bin/testsrv.sh
#URL:                   https://github.com/ledrsnet
#Description:          
#Copyright (C):         2021 All rights reserved
#
#####################################################################
. printok.sh

PRO_FILE="/var/local/subsys/test"
start(){
 [ -f $PRO_FILE ] && getOkFailed "This Serivce is already running..." failed  || { touch $PRO_FILE&&getOkFailed  "Service is to start..." ok; }
}

stop(){
 [ -f $PRO_FILE ] && rm -rf $PRO_FILE && getOkFailed "This Service is stopping..." ok || getOkFailed "This Service is not running..." failed
}

status(){
  [ -f $PRO_FILE ] && echo "Service is running..." || echo "Service is stopped..."
}


case $1 in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
status)
    status
    ;;
*)
    echo "`basename $0` Usage: start|stop|restart|status"
    exit 2
    ;;
esac

100.编写脚本/root/bin/copycmd.sh
(1) 提示用户输入一个可执行命令名称
(2) 获取此命令所依赖到的所有库文件列表
(3) 复制命令至某目标目录(例如/mnt/sysroot)下的对应路径下
如:/bin/bash ==> /mnt/sysroot/bin/bash
/usr/bin/passwd ==> /mnt/sysroot/usr/bin/passwd
(4) 复制此命令依赖到的所有库文件至目标目录下的对应路径下: 如:/lib64/ld-linux-x86-
64.so.2 ==> /mnt/sysroot/lib64/ld-linux-x86-64.so.2
(5)每次复制完成一个命令后,不要退出,而是提示用户键入新的要复制的命令,并重复完成上述
功能;直到用户输入quit退出

#!/bin/bash
# 
#####################################################################
#
#Author:                LiangDong
#Email:                 [email protected]
#Date:                  2021-04-17
#FileName:             copycmd.sh
#URL:                   https://github.com/ledrsnet
#Description:          
#Copyright (C):         2021 All rights reserved
#
#####################################################################
. printok.sh

ROOT_PATH="/mnt/sysroot"
TEMP_PATH=""
FULL_NAME=""
while read -p "Please input one command:" cmd;do
    [ "$cmd" = "quit" ] && exit 0
    `which $cmd &>/dev/null`  || { echo "输入命令非法.";exit 2; }
    FULL_NAME=`which $cmd`
    TEMP_PATH=$ROOT_PATH$(dirname $FULL_NAME)
    if [ -d $TEMP_PATH ];then
        cp -a $FULL_NAME $TEMP_PATH/
    else
        mkdir -p $TEMP_PATH || exit 2;
        cp -a $FULL_NAME $TEMP_PATH/
    fi
    getOkFailed "$cmd command copy finished." ok
    ldd $FULL_NAME | sed -rn "s#.*(/lib[0-9]+.*[0-9]) .*#\1#p" > temp.txt || exit 2;

    while read file;do
        TEMP_PATH=$ROOT_PATH$(dirname $file)
        if [ -d $TEMP_PATH ];then
            cp -a $file $TEMP_PATH/
        else
            mkdir -p $TEMP_PATH ||exit 2;
            cp -a $file $TEMP_PATH/
        fi
    done  < temp.txt
    rm -rf temp.txt
    getOkFailed "$cmd dependency copy finished." ok
done

101.文件host_list.log 如下格式,请提取”.magedu.com”前面的主机名部分并写入到回到该文件中

 awk -F'[ .]+' '{print $2}' host_list.log >> host_list.log

102.统计/etc/fstab文件中每个文件系统类型出现的次数

# awk '/^[^# ]/{print $3}' /etc/fstab |sort|uniq -c |sort -nr
# awk '/^[^# ]/{fstype[$3]++}END{for(i in fstype){print i,fstype[i]}}' /etc/fstab

103.统计/etc/fstab文件中每个单词出现的次数

awk '/^[^# ]/{for(i=1;i<=NF;i++){words[$i]++}}END{for (i in words){print i,words[i]}}' /etc/fstab   

104.提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有数字

# cat extractNum.awk
#!/usr/bin/awk -f
{ for(i=1;i<=NF;i++){
    if($i ~ /[0-9]/){
        num=(num" "$i)
    }
  }
}END{
    print num
}

# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw"|./extractNum.awk -F "" 
 0 5 9 7 3

105.有一文件记录了1-100000之间随机的整数共5000个,存储的格式100,50,35,89…请取出其中最大和最小的整数

# for i in {1..5000};do t=`echo $RANDOM *10%99999+1|bc`;[ $i -eq 5000 ] &&  { echo -n ${t}>> randomInt.txt; } || { echo -n ${t},>> randomInt.txt; }  done

# cat maxmin.awk
#!/usr/bin/awk -f
function maxf(x,y) {
    x>y?var=x:var=y
    return var
}
function minf(x,y) {
    x<y?var=x:var=y
    return var
}

{max=$1;min=$1;
 for(i=2;i<=NF;i++){
    max=maxf(max,$i)
    min=minf(min,$i)
 }
}
END{
    print max,min

}
[root@maple-c8 ~]# ./maxmin.awk -F "," randomInt.txt 
99972 21

106.解决Dos攻击生产案例:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔5分钟。防火墙命令为:iptables -A INPUT -s IP -j REJECT

#!/usr/bin/awk -f
BEGIN {
	beg=strftime("%Y-%m-%dT%H:%M",systime()-300) ;
	#定义一个小时前的时间,并格式化日期格式
	end=strftime( "%Y-%m-%dT%H:%M",systime()) ;
	#定义结束时间
	#print beg;
	#print end;
}
$4 > beg && $4 < end { #定义取这个时间段内的日志
	count[$12]+=1;   #利用ip当做数组下标,次数当做数组内容
}
END {
	for(i in count){    #结束从数组取数据代表数组的下标,也就是ip
		if(count[i]>100) { #如果次数大于3次,做操作
			print count [i]" "i;
			system("iptables -I INPUT -S”i”j DROP" )
		}
	}
}
awk -F'"' -f check_nginx_log.awk /apps/nginx/logs/access.log

107.将以下文件内容中FQDN取出并根据其进行计数从高到低排序

[root@maple-c8 ~]# awk -F"/" '{fqdn[$3]++}END{for(i in fqdn){print fqdn[i],i}}' host_list.log |sort -nr

108.将以下文本文件awktest.txt中 以inode列为标记,对inode列相同的counts列进行累加,并且统计出同一inode中,beginnumber列中的最小值和endnumber列中的最大值

inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|

输出结果为:

106|3363120000|3368579999|30000|
310|3337000000|3362120961|10103|
311|3313460102|3362120963|39900|
#!/usr/bin/awk -f
#脚本这里面的NR!=1的判断不能放在独立的一行。另外注意awk关联数组以字符串作为索引的必须加引号,妹的,光排错了。。
NR!=1{
 if( $1 in inode){
     if(inode[$1]["beginMinNum"]>$2){
         inode[$1]["beginMinNum"]=$2
     }
     if(inode[$1]["endMaxNum"]<$3){
         inode[$1]["endMaxNum"]=$3
     }
     inode[$1]["counts"]+=$4
 }else{
    inode[$1]["beginMinNum"]=$2;
    inode[$1]["endMaxNum"]=$3;
    inode[$1]["counts"]=$4;
 }
}
END{
#   for (i in inode){
#     for(j in inode[i]){
#       print inode[i][j];
#     }   
#   }
    OFS="|";
    for(i in inode){
        print i,inode[i]["beginMinNum"],inode[i]["endMaxNum"],inode[i]["counts"]
    }
}

[root@maple-c8 ~]# awk -F "|" -f inodeawk.awk awktest.txt 
106|3363120000|3368579999|30000
310|3337000000|3362120961|10103
311|3313460102|3362120963|39900

109.单列文件,奇数行和偶数行合成两列 awk实现

seq 20 > a.txt
# sed
sed -En 'N;s/\n/ /p' a.txt 
# awk
awk '{if (NR%2==1){ORS=" ";print $0}else{ORS="\n";print $0}}' a.txt
awk '{if (NR%2==1){ ji[NR]=$0;}else{ou[NR]=$0}}END{for (i in ji){print ji[i],ou[i+1]}}' a.txt 
  1. 有 a.txt 文件,里面内容如下
    1234569
    abcABCabc
    要求使用 awk 打印出以下结果
    987654321
    cbaCBAcba
 awk -F '' '{ORS=""; for(i=1;i<=NF;NF--){ if(NF!=1){ print $NF}else{ORS="\n";print $NF;}} }'  a.txt 
987654321
cbaCBAcba

不定时持续更新中····

你可能感兴趣的:(#,Linux基础,linux,shell,centos)