当前位置:网站首页>How to use grep to display file names and line numbers before matching lines
How to use grep to display file names and line numbers before matching lines
2022-07-29 10:58:00 【mikes zhang】
grep It's a Linux Utilities , It is usually used to search the contents of a file or any input passed to it . When searching multiple files , It is useful to display the file name and line number , Especially when using it in shell When automating in scripts .
Use grep Show filename
By default , If you pass multiple files to , For the sake of clarity grep, It will show filename: Before matching rows . You can actually turn off this behavior with flags , The -h The flag never shows the file name :
however , If you only pass one file to grep, By default, it will not display the file name . In the use of shell When scripts are automated , This could be a problem , Because you may not know how many files are in the directory , And it may break the automation that depends on file names .
The simple solution is to use uppercase -H sign , It is the opposite of the file name ,-h And the file name will be opened anyway , Even if only one file is passed as input .
grep -H "foo" file
The -H The logo has another unexpected but useful effect —— When and from The input of ( for example Unix The Conduit ) When pairing stdin, It will (standard input): Print instead of file name .
Use grep According to the line Numbers
You can also associate it with -n Flags are used together to get line numbers :
grep -Hn "foo"
accord with POSIX
Medium -H sign grep Do not conform to the POSIX, And in some more obscure ones based on Unix Is not available in the operating system of . Fortunately, , You can use a hack, By way of /dev/null The forged second file input is passed to grep, This will trick it into thinking that there are multiple files :
边栏推荐
- Start from scratch blazor server (3) -- add cookie authorization
- Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
- Meituan and hungry were interviewed by Hangzhou supervisors to implement the responsibility of food safety management and prohibit malicious competition
- 正则表达式匹配网址
- Regular expression matching URL
- 如何使用“COPY –link”加速 Docker 构建和优化缓存
- matplotlib中文问题
- 大伟 Golang之路
- VMware: use commands to update or upgrade VMware esxi hosts
- Leetcode bit operation
猜你喜欢
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
如何在匹配行之前使用 grep 显示文件名和行号
Pytorch 入门
Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
Niuke net brush questions
TCP和UDP
Conference OA project - my approval
How to realize the function of adding watermark
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
随机推荐
QWidget、QDialog、QMainWindow 的异同点
GPO: using PowerShell scripts in start/logon
Peking University open classes are coming! Welcome to the "AI for science" class
Hugo NexT V4 介绍
即学即用的问题解决思维,给无意识的生活装上“后视镜”
LeetCode_416_分割等和子集
How can agile development reduce cognitive bias in collaboration| Agile way
会议OA项目----我的审批
StarRocks 技术内幕:实时更新与极速查询如何兼得
Package delivery (greedy)
8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
2.安装MySQL
The heavy | open atomic school source activity was officially launched
js两个数组对象进行合并去重
Svn revision keyword
『面试知识集锦100篇』1.面试技巧篇丨HR的小心思,你真的懂吗?
Kunlunbase instruction manual (II) best practices for peer-to-peer deployment
Package Delivery(贪心)
Kunlunbase instruction manual (III) data import & synchronization
一键搭建博客:如何使用WordPress插件搭建专属博客