Linux环境grep搜索方法记录

news2025/2/27 11:28:53

1 grep

grep 命令,用来搜索字符串所在位置,可以具体到不同文件,不同行;
在Linux 下,查看命令释义如下

zhaoc@ubuntu2004:~$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions in patterns and data
      --no-ignore-case      do not ignore case distinctions (default)
  -w, --word-regexp         match only whole words
  -x, --line-regexp         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 selected lines
  -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 file name with output lines
  -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 nonempty parts of lines that match
  -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=GLOB        search only files that match GLOB (a file pattern)
      --exclude=GLOB        skip files that match GLOB
      --exclude-from=FILE   skip files that match any file pattern from FILE
      --exclude-dir=GLOB    skip directories that match GLOB
  -L, --files-without-match  print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
  -c, --count               print only a count of selected 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
      --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)

When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) 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: <https://www.gnu.org/gethelp/>

2 搜索方式

2.1 通用格式

我目前使用的命令,通用格式如下

grep <命令选项(可选)> <附加命令项(可选)> <带搜索字符串(支持正则)> <路径(默认为当前路径)>

2.2 当前路径搜索

在当前路径搜索特定字符串,并显示出对应行数

grep -rn "zhaoc"

在这里插入图片描述

2.3 指定路径搜索

grep -rn zhaoc  ~/11-tools/

在这里插入图片描述

2.4 搜索指定文件类型

grep -rin --include=*.h --include=*.cpp foo <可选搜索路径>

实际操作,举例如下:

zhaoc@ubuntu2004:~$ date; grep -rn --include="*h" include ~/gitRepository/UsefulWheels/
20231231日 星期日 22:18:09 CST
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/BasicInclude.h:4:#include <iostream>
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/BasicInclude.h:5:#include <string>
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/BasicInclude.h:6:#include <string.h>
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/BasicInclude.h:7:#include <vector>
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/CppWheels_Other.h:4:#include "BasicInclude.h"
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/CppWheels_Other.h:5:#include "Linuxcpptypes.h"
/home/zhaoc/gitRepository/UsefulWheels/LinuxCppInterface/CppWheels_String.h:4:#include "BasicInclude.h"
/home/zhaoc/gitRepository/UsefulWheels/CppInterface/CppWheels_String.h:4:#include <iostream>
/home/zhaoc/gitRepository/UsefulWheels/CppInterface/CppWheels_String.h:5:#include <string>
/home/zhaoc/gitRepository/UsefulWheels/CppInterface/CppWheels_String.h:6:#include <vector>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1347181.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

安装Hadoop:Hadoop的单机模式、伪分布式模式——备赛笔记——2024全国职业院校技能大赛“大数据应用开发”赛项

前言 Hadoop包括三种安装模式&#xff1a; 单机模式&#xff1a;只在一台机器上运行&#xff0c;存储是采用本地文件系统&#xff0c;没有采用分布式文件系统HDFS&#xff1b;伪分布式模式&#xff1a;存储采用分布式文件系统HDFS&#xff0c;但是&#xff0c;HDFS的名称节点…

【VRTK】【VR开发】【Unity】18-VRTK与Unity UI控制的融合使用

课程配套学习项目源码资源下载 https://download.csdn.net/download/weixin_41697242/88485426?spm=1001.2014.3001.5503 【背景】 VRTK和Unity自身的UI控制包可以配合使用发挥效果。本篇就讨论这方面的实战内容。 之前可以互动的立体UI并不是传统的2D UI对象,在实际使用中…

Mac 安装 Minikube 及解决 “[ERROR ImagePull]: failed to pull image“ 问题

文章目录 1. 引言2. Mac 安装 Docker3. Mac 安装 Minikube4. 拉起集群4.1 启动4.2 验证 5. 参考 1. 引言 Minikube 是一种轻量化的 Kubernetes 集群&#xff0c;旨在帮助开发者和学习者更好地学习和体验 Kubernetes 的功能。它可以在个人计算机的虚拟化环境中快速构建和启动 Ku…

网易有道词典不能截屏翻译,不能联网解决办法

对应版本&#xff1a; win10系统&#xff0c;联想拯救者笔记本&#xff0c;网易有道词典8.10.2.0。 网易有道词典免费下载链接&#xff1a;https://download.csdn.net/download/qq_42755734/88684985 修改代理&#xff1a; youdao.com 0 取消勾选---不更新 效果&#xff1a…

移动应用开发:揭秘内侧APP封装台的高效

在数字化浪席卷下&#xff0c;移应用已经成连接企业与用户纽带。为了抢占市场先机&#xff0c;快速发布高质量的移动应用成为业竞争的关键。侧APP封装平因此而诞生&#xff0c;成为了应开发者的得助手。以下是内侧APP封装台的全面解读&#xff0c;助在应用开发海洋中乘风破浪。…

告别HTTP,拥抱HTTPS!免费SSL证书领取指南

为什么选择HTTPS&#xff1f; HTTP和HTTPS之间的主要区别在于安全性。HTTP是一种不安全的协议&#xff0c;数据在传输过程中是明文的&#xff0c;容易受到中间人攻击。而HTTPS通过SSL&#xff08;Secure Sockets Layer&#xff09;或TLS&#xff08;Transport Layer Security&…

python期刊稿件在线投稿系统q2ud0

