學習啦 > 學習電腦 > 操作系統(tǒng) > Linux教程 > liunx grep命令最常用的功能總結

liunx grep命令最常用的功能總結

時間: 志藝942 分享

liunx grep命令最常用的功能總結

  對于Linux系統(tǒng)來說,無論是中央處理器、內(nèi)存、磁盤驅動器、鍵盤、鼠標,還是用戶等都是文件,Linux系統(tǒng)管理的命令是它正常運行的核心,與之前的DOS命令類似。接下來是小編為大家收集的liunx grep命令最常用的功能總結,歡迎大家閱讀:

  liunx grep命令最常用的功能總結

  1. grep最簡單的使用方法,匹配一個詞:grep word filename

  2. 可以從多個文件中匹配:grep word filename1 filenam2 filename3

  3. 可以使用正則表達式匹配:grep -E pattern f1 f2 f3...

  4. 可以使用-o只打印匹配的字符,如下所示:

  lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\."

  line.

  5. 打印除匹配行之外的其他行,使用-v

  lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"

  3

  4

  6. 統(tǒng)計匹配字符串的行數(shù),使用-c

  lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c

  2

  7. 如果我們統(tǒng)計字符串模式匹配的次數(shù),可以結合-o和-c,如下:

  lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]" | wc -l

  8

  8. 如果需要顯示行號,可以打開-n,如下:

  lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"

  3:33333

  9. -b選項可以打印出匹配的字符串想對于其所在的行起始位置的偏移量(從0開始),通常配合-o使用,如下:

  lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4

  4:4

  10. 當字符串在多個文件中匹配時,-l選項將只打印文件名

  11. -L與-l相對,只打印不匹配的文件名

  lichao@ubuntu:command$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:command$ cat test2.txt

  a

  very

  popular

  os,

  linux

  lichao@ubuntu:command$ cat test3.txt

  what

  the

  fxxk

  lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt

  test1.txt

  test2.txt

  lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt

  test3.txt

  12. 打開遞歸搜索功能

  lichao@ubuntu:command$ grep -n -R linux .

  ./test2.txt:5:linux

  ./test1.txt:1:linux

  13. 忽略大小寫:-i

  lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello"

  HELLO WORLD

  14. 匹配多個字符串模式

  lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o

  This

  is

  line

  15. 用單獨的文件提供匹配樣式,每個匹配的樣式作為一行,如下例所示:

  lichao@ubuntu:command$ cat pattern.txt

  1$

  2

  3

  lichao@ubuntu:command$ cat num.txt

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  lichao@ubuntu:command$ grep -f pattern.txt num.txt

  1

  2

  3

  16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息;使用-B n打印匹配行及其前n行信息;使用 -C n,打印匹配行及其前后n行信息;如果有多重匹配,將使用--隔離。示例如下:

  lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3

  5

  6

  7

  8

  lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3

  2

  3

  4

  5

  lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3

  2

  3

  4

  5

  6

  7

  8

  lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2

  a

  b

  c

  --

  a

  b

  c

  17. 使用-q進入靜默模式,該模式下,grep命令運行目的僅僅是執(zhí)行一個條件測試,通常在腳本中使用。通過檢查其返回值進行下一步操作。示例如下:

  lichao@ubuntu:command$ cat tmp.txt

  hello

  world

  lichao@ubuntu:command$ cat tmp.csh

  #!/bin/bash

  if [ $# -ne 2 ]; then

  echo "Usage:

學習啦 > 學習電腦 > 操作系統(tǒng) > Linux教程 > liunx grep命令最常用的功能總結

liunx grep命令最常用的功能總結

時間: 志藝942 分享

liunx grep命令最常用的功能總結

  對于Linux系統(tǒng)來說,無論是中央處理器、內(nèi)存、磁盤驅動器、鍵盤、鼠標,還是用戶等都是文件,Linux系統(tǒng)管理的命令是它正常運行的核心,與之前的DOS命令類似。接下來是小編為大家收集的liunx grep命令最常用的功能總結,歡迎大家閱讀:

  liunx grep命令最常用的功能總結

  1. grep最簡單的使用方法,匹配一個詞:grep word filename

  2. 可以從多個文件中匹配:grep word filename1 filenam2 filename3

  3. 可以使用正則表達式匹配:grep -E pattern f1 f2 f3...

  4. 可以使用-o只打印匹配的字符,如下所示:

  lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\."

  line.

  5. 打印除匹配行之外的其他行,使用-v

  lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"

  3

  4

  6. 統(tǒng)計匹配字符串的行數(shù),使用-c

  lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c

  2

  7. 如果我們統(tǒng)計字符串模式匹配的次數(shù),可以結合-o和-c,如下:

  lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]" | wc -l

  8

  8. 如果需要顯示行號,可以打開-n,如下:

  lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"

  3:33333

  9. -b選項可以打印出匹配的字符串想對于其所在的行起始位置的偏移量(從0開始),通常配合-o使用,如下:

  lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4

  4:4

  10. 當字符串在多個文件中匹配時,-l選項將只打印文件名

  11. -L與-l相對,只打印不匹配的文件名

  lichao@ubuntu:command$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:command$ cat test2.txt

  a

  very

  popular

  os,

  linux

  lichao@ubuntu:command$ cat test3.txt

  what

  the

  fxxk

  lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt

  test1.txt

  test2.txt

  lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt

  test3.txt

  12. 打開遞歸搜索功能

  lichao@ubuntu:command$ grep -n -R linux .

  ./test2.txt:5:linux

  ./test1.txt:1:linux

  13. 忽略大小寫:-i

  lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello"

  HELLO WORLD

  14. 匹配多個字符串模式

  lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o

  This

  is

  line

  15. 用單獨的文件提供匹配樣式,每個匹配的樣式作為一行,如下例所示:

  lichao@ubuntu:command$ cat pattern.txt

  1$

  2

  3

  lichao@ubuntu:command$ cat num.txt

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  lichao@ubuntu:command$ grep -f pattern.txt num.txt

  1

  2

  3

  16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息;使用-B n打印匹配行及其前n行信息;使用 -C n,打印匹配行及其前后n行信息;如果有多重匹配,將使用--隔離。示例如下:

  lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3

  5

  6

  7

  8

  lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3

  2

  3

  4

  5

  lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3

  2

  3

  4

  5

  6

  7

  8

  lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2

  a

  b

  c

  --

  a

  b

  c

  17. 使用-q進入靜默模式,該模式下,grep命令運行目的僅僅是執(zhí)行一個條件測試,通常在腳本中使用。通過檢查其返回值進行下一步操作。示例如下:

  lichao@ubuntu:command$ cat tmp.txt

  hello

  world

  lichao@ubuntu:command$ cat tmp.csh

  #!/bin/bash

  if [ $# -ne 2 ]; then

  echo "Usage:

學習啦 > 學習電腦 > 操作系統(tǒng) > Linux教程 > liunx grep命令最常用的功能總結

liunx grep命令最常用的功能總結

時間: 志藝942 分享

  file=

liunx grep命令最常用的功能總結

  對于Linux系統(tǒng)來說,無論是中央處理器、內(nèi)存、磁盤驅動器、鍵盤、鼠標,還是用戶等都是文件,Linux系統(tǒng)管理的命令是它正常運行的核心,與之前的DOS命令類似。接下來是小編為大家收集的liunx grep命令最常用的功能總結,歡迎大家閱讀:

  liunx grep命令最常用的功能總結

  1. grep最簡單的使用方法,匹配一個詞:grep word filename

  2. 可以從多個文件中匹配:grep word filename1 filenam2 filename3

  3. 可以使用正則表達式匹配:grep -E pattern f1 f2 f3...

  4. 可以使用-o只打印匹配的字符,如下所示:

  lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\."

  line.

  5. 打印除匹配行之外的其他行,使用-v

  lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"

  3

  4

  6. 統(tǒng)計匹配字符串的行數(shù),使用-c

  lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c

  2

  7. 如果我們統(tǒng)計字符串模式匹配的次數(shù),可以結合-o和-c,如下:

  lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]" | wc -l

  8

  8. 如果需要顯示行號,可以打開-n,如下:

  lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"

  3:33333

  9. -b選項可以打印出匹配的字符串想對于其所在的行起始位置的偏移量(從0開始),通常配合-o使用,如下:

  lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4

  4:4

  10. 當字符串在多個文件中匹配時,-l選項將只打印文件名

  11. -L與-l相對,只打印不匹配的文件名

  lichao@ubuntu:command$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:command$ cat test2.txt

  a

  very

  popular

  os,

  linux

  lichao@ubuntu:command$ cat test3.txt

  what

  the

  fxxk

  lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt

  test1.txt

  test2.txt

  lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt

  test3.txt

  12. 打開遞歸搜索功能

  lichao@ubuntu:command$ grep -n -R linux .

  ./test2.txt:5:linux

  ./test1.txt:1:linux

  13. 忽略大小寫:-i

  lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello"

  HELLO WORLD

  14. 匹配多個字符串模式

  lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o

  This

  is

  line

  15. 用單獨的文件提供匹配樣式,每個匹配的樣式作為一行,如下例所示:

  lichao@ubuntu:command$ cat pattern.txt

  1$

  2

  3

  lichao@ubuntu:command$ cat num.txt

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  lichao@ubuntu:command$ grep -f pattern.txt num.txt

  1

  2

  3

  16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息;使用-B n打印匹配行及其前n行信息;使用 -C n,打印匹配行及其前后n行信息;如果有多重匹配,將使用--隔離。示例如下:

  lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3

  5

  6

  7

  8

  lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3

  2

  3

  4

  5

  lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3

  2

  3

  4

  5

  6

  7

  8

  lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2

  a

  b

  c

  --

  a

  b

  c

  17. 使用-q進入靜默模式,該模式下,grep命令運行目的僅僅是執(zhí)行一個條件測試,通常在腳本中使用。通過檢查其返回值進行下一步操作。示例如下:

  lichao@ubuntu:command$ cat tmp.txt

  hello

  world

  lichao@ubuntu:command$ cat tmp.csh

  #!/bin/bash

  if [ $# -ne 2 ]; then

  echo "Usage: $0 match_pattern file_name"

  exit

  fi

  match=$1

  file=$2

  grep -q $match $file

  if [ $? -ne 0 ]; then

  echo "$match not exist in $file"

  else

  echo "$match exist in $file"

  fi

  lichao@ubuntu:command$ ./tmp.csh hello tmp.txt

  hello exist in tmp.txt

  18. -Z選項在輸出匹配文件名時將以/0結尾配合xargs -0可以發(fā)揮很多作用,例如刪除匹配某個模式的文件如下:

  lichao@ubuntu:command$ ls -llrt

  total 28

  -rw-rw-r-- 1 lichao lichao 13 Nov 1 20:38 test1.txt

  -rw-rw-r-- 1 lichao lichao 27 Nov 1 20:39 test2.txt

  -rw-rw-r-- 1 lichao lichao 14 Nov 1 20:39 test3.txt

  -rw-rw-r-- 1 lichao lichao 21 Nov 1 20:45 num.txt

  -rw-rw-r-- 1 lichao lichao 7 Nov 1 20:45 pattern.txt

  -rw-rw-r-- 1 lichao lichao 12 Nov 1 21:25 tmp.txt

  -rwxr-xr-x 1 lichao lichao 217 Nov 1 21:27 tmp.csh

  lichao@ubuntu:command$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:command$ cat test2.txt

  a

  very

  popular

  os,

  linux

  lichao@ubuntu:command$ grep "linux" * -lZ | xargs -0 rm

  lichao@ubuntu:command$ ls

  num.txt pattern.txt test3.txt tmp.csh tmp.txt

  以上命令將包含linux字符串的test1.txt和test2.txt刪除。

  19. 排除/包括文件或者目錄:1)--include *{.c,.cpp} 只在目錄中搜索.c和.cpp文件;2)--exclude "README" 排除所有README文件 3) --include-dir 僅在某些目錄中搜索 4) --exclude-dir 排除某些目錄 5) --exclude-from FILE 從文件FILE中讀取需要排除的文件列表

  lichao@ubuntu:test$ ls

  dir1 dir2 exclude.config test1.txt test2.doc test3.word

  lichao@ubuntu:test$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:test$ cat test2.doc

  wonderful

  os,

  linux

  lichao@ubuntu:test$ cat test3.word

  wonderful

  os,

  linux

  lichao@ubuntu:test$ ls dir1/

  test1.txt test2.doc test3.word

  lichao@ubuntu:test$ ls dir2/

  test1.txt test2.doc test3.word

  lichao@ubuntu:test$ cat exclude.config

  *.txt

  lichao@ubuntu:test$ grep "linux" -R -n .

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./test1.txt:1:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test3.word:3:linux

  ./dir2/test1.txt:1:linux

  ./dir1/test2.doc:3:linux

  ./dir1/test3.word:3:linux

  ./dir1/test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --include *.txt --include *.doc

  ./test2.doc:3:linux

  ./test1.txt:1:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test1.txt:1:linux

  ./dir1/test2.doc:3:linux

  ./dir1/test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc

  grep: unrecognized option '--eclude'

  Usage: grep [OPTION]... PATTERN [FILE]...

  Try 'grep --help' for more information.

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc

  ./test3.word:3:linux

  ./dir2/test3.word:3:linux

  ./dir1/test3.word:3:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./test1.txt:1:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test3.word:3:linux

  ./dir2/test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude-from exclude.config

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test3.word:3:linux

  ./dir1/test2.doc:3:linux

  ./dir1/test3.word:3:linux

  
