0

SOLUTION

Initial solution

find . -type f -exec sed -i ':a;N;$!ba;s/\n //g' {} + | grep -l "672.15687489"

Initial post:

I was wondering how to search for a pattern in a file. The but is that the pattern is spanned in two lines and I don't know in which part the pattern is divided.

Example:

The pattern:  _"672.15687489"_

But, in the file could be one of these several options:

672.15\n687489

672.156\n87489

672.1568\n7489

672.15687\n489

...

I don't care how the pattern is splitted, the only thing I want is the name of the file that have the pattern.

Another.Chemist
  • 2,386
  • 3
  • 29
  • 43
  • 1
    possible duplicate of [How to find patterns across multiple lines using grep?](http://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep) – Andy Lester Mar 06 '13 at 05:11
  • 1
    Kindly post the solution as an answer to your own question, rather than to edit the question. – Lundin Mar 06 '13 at 07:42
  • If a file contains 672x15687489 (which has an `x` where you wrote `.`), do you want to see the file name? Your `grep` will select that, and any other string containing any other character in place of the `.` since the dot is a metacharacter. The chances of the mismatch occurring are low to non-existent, so it is not a big deal, but if you really want a dot as the 4th character, you have to look for it with `\.` or `[.]` in the `grep` pattern. – Jonathan Leffler Mar 06 '13 at 14:26
  • Don't edit an answer into your post, keep it to the question only. Just accept the answer that lead you to the solution. If you have something to add to it, either comment on it, or post your own answer that expands on the one you accept. – millimoose Mar 06 '13 at 20:37

1 Answers1

2

Thank you for the hilarious sed | grep "solution":

sed -i ':a;N;$!ba;s/\n //g' {} + | grep -l "672.15687489"

but in reality, just use awk. Here's a GNU awk solution that won't change your original file, doesn't require multiple commands and a pipe, and does not require a James Bond decoder ring to understand an arcane combination of letters and punctuation marks:

$ cat file
foo
672.15
687489
bar
$ gawk -v RS='\0' '{gsub(/\n/,"")} /672.15687489/{print FILENAME; exit}' file
file

All you need to know is that setting RS to the Null character tells gawk to read the whole file as a single record. Other awks may or may not support this but GNU awk does. There are other awk solutions, all of which would be clearer than the posted sed+grep solution.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185