本系统的用户可分为管理员、投稿者、审稿人和编辑四个用户角色组成。管理员可以管理系统内所有功能&#xff0c;主要有个人中心、投稿者管理、审稿人管理、编辑管理、个人稿件管理、审核稿件管理、稿件信息管理、类型管理等功能&#xff1b;编辑登录系统主要有个人中心、审核稿…

2023-12-22 LeetCode每日一题(得到山形数组的最少删除次数)

2023-12-22每日一题 一、题目编号 1671. 得到山形数组的最少删除次数二、题目链接 点击跳转到题目位置 三、题目描述 我们定义 arr 是 山形数组 当且仅当它满足&#xff1a; arr.length > 3存在某个下标 i &#xff08;从 0 开始&#xff09; 满足 0 < i < arr.…

阿里云PolarDB数据库优惠价格表11元一天起

阿里云数据库PolarDB租用价格表&#xff0c;云数据库PolarDB MySQL版2核4GB&#xff08;通用&#xff09;、2个节点、60 GB存储空间55元5天&#xff0c;云数据库 PolarDB 分布式版标准版2核16G&#xff08;通用&#xff09;57.6元3天&#xff0c;阿里云百科aliyunbaike.com分享…

UE蓝图 RPG动作游戏(一) day15

角色状态制作 制作角色动画混合空间 创建一个动混合空间 添加动作在混合空间 动画蓝图 创建一个动画蓝图 先使用混合空间进行移动&#xff0c;后续优化后再使用状态机 编写垂直水平速度逻辑初始化&#xff0c;获取到此动画的角色组件 获取Horizontal与Vertical的速度逻辑 …

vue项目表单使用正则过滤ip、手机号

import useFormValidate from /hooks/useFormValidatesetup(props, { emit }) {const { validateName, validateIPAndPort } useFormValidate()const state reactive({workFaceInfo: props.info?.id ? props.info : {},sysTypeData: props.sysType,formRules: {name: [{req…

QDockWidget学习

一、使用方法 QT之QDockWidget使用详解_qt dockwidget-CSDN博客 重点摘抄&#xff1a; 二、QDockWidget的常用函数&#xff1a; QDockWidget嵌套布局详解-实现Visual Studio布局_http://blog.csdn.net/czyt1988/article/details/5120-CSDN博客 摘抄&#xff1a;

【Redis-05】Redis如何实现保存键值对的保存及过期键的管理策略

在之前的文章我们介绍过&#xff0c;Redis服务器在启动之初&#xff0c;会初始化RedisServer的实例&#xff0c;在这个实例中存在很多重要的属性结构&#xff0c;同理本篇博客中介绍的数据库实现原理也会和其中的某些属性相关&#xff0c;我们继续看一下吧。 1.服务器和客户端…

大数据概念:数据网格和DataOps

数据网格&#xff08;Data Mesh&#xff09; 一种新型的数据架构模式&#xff0c;旨在解决传统数据架构中存在的一些问题&#xff0c;例如数据孤岛、数据冗余、数据安全等。数据网格将数据作为一种服务&#xff0c;通过在分布式环境中提供数据服务&#xff0c;实现数据的共享和…

Apollo自动驾驶系统:实现城市可持续交通的迈向

前言 「作者主页」&#xff1a;雪碧有白泡泡 「个人网站」&#xff1a;雪碧的个人网站 ChatGPT体验地址 文章目录 前言引言&#xff1a;1. 什么是微服务架构&#xff1f;2. 微服务架构的组成要素3. 微服务架构的挑战和解决方案4. 微服务架构的可扩展性和弹性 第二部分&#x…

回首2023: 程序员跳出舒适圈

1 前言 今天的冬日暖阳高照&#xff0c;照耀着我穿着羽绒服的身体&#xff0c;让我感到火一般的燥热&#xff0c;仿佛错觉中已经到了阳春三月。刚刚把孩子洗好&#xff0c;我坐在电脑前&#xff0c;准备整理一下思绪&#xff0c;回顾一下2023年的生活和工作。 2 2023 回顾 回…

信号与线性系统翻转课堂笔记17——z变换及其性质

信号与线性系统翻转课堂笔记17——z变换及其性质 The Flipped Classroom17 of Signals and Linear Systems 对应教材&#xff1a;《信号与线性系统分析&#xff08;第五版&#xff09;》高等教育出版社&#xff0c;吴大正著 一、要点 &#xff08;1&#xff09;序列的z变换…

Hive生产调优介绍

1.Fetch抓取 Fetch抓取是指&#xff0c;Hive中对某些情况的查询可以不必使用MapReduce计算。例如&#xff1a;SELECT * FROM employees;在这种情况下&#xff0c;Hive可以简单地读取employee对应的存储目录下的文件&#xff0c;然后输出查询结果到控制台。 在hive-default.xml…

DFS

目录 DFS 实现数字全排列 N 皇后问题 DFS 算法的理解 优先考虑深度&#xff0c;换句话说就是一条路走到黑&#xff0c;直到无路可走的情况下&#xff0c;才会选择回头&#xff0c;然后重新选择一条路。空间复杂度&#xff1a;O&#xff08;h&#xff09;和高度成正比 不具…

【每日一题】【12.29】 - 【12.31】年终收尾

&#x1f525;博客主页&#xff1a; A_SHOWY&#x1f3a5;系列专栏&#xff1a;力扣刷题总结录 数据结构 云计算 数字图像处理 力扣每日一题_ 这三天的题目难度相对较小&#xff0c;基本都为模拟题&#xff0c;但是第二三的题目年份贡献类型很有代表性。2023年最后三天年终收…