简体   繁体   English

grep for files包含特定位置的特定行中的特定文本

[英]grep for files containin specific text in a specific line at a specific position

I need to grep for filenames of files that have certain string ("OB") in a certain position (7-8) of a certain line (line 1) of the file. 我需要grep文件的文件名,该文件在文件的某一行(第1行)的某个位置(7-8)具有某些字符串(“OB”)。

What is the best way to do that. 什么是最好的方法。

How about using head to get the first line of each file, then grep with a corresponding regexp and output the line before to retain the filename: 如何使用head获取每个文件的第一行,然后使用相应的正则表达式grep并输出该行以保留文件名:

head -n1 * | grep -EB1 '^.{6}OB'

Of course, you have to change the file selection - here * - to suit your needs. 当然,您必须更改文件选择 - 此处* - 以满足您的需求。

Update: Question was updated - If you just want the filenames, just add another grep to catch the filenames given by the head command: 更新:问题已更新 - 如果您只想要文件名,只需添加另一个grep来捕获head命令给出的文件名:

head -n1 * | grep -EB1 '^.{6}OB' | grep '==>'

awk怎么样..

awk 'FNR == 1 && /^.{6}OB/ {print FILENAME; nextfile}' *

awk might be better for this job: awk可能对这项工作更好:

line=1
pos=7
len=2
awk "FNR==${line} && substr(\$0,${pos},${len})=='OB'{print FILENAME}" myfiles

Or alternatively: 或者:

awk -vl=${line} -vp=${pos} -vn=${len} 'FNR==l && substr($0,p,n)=="OB"{print FILENAME}' myfiles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM