(转)grep Programs and Regular expressions

http://ss64.com/bash/grep-regex.html

grep Programs and Regular expressions

grep searches input files for lines that match a given pattern.

grep programs

`grep' searches the named input files (or standard input if no files are named, or the file name `-' is given) for lines containing a match to the given pattern. By default, `grep' prints the matching lines. There are three major variants of `grep', controlled by the following options.

   -G
   --basic-regexp
        Interpret PATTERN as a basic regular expression.  This is the default.
   -E
   --extended-regexp
        Interpret PATTERN as an extended regular expression.
   -F
   --fixed-strings
        Interpret PATTERN as a list of fixed strings, separated by
        newlines, any of which is to be matched.

In addition, two variant programs EGREP and FGREP are available.
EGREP is the same as `grep -E'.  FGREP is the same as `grep -F'.

Regular Expressions

A regular expression is a pattern that describes a set of strings.

Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions.

`grep' understands two different versions of regular expression syntax: "basic" and "extended". In GNU `grep', there is no difference in available functionality using either syntax. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards.

The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any metacharacter with special meaning may be quoted by preceding it with a backslash. A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list; if the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, [a-d] is equivalent to [abcd]. Many locales sort characters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might be equivalent to [aBbCcDd], for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C.

Finally, certain named classes of characters are predefined within bracket expressions, as follows. Their names are self explanatory, and they are [:alnum:], [:alpha:], [:cntrl:],[:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:]. For example, [[:alnum:]] means [0-9A-Za-z], except the latter form depends upon the C locale and the ASCII character encoding, whereas the former is independent of locale and character set. (Note that the brackets in these class names are part of the symbolic names, and must be included in addition to the brackets delimiting the bracket list.) Most metacharacters lose their special meaning inside lists. To include a literal ] place it first in the list. Similarly, to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.

The period . matches any single character. The symbol \w is a synonym for [[:alnum:]] and \W is a synonym for [^[:alnum]].

The caret ^ and the dollar sign $ are metacharacters that respectively match the empty string at the beginning and end of a line. The symbols \< and \> respectively match the empty string at the beginning and end of a word. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word.

A regular expression may be followed by one of several repetition operators:

   ?   The preceding item is optional and will be matched at most once.
   *   The preceding item will be matched zero or more times.
   +   The preceding item will be matched one or more times.
  {N}  The preceding item is matched exactly N times.
 {N,}  The preceding item is matched n or more times.
{N,M}  The preceding item is matched at least N times, but not more than M times.


Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively match the concatenated subexpressions.

Two regular expressions may be joined by the infix operator `|'; the resulting regular expression matches any string matching either subexpression.

Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole subexpression may be enclosed in parentheses to override these precedence rules.

The backreference `\N', where N is a single digit, matches the substring previously matched by the Nth parenthesized subexpression of the regular expression.

In basic regular expressions the metacharacters 
  ? , + , { , | , ( , and  )
lose their special meaning; instead use the backslashed versions:
 \? ,\+ ,\{ ,\| ,\( , and \)

Traditional `egrep' did not support the `{' metacharacter, and some `egrep' implementations support `\{' instead, so portable scripts should avoid `{' in `egrep' patterns and should use `[{]' to match a literal `{'.

GNU `egrep' attempts to support traditional usage by assuming that `{' is not special if it would be the start of an invalid interval specification. For example, the shell command `egrep '{1'' searches for the two-character string `{1' instead of reporting a syntax error in the regular expression. POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.

Usage
*****

   Here is an example shell command that invokes GNU `grep':

     grep -i 'hello.*world' menu.h main.c

This lists all lines in the files `menu.h' and `main.c' that contain the string `hello' followed by the string `world'; this is because `.*' matches zero or more characters within a line. *Note Regular Expressions::. The `-i' option causes `grep' to ignore case, causing it to match the line `Hello, world!', which it would not otherwise match. *Note Invoking::, for more details about how to invoke `grep'.

   Here are some common questions and answers about `grep' usage.

  1. How can I list just the names of matching files?

          grep -l 'main' *.c

     lists the names of all C files in the current directory whose
     contents mention `main'.

  2. How do I search directories recursively?

          grep -r 'hello' /home/gigi

     searches for `hello' in all files under the directory
     `/home/gigi'.  For more control of which files are searched, use
     `find', `grep' and `xargs'.  For example, the following command
     searches only C files:

          find /home/gigi -name '*.c' -print | xargs grep 'hello' /dev/null

  3. What if a pattern has a leading `-'?

          grep -e '--cut here--' *

     searches for all lines matching `--cut here--'.  Without `-e',
     `grep' would attempt to parse `--cut here--' as a list of options.

  4. Suppose I want to search for a whole word, not a part of a word?

          grep -w 'hello' *

     searches only for instances of `hello' that are entire words; it
     does not match `Othello'.  For more control, use `\<' and `\>' to
     match the start and end of words.  For example:

          grep 'hello\>' *

     searches only for words ending in `hello', so it matches the word
     `Othello'.

  5. How do I output context around the matching lines?

          grep -C 2 'hello' *

     prints two lines of context around each matching line.

  6. How do I force grep to print the name of the file?

     Append `/dev/null':

          grep 'eli' /etc/passwd /dev/null

  7. Why do people use strange regular expressions on `ps' output?

          ps -ef | grep '[c]ron'

     If the pattern had been written without the square brackets, it
     would have matched not only the `ps' output line for `cron', but
     also the `ps' output line for `grep'.

  8. Why does `grep' report "Binary file matches"?

     If `grep' listed all matching "lines" from a binary file, it would
     probably generate output that is not useful, and it might even
     muck up your display.  So GNU `grep' suppresses output from files
     that appear to be binary files.  To force GNU `grep' to output
     lines even from files that appear to be binary, use the `-a' or
     `--text' option.

  9. Why doesn't `grep -lv' print nonmatching file names?

     `grep -lv' lists the names of all files containing one or more
     lines that do not match.  To list the names of all files that
     contain no matching lines, use the `-L' or `--files-without-match'
     option.

 10. I can do OR with `|', but what about AND?

          grep 'paul' /etc/motd | grep 'franc,ois'

     finds all lines that contain both `paul' and `franc,ois'.

 11. How can I search in both standard input and in files?

     Use the special file name `-':

          cat /etc/passwd | grep 'alain' - /etc/motd

Bugs

Large repetition counts in the `{m,n}' construct may cause `grep' to use lots of memory. In addition, certain other obscure regular expressions require exponential time and space, and may cause grep to run out of memory. Backreferences are very slow, and may require exponential time.

Related:

egrep - Search file(s) for lines that match an extended expression
fgrep - Search file(s) for lines that match a fixed string
gawk - Find and Replace text within file(s)
grep - Search file(s) for lines that match a given pattern 
tr - Translate, squeeze, and/or delete characters 
Equivalent Windows command: FINDSTR - Search for strings in files

 

同一台电脑使用模拟ModbusTcp主从通讯的时候,Wireshark抓取不到包 在使用ModBus Slave和Poll模拟主从通讯的时候,发现Wireshark怎么都抓取不到包怎么排查和解决? 阅读详情

相关推荐

从ADRV9002到ADRV9003:手把手教你移植FPGA驱动,避开那些官方没说的坑

本文详细介绍了从ADRV9002到ADRV9003的FPGA驱动移植实战指南,重点解析了两者在硬件架构上的关键差异,如通道配置和DPD功能的移除。通过提供完整的移植步骤、调试技巧和性能优化建议,帮助工程师避开官方文档未提及的常见陷阱,实现高效驱动开发。

weixin_28738397的博客 175

Ubuntu 18.04手动编译安装新版Git教程

Git是分布式版本控制系统的标准工具,其核心原理基于快照存储、有向无环图(DAG)提交历史与本地仓库完全克隆机制。新版Git(2.20+)显著提升了命令语义一致性、子模块递归支持、凭据安全管理和日志可视化能力,技术价值体现在开发效率提升、CI/CD脚本跨环境可复现性增强及团队协作分支策略对齐。典型应用场景包括老旧LTS系统(如Ubuntu 18.04)的功能升级、企业级持续集成环境定制、以及VS Code等IDE深度集成需求。本文聚焦在Ubuntu 18.04环境下从源码编译安装Git 2.40.1的完整实

weixin_30564785的博客 390

Hermes-Agent-完整指南

Hermes Agent 是由 **Nous Research** 开发的一款**自我改进型 AI Agent**,具有内置的学习闭环系统。它的核心理念是"一个与你共同成长的 Agent"。- 能够从经验中创建技能(Skill-Learning Automated),在使用过程中自我改进, 跨会话持久记忆, 深度用户建模

liu_zhiyi的博客 3037

JP1名词解释

http://soft.yesky.com/0/3068500.shtml JP1名词解释 2007-02-05 16:53作者:佚名出处:天极网责任编辑:hwb &lt;!--广告--&gt;     JP1/Base   提供事务服务功能的程序。可以控制服务的起动顺序,并发送和接收JP1事件。JP1/Base是JP1/IM - Console,JP1/A...

mlxia的专栏 2686

【Matlab/Unity】跨平台UDP通信实战:从数据发送到实时可视化

本文详细介绍了Matlab与Unity之间通过UDP协议实现跨平台通信的实战方法,涵盖数据发送、接收和实时可视化全流程。针对科研仿真和工业设计场景,提供优化代码示例和性能对比数据,帮助开发者解决低延迟传输、线程安全等关键问题,显著提升跨平台协作效率。

weixin_29044713的博客 153

不止于连接:用VS Code Remote-SSH深度开发树莓派(Ubuntu 22.04环境实战)

本文详细介绍了如何在Ubuntu 22.04环境下使用VS Code的Remote-SSH功能进行树莓派深度开发。通过SSH配置、VS Code扩展安装和远程环境优化,开发者可以在本地高效编写代码,同时在树莓派上直接运行和测试,解决性能瓶颈问题。文章还提供了高级功能如端口转发、多项目管理和远程调试的实战技巧。

didui8202的博客 352

(转)日语的文字编码

http://blog.csdn.net/jackyren007/archive/2008/07/31/2743779.aspx 日语的文字编码 收藏 1、常用编码日语的文字编码主要是Shift_JIS、EUC-JP、ISO-2022-JP这三种。(1)Shift_JIS主要是Windows和Macintosh使用的文字编码。Shift_JIS 的文字集合基本是按照JIS X 0208...

mlxia的专栏 1520

数字图像处理 基于matlab、opencv计算图像的梯度方向和梯度幅值

图像可以被视为标量场(即二维函数)。通过微分将标量场转换为矢量场。梯度是一个向量,描述了在x或y方向上移动时,图像变化的速度。我们使用导数来回答这样的问题,图像梯度的大小告诉图像变化的速度,而梯度的方向告诉图像变化最快的方向。因为梯度有方向和大小,所以将这些信息编码为向量是很自然的。该向量的长度提供了梯度的大小,而其方向提供了梯度方向。因为梯度在每个位置可能不同,所以我们在每个图像位置用不同的向量来表示它。

学以致用 知行合一 3202

windows系列操作系统支持最大多少内存

windows系列操作系统支持最大多少内存请参考下表:                Physical RAM  Virtual Address Space&nbsp;windows NT 4.0 WorkStation      4GB       2GB&nbsp;windows NT 4.0 Server         4GB       2GB&nbsp;windows NT 4.0...

mlxia的专栏 1365

全局注意机制 GAM | Global Attention Mechanism: Retain Information to Enhance Channel-Spatial Interactions

全局注意机制 GAM

Coding... 2577

71、CAN总线双绞线绞距与抗共模干扰的定量关系

摘要:本文探讨了CAN总线双绞线绞距与抗共模干扰的定量关系。通过理论推导得出共模电压与绞距平方成正比,绞距越小抑制效果越好。工程实践表明,工业现场推荐绞距不超过30mm,高频干扰场景需优先优化绞距。实测数据验证了绞距均匀性的重要性,建议控制在15-25mm范围,安装时保持绞合结构完整。案例显示,将绞距从50mm改为15mm后,共模干扰从12V降至1.8V,有效解决了CAN通信问题。绞距选择需兼顾理论计算与现场实测,同时注意与屏蔽层的协同设计。

2401_88858637的博客 186

使用命令行配置启动windows服务

  使用命令行启动服务 1.   打开“命令提示符”。 2.   键入: sc config service name start= {boot|system|auto|demand|disabled} 值 描述 sc config 修改注册表和“服务控制管理器”数据库中的服务项的值。 service name 指定服务的简短名称。 start= 指定服务的启动类型。 boot 由启动加载程序加...

mlxia的专栏 1345

基于RK3588与昇腾310B的异构K8s集群部署与NPU统一调度实战

本文介绍了基于RK3588与昇腾310B的异构K8s集群部署方案,通过HAMi+Volcano实现NPU统一调度。方案包含硬件配置、操作系统准备(银河麒麟/欧拉)、容器运行时部署及K8s集群搭建等关键步骤,支持多厂商NPU设备的统一抽象和精细化切分,为边缘AI计算提供高效资源管理能力。

huanghm88的专栏 280

开启Windows 2003 最大内存支持

开启Windows 2003 最大内存支持 Windows Server 2003 Enterprise Edition(企业版):&nbsp;* 支持8节点集群、NUMA;支持8路SMP&nbsp;* 内存支持: (i386)32位版支持32GB内存&nbsp;(X64) 64位版支持64GB内存&nbsp;windows 2003 默认只支持小内存模式(最大3G)...

mlxia的专栏 1325

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

(转)如何一次性从Outlook Express中导出邮件

http://hi.baidu.com/grape17/blog/item/05d354a7f7d76d95d04358bf.html 如何一次性从Outlook Express中导出邮件 2007-01-25 16:30 如何一次性从outlook express中导出邮件? 可以照如下步骤操作: 1、打开Outlook Express,进入要备份的信箱,比如:收件箱。 ...

mlxia的专栏 742

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

JavaPassion 中文站

JavaPassion 中文站 www.javapassion.cn 了解 Sang Shin Sang Shin 目前是 Sun Microsystems 的技术架构师、咨询师和技术传道士。他面向全球的开发者听众讲解与 Java 和 Solaris 相关的技术。他目前在波士顿地区工作,主要负责技术推广和咨询工具,包括一些重要的 Java 技术的传道和咨询工具,如 Java EE(以前叫做 ...

mlxia的专栏 703

(转)find:根据指定条件查找文件

http://man.ddvip.com/linux/Mandrakelinuxref/command-find.html find:根据指定条件查找文件 上一页  第 5 章 命令行工具  下一页 find:根据指定条件查找文件 find 是一个资深的 UNIX® 工具。它的功能在于递归扫描一个或多个目录,...

mlxia的专栏 401

(转)SQLデータ型

https://www.microfocus.co.jp/manuals/VisCob2010R2/BKDBDBDTYP.html SQLデータ型 SQL データ型は、COBOL で使用されるデータ形式と異なります。 SQL には一連の標準データ型がありますが、実際の実装状況はデーベースによって異なり、これらのデータ型をすべて実装するデータベースはほとんどありません。 ...

mlxia的专栏 396

命令行下启动系统服务 net start

命令行下启动系统服务Net start 启动服务,或显示已启动服务的列表。两个或多个词组成的服务名,例如 Net Logon 或 Computer Browser,必须两边加引号 (")。 net start [service] 参数 无 键入不带参数的 net start 显示正在运行服务的列表。Net start Messenger 启动“信使”服务。“信使”服务允许计算机接收邮件。Net s...

mlxia的专栏 365

(转)VB6迁移到VB.NET的一些问题汇总

http://www.cnblogs.com/ols/archive/2010/03/23/1692297.html VB6迁移到VB.NET的一些问题汇总     在工作中,可能会碰到需要将VB编写的代码迁移到VB.NET的情况,有好多细节需要处理。好在互联网发展了这么多年,好多问题在互联网上都有答案,所以需要的时候搜索一下就能解决。     本人也碰到了从VB迁移到VB.NET的情况...

mlxia的专栏 332

(转)Linux——计划任务管理

http://hi.baidu.com/%BA%CE%B4%A6%C8%BE%B3%BE%B0%A3a/blog/item/64962c88c05c1fc09023d909.html Linux——计划任务管理 2011-05-05 10:25 在Linux操作系统中,除了用户即时执行的命令操作以外,还以为配置在指定的时间、指定的日期执行预先计划好的系统管理任务(如:定期备...

mlxia的专栏 318

(转)[红旗] 手动可以执行,加入crontab 就不行了

  http://bbs.chinaunix.net/viewthread.php?tid=2295674 [红旗] 手动可以执行,加入crontab 就不行了   1楼 发表于 2011-04-02 14:42 | 只看该作者   30 2 * * * root /usr/sbin/b...

mlxia的专栏 303

IBM DB2 UDB 和 SQL Server 2000

http://www.microsoft.com/china/sql/prodinfo/compare/ibm/mythsrealities.mspx 产品信息 &gt; 数据库系统对比 IBM DB2 UDB 和 SQL Server 2000 更新日期: 2002年11月01日 IBM DB2 和 SQL Server 2000 神话 #1:IBM DB2 UDB 是...

mlxia的专栏 282

(转)《软件工程的事实与谬论》

http://book.group.iteye.com/group/blog/280777 《软件工程的事实与谬论》 本来我真的很想把这些文字敲出来, 但是在网上发现了hydrangea 的文章, 便转载了过来. 只保留条款, 不做分析. 感谢hydrangea 的辛勤劳动.   这本书真的是一本好书, 虽然书中的一些观点有很多我持保留意见, 但这并不妨碍我从书中学到很多...

mlxia的专栏 280

鸿蒙操作系统实现应用接续跨设备流转实战

应用接续的落地成本很低:一个配置、一个onContinue回调、一处 launchReason 判断,合计不足百行代码,就能让任务在手机、平板、鸿蒙电脑之间无缝续接。随着互助通信共享等组网能力在更多机型下放,这条能力的实用价值会持续放大,建议交互密集型应用尽早接入。

NJPJI_Yang的博客 359

【Linux网络】TCP 服务器并发处理多客户端:从 fork 分发到双重 fork 彻底消灭僵尸进程

拆解 TCP 服务器并发演进链:fork 分发连接、SIGCHLD 循环回收僵尸、双重 fork 交孙进程给 init 收养,并给出模型选型与线上排错对照。

2301_78967866的博客 429

技术拆解:Mentalab Explore Signals v0.4.0 OpenGL绘图引擎性能实测,BIN转BIDS与CSV转BDF转换向导全解读

Mentalab在GitHub发布Explore Signals v0.4.0,这是面向Mentalab Explore Pro配套的桌面端波形浏览与数据转换工具。本文按更新日志逐条拆解本版本在渲染、交互、转换管线与兼容性上的变化。

欣佰特cnbestec的博客 104
上一篇: (转)find:根据指定条件查找文件
下一篇: (转)Determining equivalent SQL and COBOL data types
mlxia
博客等级 码龄26年 28粉丝 410原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值