每天2分钟学习unix/linux系统shell编程(五)grep,head,tail,wc

内容提要,今天将要学习的有grep,head,tail,wc,date,reboot,poweroff等shell命令,下面我们开始。先介绍这些基础命令,有了这些基础,后续再进行shell的语法的讲解。

一、shell命令:grep

  • 命令:grep
  • 作用:搜索文件,在指定的文件列表中搜索指定的字符串,并把匹配的行打印出来。
  • 选项:用表格来展示

选项

作用

-c

输出匹配字符串的行的统计数

-h

在多个文件中查找,不显示文件名

-n

显示匹配行以及行号

-v

显示不匹配的行

-i

不区分大小写

-l

在多个文件中查找时只显示文件名

  • 参数:带路径的文件或者文件夹。

使用示例:

oot@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  g.txt
root@debian:~/test# grep "hell" *.txt
g.txt:hello, everyone
root@debian:~/test# grep "hell" *.txt -c
b.txt:0
g.txt:1
root@debian:~/test# grep "hell" *.txt -h
hello, everyone
root@debian:~/test# grep "hell" *.txt -l
g.txt
root@debian:~/test# grep "hell" *.txt -n
g.txt:1:hello, everyone
root@debian:~/test# grep "hell" *.txt -i
g.txt:hello, everyone
root@debian:~/test# 

上面例子中有涉及到通配符,这个下节统一讲,下期要讲find命令时再讲。

二、shell命令:head,tail

  • 命令:head,tail
  • 作用:head看文件前N行,tail文件的结尾N行。
  • 选项:-n 跟上数字表示看多少行。如果是正在写入的文件使用-f选项可以实时看到文件内容。
  • 参数:带路径的文件

使用示例:

root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  g.txt
root@debian:~/test# tail -n 3 g.txt 
997
998
999
root@debian:~/test# head -n 3 g.txt 
1
2
3
root@debian:~/test#

三、shell命令:wc

  • 命令:wc
  • 作用:统计文件的行数,字数,字节数
  • 选项:-c 统计字节数 -l 统计行数 -w 统计字数
  • 参数:带路径的文件

使用示例:

root@debian:~/test# wc -l g.txt 
999 g.txt
root@debian:~/test# wc -c g.txt 
3888 g.txt
root@debian:~/test# wc -w g.txt 
999 g.txt
root@debian:~/test#

四、shell命令:date

  • 命令:date
  • 作用:显示当前计算机时间
  • 选项:无
  • 参数:无

使用示例:

root@debian:~/test# date
2022年 11月 17日 星期四 00:04:25 CST
root@debian:~/test# 

五、shell命令:reboot,poweroff

  • 命令:reboot,poweroff
  • 作用:reboot是重启,poweroff是关机,切记在生产环境不要轻易执行这两个命令,要再三确认才可以哦。
  • 选项:无
  • 参数:无

这一期先分享到这里,其它内容后续再安排。感谢大家支持。

举报
评论 0