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

边栏推荐
- How to realize the function of adding watermark
- PaddleLite 编译以及代码跑通复盘
- 报表控件FastReport与StimulSoft功能对比
- Start from scratch blazor server (3) -- add cookie authorization
- 暑假集训week1
- The heavy | open atomic school source activity was officially launched
- Kunlunbase instruction manual (III) data import & synchronization
- 2022最新 wifi大师小程序独立版3.0.8
- JVM知识点详细整理(长文警告)
- Starrocks technology insider: how to have both real-time update and fast query
猜你喜欢

Getting started with pytoch

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

2022最新 wifi大师小程序独立版3.0.8
小笑授权系统V5.0开心版

Understand what a binary tree is (types, traversal methods, definitions of binary trees)

Niuke net brush questions

Peking University open classes are coming! Welcome to the "AI for science" class

会议OA项目----我的审批

Leetcode bit operation

DoD 和 DoR,消减「认知偏差」的两大神器
随机推荐
GPO: using PowerShell scripts in start/logon
8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice
The 2022 open atom global open source summit opened in Beijing
The heavy | open atomic school source activity was officially launched
Survival analysis using rtcga clinical data
基于flask写的一个小商城mall项目
一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
Basic construction of QT project
AI model risk assessment Part 2: core content
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
Svn revision keyword
Factoextra: visualization of multivariate statistics
Use tidymodels to solve the binary logistic model
深入理解C# 可空类型
开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
How to synchronize when the primary and sub warehouses are modified?
Luogu p4185 [usaco18jan]mootube g problem solution
Pytorch 入门
PHP basics uses arrays to save data
Kunlun storage vs PostgreSQL OLTP test