Common linux commands | grep


Common linux commands | grep

About the author

Mr. lemon, senior operation and Maintenance Engineer (self claimed), SRE expert (target), dreams of buying a Porsche at the age of 35. Like to study the underlying technology, think that the underlying foundation is the king. All new technologies are inseparable from the operating system (CPU, memory, disk), network and so on. Insist on the input and output, record their own learning bit by bit, keep on moving forward in the ordinary, one day will meet a different self. Official account: operation and maintenance Wang (ID:Leeeee_) Li)。

1、 Preface

I usually like to take notes. I have worked for four or five years and have taken thousands of notes. Recently, I left my job, coupled with the bottleneck in the workplace, so I’m ready to make a good reply and sort out the relevant notes and knowledge points. Maybe there will be a series of blog output, from basic to advanced.

Common linux commands | grep

2、 What is grep

Global regular expression print

Grep is a Linux / Unix command line tool for searching strings in specified files. Text search patterns are called regular expressions. When it finds a match, it outputs the row with the result. The grep command is very convenient when searching for large log files.


grep [options] pattern [files]

3、 Common parameters

-c: Output only the number of matching rows

– h : Print out the matching line, but do not display its file name

– i : Case insensitive

– l : When querying multiple files, only the file name containing matching characters will be output

-n: Print matching lines and line numbers

-v: Reverse matching, that is, the unmatched rows are displayed

-w: Matches the entire word, not the entire string

-b: Show matching locations

– o : Show only matching strings

-s: Do not display error messages

– E exp: match multiple

-E: Using regular matching

4、 Examples

1: Match mark color

echo “This is a word \n heihei” |grep word --color=auto

2: Take out / etc / passwd containing root

grep root /etc/passwd or cat /etc/passwd |grep root

3: Take out / etc / passwd without root

grep -v root /etc/passwd or cat /etc/passwd |grep -v root

4: Demsg output information to find the line with eth, and output line number, color

dmesg | grep eth -n --color=auto

5: The output information of demsg finds the row of eth, and finds the first two rows and the last three rows containing eth

dmesg |grep eth -n -A3 -B2 --color=auto

fiveGrep exit status

echo $?

#0: success 
#1: no match 
#2: indicates that the file in the parameter does not exist

6Application scenarios

1: Matches the context of the row

ifconfig | grep -C 3 "Link encap"

-A n ﹣ after n lines, a memory (after)

-B N ﹣ the first n lines, B memory is (before)

– C n   The first n lines, the last n lines, C memory is (Center)

2: Count the number with – C

grep -c "" file.txt

3: Search directory according to file content

Grep "declare" ා search in current directory 
Grep - R "declare" # search in current directory and subdirectories 
Grep - R - L "declare" # find the line that only displays the file name, not the specific display 
grep -R --exclude-dir="filename"  #Except for a directory mismatch

4. Filter out comment lines

grep ^[^#] filename

5. Filter out comment lines with spaces

grep "^\s*[^# \t].*$" filename

6. Find the string containing the file1 string in File2f



grep -f file1 file2

#The results are as follows

7. The difference between PS aux | grep init and PS aux | grep [i] nit

ps aux | grep init:

Common linux commands | grep

ps aux |grep [i]nit:

Common linux commands | grep

In the first init, she will also get grep init into the process, and all the inits can be matched

The second [i] nit will bring grep [i] nit into the process, but [i] nit can only match init and nit, and there is only one

echo grep init |grep init 
echo grep init |grep [i]nit 
echo grep [i]nit |grep init 
echo grep [i]nit |grep [i]nit

7、 The difference between grep – E and grep

1. How to use it

grep -E “^[a-zA-Z0-9]” filename 
Grep "^ [a-za-z0-9]" file name # if e is not added, there will be a matching error

2. Examples

The following two writing methods can achieve the same goal, using – E and not using – e writing method

cat a.txt |grep -oE 'id=[0-9]{9,10}' 
cat a.txt |grep -o 'id=[0-9]\{9,10\}'

Recommended Today

Libp2p RS version 0.3.0 introduction

V0.3.0 released on 4.23, usingAsyncRead & AsyncWriteTo replace ourReadEx & WriteEx & SplitEx; SimplifiedKad/DHTImplementation logic. modify ReadEx & WriteEx & SplitEx: At first we tried to useasync-traitTo define their own IO operationsTraitFor more pure useasync/awaitTo write code. withReadExFor example, it is roughly as follows: #[async_trait] pub trait ReadEx { async fn read(&mut self, buf: &mut […]