日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux-grep命令

發布時間:2025/3/21 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux-grep命令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述

grep(global search regular expression(RE) and print out the line,全面搜索正則表達式并把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹配的行打印出來。

語法

grep命令常見用法

在文件中搜索一個單詞,命令會返回一個包含“match_pattern”的文本行

grep match_pattern file_name grep "match_pattern" file_name

在多個文件中查找

grep "match_pattern" file1 file2 ...

輸出除匹配項之外的所有行 -v 選項

grep -v "match_pattern" file_name

標記匹配顏色 –color=auto 選項

grep "match_pattern" file_name --color=auto

使用正則表達式 -E 選項

grep -E "[1-9]+" 或 egrep "[1-9]+"

只輸出文件中匹配到的部分 -o 選項

[root@entel2 test]# echo this is a test line. | grep -o -E "[a-z]+\." line.[root@entel2 test]# echo this is a test line. | egrep -o "[a-z]+\." line.

統計文件或者文本中包含匹配字符串的行數 -c 選項

[root@entel2 test]# cat args.txt aaa bbb ccc bbb ddd aaaaa [root@entel2 test]# grep -c "aaa" args.txt 2

輸出包含匹配字符串的行數 -n 選項

[root@entel2 test]# grep -n "aaa" args.txt 1:aaa 6:aaaaa [root@entel2 test]# cat args.txt |grep -n "aaa" 1:aaa 6:aaaaa#多個文件 grep "match pattern" -n file_1 file_2

打印樣式匹配所位于的字符或字節偏移

[root@entel2 test]# echo gun is not unix | grep -b -o "not" 7:not [root@entel2 test]# [root@entel2 test]# echo gun is not unix | grep -b -o "is" 4:is [root@entel2 test]# echo gun is not unix | grep -b -o "gun" 0:gun [root@entel2 test]# echo gun is not unix | grep -b -o "i" 4:i 13:i

一行中字符串的字符便宜是從該行的第一個字符開始計算,起始值為0。
選項 -b -o 一般總是配合使用。

搜索多個文件并查找匹配文本在哪些文件中

[root@entel2 test]# cat grep1.txt xiaogongjiang xiaogongjiang1 [root@entel2 test]# cat grep2.txt xiaogongjiang this is just for test grep -l [root@entel2 test]# grep -l "xiaogongjiang" grep1.txt grep2.txt grep1.txt grep2.txt [root@entel2 test]# grep -l "xiaogongjiang" grep* grep1.txt grep2.txt [root@entel2 test]# grep -l "test" grep* grep2.txt [root@entel2 test]#

grep遞歸搜索文件

在多級目錄中對文本進行遞歸搜索

# .表示當前目錄。 [root@entel2 ~]# grep "xiaogongjiang" . -n -r ./test/grep1.txt:1:xiaogongjiang ./test/grep1.txt:2:xiaogongjiang1 ./test/grep2.txt:1:xiaogongjiang

忽略匹配樣式中的字符大小寫

[root@entel2 ~]# echo "hello world" | grep -i "HELLO" hello world

選項 -e 制動多個匹配樣式

[root@entel2 ~]# echo this is a text line | grep -e "is" -e "line" -o is is line

在grep搜索結果中包括或者排除指定文件

使用0值字節后綴的grep與xargs

grep靜默輸出

打印出匹配文本之前或者之后的行


總結

以上是生活随笔為你收集整理的Linux-grep命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。