广告
返回顶部
首页 > 资讯 > 操作系统 >Linux常用命令之grep命令用法详解
  • 190
分享到

Linux常用命令之grep命令用法详解

Linuxgrep命令用法Linuxgrep命令 2022-06-03 14:06:26 190人浏览 安东尼
摘要

1.官方简介 grep是linux的常用命令,用于对文件和文本执行重复搜索任务的Unix工具,可以通过grep命令指定特定搜索条件来搜索文件及其内容以获取有用的信息。 Usage: grep [OPTioN]..

1.官方简介

grep是linux的常用命令,用于对文件和文本执行重复搜索任务的Unix工具,可以通过grep命令指定特定搜索条件来搜索文件及其内容以获取有用的信息。


Usage: grep [OPTioN]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
 -E, --extended-regexp  PATTERN is an extended regular expression (ERE)
 -F, --fixed-strings  PATTERN is a set of newline-separated fixed strings
 -G, --basic-regexp  PATTERN is a basic regular expression (BRE)
 -P, --perl-regexp   PATTERN is a Perl regular expression
 -e, --regexp=PATTERN  use PATTERN for matching
 -f, --file=FILE   obtain PATTERN from FILE
 -i, --ignore-case   ignore case distinctions
 -w, --Word-regexp   force PATTERN to match only whole words
 -x, --line-regexp   force PATTERN to match only whole lines
 -z, --null-data   a data line ends in 0 byte, not newline

Miscellaneous:
 -s, --no-messages   suppress error messages
 -v, --invert-match  select non-matching lines
 -V, --version    display version infORMation and exit
  --help    display this help text and exit

Output control:
 -m, --max-count=NUM  stop after NUM matches
 -b, --byte-offset   print the byte offset with output lines
 -n, --line-number   print line number with output lines
  --line-buffered  flush output on every line
 -H, --with-filename  print the file name for each match
 -h, --no-filename   suppress the file name prefix on output
  --label=LABEL   use LABEL as the standard input file name prefix
 -o, --only-matching  show only the part of a line matching PATTERN
 -q, --quiet, --silent  suppress all normal output
  --binary-files=TYPE assume that binary files are TYPE;
       TYPE is 'binary', 'text', or 'without-match'
 -a, --text    equivalent to --binary-files=text
 -I      equivalent to --binary-files=without-match
 -d, --directories=ACTION how to handle directories;
       ACTION is 'read', 'recurse', or 'skip'
 -D, --devices=ACTION  how to handle devices, FIFOs and Sockets;
       ACTION is 'read' or 'skip'
 -r, --recursive   like --directories=recurse
 -R, --dereference-recursive
       likewise, but follow all symlinks
  --include=FILE_PATTERN
       search only files that match FILE_PATTERN
  --exclude=FILE_PATTERN
       skip files and directories matching FILE_PATTERN
  --exclude-from=FILE skip files matching any file pattern from FILE
  --exclude-dir=PATTERN directories that match PATTERN will be skipped.
 -L, --files-without-match print only names of FILEs containing no match
 -l, --files-with-matches print only names of FILEs containing matches
 -c, --count    print only a count of matching lines per FILE
 -T, --initial-tab   make tabs line up (if needed)
 -Z, --null    print 0 byte after FILE name

Context control:
 -B, --before-context=NUM print NUM lines of leading context
 -A, --after-context=NUM print NUM lines of trailing context
 -C, --context=NUM   print NUM lines of output context
 -NUM      same as --context=NUM
  --group-separator=SEP use SEP as a group separator
  --no-group-separator use empty string as a group separator
  --color[=WHEN],
  --colour[=WHEN]  use markers to highlight the matching strings;
       WHEN is 'always', 'never', or 'auto'
 -U, --binary    do not strip CR characters at EOL (MSDOS/windows)
 -u, --unix-byte-offsets report offsets as if CRs were not there
       (MSDOS/Windows)

'egrep' means 'grep -E'. 'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input. With no FILE, read . if a command-line
-r is given, - otherwise. If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page: <Http://www.gnu.org/software/grep/>
General help using GNU software: http://www.gnu.org/gethelp/

我平时也是简单的查看一个用户数据,用于简单的数据校对,最近突然接到分析后台日志的需求,才发现grep用处还是不少的。

比如我们后台日志相当大,要是直接从服务器直接拉取,耗时长占用带宽,所以方案就是直接使用 grep关键字重定向到新的文件中,从14G直接到12M,然后再数据清洗和分析。

2.实战介绍

2.1使用grep命令对多文件中多种文本查询

note :使用egrep命令,可使用扩展的正则表达式

1.多文件

  • grep 'pattern' file1 file2

2.多文本 , 关系是OR

  • egrep 'pattern1|pattern2' *.py
  • grep -e pattern1 -e pattern2 *.py
  • grep -E 'pattern1|pattern2' *.doc

例如下面对 对文件中 存在关键字 wordA or wordB进行提取:


grep 'wordA\|wordB' *.py
grep -E 'wordA|wordB' *.doc
grep -e wordA -e wordB *.py
egrep "wordA|wordB" *.c

3.多文本关系是 AND

这里我并没有看到 直接能用的【option】,只能加一层管道符|。

例如:


grep -e pattern1 *.py |grep -e pattern2

2.2完全匹配关键词 -w


grep -w 'warning\|error\|critical' /home/logs

2.3使用-i参数忽略大小写,?color高亮显示匹配结果


egrep -wi --color 'warning|error|critical' /home/logs

2.4递归查找


egrep -Rwi --color 'warning|error' /home/logs/

到此这篇关于Linux常用命令-grep命令用法详解的文章就介绍到这了,更多相关Linux中grep命令详解内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Linux常用命令之grep命令用法详解

