Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use grep Command in linux

Shulou Source: shulou.com Published: 2022-06-02 21:48:12 09月22日 Update

This article uses easy-to-understand examples to introduce the use of grep commands in linux, the code is very detailed, interested friends can refer to, hope to be helpful.

Grep

Grep is a powerful text search tool that uses regular expressions to search for text and print matching lines.

Common syntax:

[root@www ~] # grep [- acinv] [--color=auto] 'search string' filename

Options and parameters:

-a: search binary files for data in the form of text files

-c: calculates the number of times a 'search string' is found

-I: ignore the case difference, so the case is considered the same

-n: output line number

-v: reverse selection, that is, the line that shows no 'search string' content

-- color=auto: you can color the keywords you find.

For example:

Take out the line where root appears in / etc/passwd

# grep root / etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin or # cat / etc/passwd | grep root root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

Take out the lines where root appears in / etc/passwd and display the line numbers of these lines in / etc/passwd

# grep-n root / etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin this is the end of the usage of the grep command in linux. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.

Tags: Character string usage command method content size file text more line number help search different good powerful same one line code example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft OPPO Reno macOS Huawei