Linux operation commands (6)

Linux operation commands (6)

1. Introduction to the experiment

1.1 Experiment content

This lab will introduce the usage of wc and grep commands in Linux commands.

1.2 Experimental knowledge points

  • wc command
  • grep command
  • regular expression

1.3 Experimental Environment

The experimental environment used in the course is Ubuntu Linux 14.04 64-bit version. The program will be used in the experiment:

  • Xfce Terminal

2. Experimental steps

2.1 wc command

The wc command is a statistical tool, mainly used to display the number of lines, words and bytes contained in a file.

The wc command is short for word count.

(1) Command format

wc [options] [file]

(2) Common parameters

parameter describe
-c Statistics bytes
-l count rows
-m count characters, this flag cannot be used with the -c flag
-w Counting words, a word is defined as a string separated by whitespace, tab or newline characters
-L print the length of the longest line

(3) Common examples

Example 1: To count the number of bytes, lines and characters of a file, you can use the following command:

wc -c c.txt
wc -l c.txt
wc -m c.txt

img

Note that a newline at the end of each line counts as a character, and a space counts as a character. In addition, because the system uses UTF-8 encoding, a Chinese character is 3 bytes, 9 Chinese characters plus a newline, a total of 28 bytes.

Example 2: To count the number of bytes, lines, and characters of a file, only print numbers, not file names, you can use the following commands:

cat c.txt | wc -c 
cat c.txt | wc -l 
cat c.txt | wc -m

img

Example 3: To count the number of commands in the /bin directory, you can use the following commands:

ls /bin | wc -l

2.2 grep command

grep is a very powerful command for finding matching text in a file, and can accept regular expressions and wildcards, while multiple grep command options can be used to generate output in various formats.

The way grep works is that it searches one or more files for a string template. If the template includes spaces, it must be quoted, and all strings following the template are treated as filenames. The results of the search are sent to standard output and do not affect the contents of the original file.

grep 可用于 shell 脚本,因为 grep 通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回 0,如果搜索不成功,则返回 1,如果搜索的文件不存在,则返回 2。我们利用这些返回值就可进行一些自动化的文本处理工作。

(1)命令格式

grep [选项] pattern [file]

(2)常用参数

参数 描述
-c 计算找到 '搜寻字符串'(即 pattern) 的次数
-i 忽略大小写的不同,所以大小写视为相同
-n 输出行号
-v 反向选择,打印不匹配的行
-r 递归搜索
--color=auto 将找到的关键词部分加上颜色显示

(3)常用范例

例一:将/etc/passwd 文件中出现 root 的行取出来,关键词部分加上颜色显示,可以使用如下命令:

grep "root" /etc/passwd --color=auto
cat /etc/passwd | grep "root" --color=auto

img

例二:将/etc/passwd 文件中没有出现 root 和 nologin 的行取出来,可以使用如下命令:

grep -v "root" /etc/passwd | grep -v "nologin"

img

例三:在当前目录下递归搜索文件中包含 main()的文件,经常用于查找某些函数位于哪些源代码文件中,可以使用如下命令:

grep -r "main()". 

img

2.3 正则表达式与 grep 命令

正则表达式是一种符号表示法,被用来识别文本模式。在某种程度上,它们与匹配文件和路径名的 shell 通配符比较相似,但其规模更大。许多命令行工具和大多数的编程语言都支持正则表达式,以此来帮助解决文本操作问题。

正则表达式元字符由以下字符组成:

^ $ . [ ] { } - ? * + ( ) | \

img

(1)常用范例

例一:利用 Linux 系统自带的字典查找一个五个字母的单词,第三个字母为 j,最后一个字母为 r ,/usr/share/dict 目录下存放字典文件,可以使用如下命令:

grep '^..j.r$' linux.words

img

例二:验证固定电话,打印符合条件的电话,固定电话格式基本都是带有 0 的区号+连接符“-”+电话号码,另外还有可能有分机号,区号有 3 位、4 位,电话号码有 7 位和 8 位的,可以使用如下命令:

grep "^0[0-9]{2,3}-[0-9]{7,8}(-[0-9]{3,4})?$" telphone.txt

区号:前面一个 0,后面跟 2-3 位数字 : 0[0-9]{2,3}

电话号码:7-8 位数字: [0-9]{7,8}

分机号:一般都是 3-4 位数字: [0-9]{3,4} img

注意执行下面的命令时没有任何匹配输出,这是因为没有加-E 选项,那例一没加为什么可以呢,这是因为 grep 把.当成 shell 通配符,不是正则表达式的元字符。

img

三、参考链接

本课程部分内容参考博文每天一个Linux命令,感谢作者peida提供的优质教程。

上一节:Linux操作命令(五) 下一节:Linux操作命令(七)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606551&siteId=291194637