[英]Searching specific lines of files using GREP
我有一个包含许多文本文件的目录。 我想在文件的特定行中搜索给定的字符串(例如仅在每个文件的第 2 行和第 3 行中搜索“abc”)。 然后,当我找到匹配项时,我想打印匹配文件的第 1 行。
我的方法 - 我正在使用 -n 选项进行 grep 搜索并将 output 存储在不同的文件中,然后在该文件中搜索行号。 然后我试图获取文件名,然后打印出它的第一行。
使用我上面提到的方法我无法获得正确文件的文件名,即使我知道这种方法非常冗长。
对此有更好更快的解决方案吗?
例如。
1.txt
file 1
one
two
2.txt
file 2
two
three
我想使用grep
在每个文件的第 2 行中搜索“two”,然后使用匹配打印文件的第一行。 在这个例子中是 2.txt 而 output 应该是“文件 2”
我知道使用sed
/ awk
更容易,但是有没有办法使用grep
来做到这一点?
使用sed
代替(GNU sed):
解析.sed
1h # Save the first line to hold space
2,3 { # On lines 2 and 3
/my pattern/ { # Match `my pattern`
x # If there is a match bring back the first line
p # and print it
:a; n; ba # Loop to the end of the file
}
}
像这样运行它:
sed -snf parse.sed file1 file2 ...
或者作为单线:
sed -sn '1h; 2,3 { /my pattern/ { x; p; :a; n; ba; } }' file1 file2 ...
您可能还想发出文件名,例如您的示例数据:
解析2.sed
1h # Save the first line to hold space
2,3 { # On lines 2 and 3
/two/ { # Match `my pattern`
F # Output the filename of the file currently being processed
x # If there is a match bring back the first line
p # and print it
:a; n; ba # Loop to the end of the file
}
}
像这样运行它:
sed -snf parse2.sed file1 file2 | paste -d: - -
Output:
file1:file 1
file2:file 2
$ awk 'FNR==2{if(/one/) print line; nextfile} FNR==1{line=$0}' 1.txt 2.txt
file 1
$ awk 'FNR==2{if(/two/) print line; nextfile} FNR==1{line=$0}' 1.txt 2.txt
file 2
FNR
将具有当前正在读取的文件的行号
FNR>=2 && FNR<=3
FNR==1{line=$0}
会保存第一行的内容以备后用nextfile
,但如果您需要删除它,该解决方案仍然有效(虽然速度较慢)使用grep
和bash
:
# Grep for a pattern and print filename and line number
grep -Hn one file[12] |
# Loop over matches where f=filename, n=match-line-number and s=matched-line
while IFS=: read f n s; do
# If match was on line 2 or line 3
# print the first line of the file
(( n == 2 || n == 3 )) && head -n1 $f
done
Output:
file 1
仅使用grep
, cut
和|
(管道):
grep -rnw pattern dir | grep ":line_num:" | cut -d':' -f 1
解释
grep -rnw pattern dir
它返回找到模式的文件的名称以及行号。 它是 output 会是这样的
path/to/file/file1(.txt):8:some pattern 1
path/to/file/file2(.txt):4:some pattern 2
path/to/file/file3(.txt):2:some pattern 3
现在我正在使用另一个grep
来获取具有正确行号的文件(例如包含第 2 行模式的文件)
grep -rnw pattern dir | grep ":2:"
它的 output 将是
path/to/file/file3(.txt):2:line
现在我正在使用 cut 来获取文件名
grep -rnw pattern dir | grep ":2:" | cut -d':' -f 1
它将 output 这样的文件名
path/to/file/file3(.txt)
PS - 如果您想从文件名中删除“path/to/file/”,您可以使用rev
然后cut
并再次rev
,您可以自己尝试或查看下面的代码。
grep -rnw pattern dir | grep ":2:" | cut -d':' -f 1 | rev | cut -d'/' -f 1 | rev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.