grep is used for files that contain specific text on a specific line in a specific location… here is a solution to the problem.
grep is used for files that contain specific text on a specific line in a specific location
I need to find the file name of a file with a specific string (“OB”) somewhere (7-8) on a certain line of the file (line 7-8).
What is the best way to do it.
Solution
How to get the first line of each file using head
, then use the corresponding regular expression grep
and output the previous line to preserve the file name:
head -n1 * | grep -EB1 '^.{ 6}OB'
Of course, you will have to change the file selection – here *
– to suit your needs.
Update: The issue has been updated – if you only need the filename, just add another grep to capture the filename given by the head
command:
head -n1 * | grep -EB1 '^.{ 6}OB' | grep '==>'