Linux Shell - Orange168/NotesOnReading GitHub Wiki

#Linux Shell

Grep

**Common use **

usage:  grep [-acinv] [] [--color=aruto] 'search_string' finename
-c, --count ; count how many time 'search_string' find in a fine
-n,--line-number:
		for example: ./.git/hooks/update.sample`:54`:case
-i , --ignore-case: 
		Ignore case distinctions in both the PATTERN and the input files.
-A, -B and -C NUM
		Displaying lines before/after/around NUM the match using grep
-v,--invert-match 
		Invert the snse of matching, to select non-matching lines.
--color[=WHEN],
		 WHEN is never, always, or auto(DEFAULT). The colors   are  defined  by  the  environment  variable  GREP_COLORS. 
		 #add `alias grep='grep --color=auto` to ~/.bashrc and source it .
-l,--files-with-match
		print the file match without the line match info. OPP -L
-w,--word-regexp
		Select only those lines containing matches that form whole words.
--exclude*  search from man 
-x,--line-regexp
		Select only those matches that exactly match the whole line.
		For example:
		grep -rx 'import android.support.v7.widget.LinearLayoutManager;'
		#result is: /samples/recyclerviewdemo/
		DividerItemDecoration.java:import android.support.v7.widget.LinearLayoutManager;
		RecyclerViewDemoActivity.java:import android.support.v7.widget.LinearLayoutManager;

A sample

Syntax:  grep -v -e "pattern" -e "pattern"
sample:
$ cat test-file.txt
a
b
c
d
$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

Grep with Regular Expression(regex)

grep -n 't[ae]st' regular_express.txt
grep -n '[^a-z]oo' regular_express.txt
grep -n '\.$' regular_express.txt # end with "." lines
grep -n '^$' regular_express.txt #find the empty line which only has head and tail
grep -n 'g..d' regular_express.txt # "." mean any char is match, and there are two of them 
grep -n 'goo*g' regular_express.txt # at least one "o"
grep -n 'o\{2\}' regular_express.txt #two o

Extention grep (grep -E or egrep)

egrep 'NW|EA' testfile     
northwest       NW      Charles Main        3.0     .98     3       34
eastern         EA      TB Savage           4.4     .84     5       20

grep 'NW\|EA' testfile # extent char add "\" grep auto add -E option 
northwest       NW      Charles Main        3.0     .98     3       34
eastern         EA      TB Savage           4.4     .84     5       20

egrep '3+' testfile
grep -E '3+' testfile
grep '3\+' testfile  
#has same product
northwest       NW      Charles Main          3.0     .98     3       34
western         WE      Sharon Gray           5.3     .97     5       23
northeast       NE      AM Main Jr.           5.1     .94     3       13
central         CT      Ann Stephens          5.7     .94     5       13

egrep '2\.?[0-9]' testfile 
grep -E '2\.?[0-9]' testfile
grep '2\.\?[0-9]' testfile 
# Has same result :
western         WE       Sharon Gray          5.3     .97     5       23
southwest       SW      Lewis Dalsass         2.7     .8      2       18
eastern         EA       TB Savage             4.4     .84     5       20

egrep '(no)+' testfile
grep -E '(no)+' testfile
grep '\(no\)\+' testfile
#Has same result 
northwest       NW      Charles Main        3.0     .98     3       34
northeast       NE       AM Main Jr.        5.1     .94     3       13
north           NO      Margot Weber        4.5     .89     5       9

[[1] linux grep命令详解][linux grep命令详解]

####Linux termial shortcut

cmd function
ctrl + a /<- move to line head
ctrl + e/-> move to line tail
ctrl + b /f move back/front a char
alt + b move back a word
ctrl+w delete a word before cursor
alt + d delete a word after cursor
ctrl +k delete all word after cursor
[linux grep命令详解]: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html "linux grep命令详解"