看了“liunx grep命令最常用的功能總結”還想看:

1.整理Linux中常用的一些grep命令介紹

2.Linux下如何使用grep命令搜索多個單詞

3.linux grep搜索命令的使用方法

4.深入Linux grep指令的詳解

5.Linux文本匹配命令grep與fgrep使用全解

  grep -q $match $file

  if [ $? -ne 0 ]; then

  echo "$match not exist in $file"

  else

  echo "$match exist in $file"

  fi

  lichao@ubuntu:command$ ./tmp.csh hello tmp.txt

  hello exist in tmp.txt

  18. -Z選項在輸出匹配文件名時將以/0結尾配合xargs -0可以發(fā)揮很多作用,例如刪除匹配某個模式的文件如下:

  lichao@ubuntu:command$ ls -llrt

  total 28

  -rw-rw-r-- 1 lichao lichao 13 Nov 1 20:38 test1.txt

  -rw-rw-r-- 1 lichao lichao 27 Nov 1 20:39 test2.txt

  -rw-rw-r-- 1 lichao lichao 14 Nov 1 20:39 test3.txt

  -rw-rw-r-- 1 lichao lichao 21 Nov 1 20:45 num.txt

  -rw-rw-r-- 1 lichao lichao 7 Nov 1 20:45 pattern.txt

  -rw-rw-r-- 1 lichao lichao 12 Nov 1 21:25 tmp.txt

  -rwxr-xr-x 1 lichao lichao 217 Nov 1 21:27 tmp.csh

  lichao@ubuntu:command$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:command$ cat test2.txt

  a

  very

  popular

  os,

  linux

  lichao@ubuntu:command$ grep "linux" * -lZ | xargs -0 rm

  lichao@ubuntu:command$ ls

  num.txt pattern.txt test3.txt tmp.csh tmp.txt

  以上命令將包含linux字符串的test1.txt和test2.txt刪除。

  19. 排除/包括文件或者目錄:1)--include *{.c,.cpp} 只在目錄中搜索.c和.cpp文件;2)--exclude "README" 排除所有README文件 3) --include-dir 僅在某些目錄中搜索 4) --exclude-dir 排除某些目錄 5) --exclude-from FILE 從文件FILE中讀取需要排除的文件列表

  lichao@ubuntu:test$ ls

  dir1 dir2 exclude.config test1.txt test2.doc test3.word

  lichao@ubuntu:test$ cat test1.txt

  linux

  is

  fun

  lichao@ubuntu:test$ cat test2.doc

  wonderful

  os,

  linux

  lichao@ubuntu:test$ cat test3.word

  wonderful

  os,

  linux

  lichao@ubuntu:test$ ls dir1/

  test1.txt test2.doc test3.word

  lichao@ubuntu:test$ ls dir2/

  test1.txt test2.doc test3.word

  lichao@ubuntu:test$ cat exclude.config

  *.txt

  lichao@ubuntu:test$ grep "linux" -R -n .

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./test1.txt:1:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test3.word:3:linux

  ./dir2/test1.txt:1:linux

  ./dir1/test2.doc:3:linux

  ./dir1/test3.word:3:linux

  ./dir1/test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --include *.txt --include *.doc

  ./test2.doc:3:linux

  ./test1.txt:1:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test1.txt:1:linux

  ./dir1/test2.doc:3:linux

  ./dir1/test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc

  grep: unrecognized option '--eclude'

  Usage: grep [OPTION]... PATTERN [FILE]...

  Try 'grep --help' for more information.

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc

  ./test3.word:3:linux

  ./dir2/test3.word:3:linux

  ./dir1/test3.word:3:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./test1.txt:1:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test3.word:3:linux

  ./dir2/test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./test1.txt:1:linux

  lichao@ubuntu:test$ grep "linux" -R -n . --exclude-from exclude.config

  ./test2.doc:3:linux

  ./test3.word:3:linux

  ./dir2/test2.doc:3:linux

  ./dir2/test3.word:3:linux

  ./dir1/test2.doc:3:linux

  ./dir1/test3.word:3:linux

  
看了“liunx grep命令最常用的功能總結”還想看:

1.整理Linux中常用的一些grep命令介紹

2.Linux下如何使用grep命令搜索多個單詞

3.linux grep搜索命令的使用方法

4.深入Linux grep指令的詳解

5.Linux文本匹配命令grep與fgrep使用全解

2991800