grep command, wc command and pipe character

1. grep command

Function: Filter file lines by keyword from the file

grammar:grep [-n] 关键字 文件路径

  • Option -n, optional, indicates to display the line numbers of matching lines in the results.

  • Parameters, keywords, required, indicating filtering keywords, with spaces or other special symbols, it is recommended to use "" to surround the keywords

  • Parameter, file path, required, indicating the file path to filter the content, which can be used as the input of the pipe character

2. wc command

Function: Count the number of lines and words in a file, etc.

grammar:wc [-c -m -l -w] 文件路径

  • Option, -c, counts the number of bytes

  • Option, -m, counts the number of characters

  • Option, -l, counts the number of lines

  • Option, -w, counts the number of words

  • Parameters, file paths, files to be counted can be used as input to the pipe character

Example:

Ⅰ.Without options

Ⅱ.Only count the number of rows

Ⅲ. Count the number of lines and words

We can find that no matter whether you write -lw or -wl for the option, the displayed result is the number of lines first and the number of words last.

3. Pipe character

Special symbols, pipe characters: |

The meaning is: use the result of the command on the left side of the pipe character as the input of the command on the right side.

Ⅰ. So what if the pipe character is typed?

Ⅱ.Application

Use the output of cat test.txt (that is, the content of the test.txt file) as the input of the parameters of the wc -l parameter command

Purpose: Count the number of lines in the test.txt file

Ⅲ. Nested use

Use the output of cat test.txt (that is, the content of the test.txt file) as the input of the parameters of the grep "i" parameter command, and then use the output of the grep "i" parameter  as the input of the parameters of the wc -wl parameter command.

Purpose: Count the number of lines and words containing the keyword "i" in the test.txt file

Guess you like

Origin blog.csdn.net/lemonzjk/article/details/135452687