本文链接: https://www.lsjlt.com/news/11489.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Linux常用命令之grep命令用法详解
    1.官方简介 grep是linux的常用命令,用于对文件和文本执行重复搜索任务的Unix工具,可以通过grep命令指定特定搜索条件来搜索文件及其内容以获取有用的信息。 Usage: grep [OPTION].....
    99+
    2022-06-03
    Linux grep命令用法 Linux grep命令
  • 一、linux grep命令详解
    目录 1. grep【擅长在文件中匹配文本】     1.1 命令参数      1.1.1 用法举例     1.2 grep搭配管道使用      1.2.1 grep和cat搭配管道      1.2.2 grep和ps搭配管道   ...
    99+
    2023-09-01
    linux 服务器 运维
  • Linux中grep命令详解
    目录一、grep基本介绍二、正则表达式grep实践2.1、输出以 I 开头的行(不区分大小写)2.2、输出以.结尾的行2.3、$符号 注意在linux平台下, 所有文件的结尾都有一个$符可以利用cat -A 查看文件2....
    99+
    2023-02-13
    Linux中grep详解 Linux中grep命令
  • linux中使用grep命令详解
    linux grep命令 Linux grep命令用于查找文件里符合条件的字符串;也可以用于查找内容包含指定的范本样式的文件。它能使用正则表达式搜索,用于在文件中搜索指定的字符串模式,列出含有匹配模式子符串的文件名,并输...
    99+
    2022-06-04
    linux grep
  • linux命令详解之chkconfig命令使用方法
    使用语法:chkconfig[--add][--del][--list][系统服务] 或chkconfig[--level < 等级代号>][系统服务][on/off/reset] chkcon...
    99+
    2022-06-04
    命令 使用方法 详解
  • linux命令详解之useradd命令使用方法
    Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统。用户的账号一方面可以帮助系统管理员对使用系统的用户进行跟踪,...
    99+
    2022-06-04
    命令 使用方法 详解
  • Linux常用命令grep怎么用
    这篇文章将为大家详细讲解有关Linux常用命令grep怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Linux常用命令grep 命令用于查找文件里符合条件的字符串。grep 指令用于查找内容包含指定...
    99+
    2023-06-28
  • Linux 常用命令之Linux more命令使用方法
    more 是我们最常用的工具之一,最常用的就是显示输出的内容,然后根据窗口的大小进行分页显示,然后还能提示文件的百分比。 more功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上。 more会以一页一页...
    99+
    2022-06-04
    Linux more命令使用方法 linux more命令
  • Linux 命令之rsync命令详解
    rsync命令 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的...
    99+
    2022-06-04
    命令 详解 Linux
  • Linux中grep和egrep命令详解
    rep / egrep 语法: grep  [-cinvABC]  'word'  filename -c :打印符合要求的行数 -i :忽略大小写 -n :在输出符合要求的行的同时连同行号...
    99+
    2022-06-04
    Linux grep egrep
  • Linux常用命令之性能命令
    本文介绍linux常用性能统计分析命令,监控进程或者系统性能。主要包括CPU(top、mpstat)、内存(vmstat、free)、I/O(iostat)、网络性能(sar)、系统日志信息(demsg)、查看进程状态(...
    99+
    2022-06-04
    Linux常用命令 linux性能命令
  • Linux常用命令详解
    系列文章目录 Linux 环境搭建以及xshell远程连接_crazy_xieyi的博客-CSDN博客   ls       该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息。 常用选项: -a 列出目...
    99+
    2023-09-01
    python 开发语言 linux java 服务器
  • Linux基础命令grep的用法
    本篇内容主要讲解“Linux基础命令grep的用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux基础命令grep的用法”吧!grep按照指定的模式,在文件中搜索匹配的行,将结果显示在标...
    99+
    2023-06-06
  • linux grep搜索命令的用法
    这篇文章主要介绍“linux grep搜索命令的用法”,在日常操作中,相信很多人在linux grep搜索命令的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux grep搜索命令的用法”的疑惑有所...
    99+
    2023-06-10
  • linux命令grep用法是什么
    grep是Linux中一种非常实用的文本搜索命令,用于在文件或标准输入中搜索指定的模式,并输出匹配的行。grep的基本用法为:gre...
    99+
    2023-09-14
    linux grep
  • django之常用命令详解
    Django 基本命令 本节主要是为了让您了解一些django最基本的命令,请尝试着记住它们,并且多多练习下 1. 新建一个 django project django-admin.py startp...
    99+
    2022-06-04
    详解 常用命令 django
  • 【Linux】ps -ef|grep -v grep|awk ‘{print $2}‘ 命令详解
    第一步:grep -v grep grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用...
    99+
    2023-10-11
    linux 运维 服务器 Powered by 金山文档
  • Linux shell命令用法及常见用例之tar命令
    前言 tar命令用来归档多个文件或目录到单个归档文件中,并且归档文件可以进一步使用gzip或者bzip2等技术进行压缩。 命令格式 tar [OPTION...] [FILE]... 命令功能 Tar(Tap...
    99+
    2022-06-04
    linux shell tar shell tar 命令 linux压缩命令tar
  • Linux常用命令mkdir详解
    mkdir  make directories  创建目录 语法格式:mkdir 【option】【directory】 mkdir [选项] [目录] 注意:mkdir 命令以及后面的选项和目录...
    99+
    2022-06-04
    mkdir命令 LINUX MKDIR 命令 Linux创建目录mkdir
  • lspci 命令详解及常用命令
    lspci命令用于显示计算机的PCI总线信息,包括PCI设备的厂商、设备ID等。常用的lspci命令参数如下:- -v:显示详细的信...
    99+
    2023-09-09
    lspci
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作