当前位置:网站首页>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 :

边栏推荐
- Talk about the establishment of performance testing environment
- DoD 和 DoR,消减「认知偏差」的两大神器
- 『知识集锦』一文搞懂mysql索引!!(建议收藏)
- 2022最新 wifi大师小程序独立版3.0.8
- 站点数据收集-Scrapy使用笔记
- Using xgboost with tidymodels
- [unity, C #] character keyboard input steering and rotation
- 北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂
- How to realize the function of adding watermark
- Use tidymodels to solve the binary logistic model
猜你喜欢

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

Using Riemann sum to calculate approximate integral in R language

DOD and Dor, two artifacts to reduce "cognitive bias"

阿里P8爆出的这份大厂面试指南,看完工资暴涨30k!

基于flask写的一个小商城mall项目

北京大学公开课重磅来袭!欢迎走进「AI for Science」课堂

会议OA项目(五)---- 会议通知、反馈详情

Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!

LeetCode二叉树系列——144.二叉树的前序遍历

DNS协议、ICMP协议、NAT技术
随机推荐
StarRocks 技术内幕:实时更新与极速查询如何兼得
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
如何使用 grep 跨多行查找模式匹配
重磅 | 基金会为白金、黄金、白银捐赠人授牌
就这?TypeScript其实并不难!(建议收藏)
基本.分块
Luogu p4185 [usaco18jan]mootube g problem solution
Start from scratch blazor server (3) -- add cookie authorization
Basic construction of QT project
How to realize the function of adding watermark
Meeting OA project (V) -- meeting notice and feedback details
Use tidymodels to solve the binary logistic model
Steps of project explanation in interview
R language brca MRNA data set analysis
Spark efficient data analysis 01. Establishment of idea development environment
Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase
Survival analysis using rtcga clinical data
如何使用“COPY –link”加速 Docker 构建和优化缓存
R language uses data set veteran for survival analysis
Discussion on the application of arcing smart electricity in elderly care institutions