I realized that most of the text related tools I use, grep/egrep/awk/sed are line-oriented.
I guess that is a simplified approach to search in files but I was wondering if there is another set of tools operating on file level that I am not aware.

So are there other tools that search over the full contents of a file without using the line mode?

  • Is there a point? – GnP Feb 12 '14 at 21:57
  • @gnp:Yes, learning how to use the proper tools for the proper job – Jim Feb 12 '14 at 22:15
  • "Proper job" for what? – OneOfOne Feb 12 '14 at 22:16
  • @OneOfOne:For searching.If I am interested in patterns across lines I am using the wrong tools – Jim Feb 12 '14 at 22:18
  • Related: unix.stackexchange.com/questions/18384/… – slm Feb 12 '14 at 22:27


If you're only interested in the names of the files that contain a search string 1 time you can use grep with its -l switch to do this.


Say I have 2 files full of numbers.

$ seq 100 > sample1.txt
$ seq 100 > sample2.txt

Now if I search that file for occurrences of the string "10".

$ grep -l 10 sample*.txt 

It will only return the files that contain a match 1 time, even if there are multiple lines that match. As proof, if I take the -l switch out:

$ grep 10 sample*.txt 


If you want to search for patterns across multiple lines you can use pcregrep along with its -M switch, for multi-line.

$ pcregrep -M "11[\n,]*.*12" sample*
| improve this answer | |

Perl gives you a "file slurp" mode:

echo "foo
baz" |
perl -0777 -ne 'print "found it" if /foo\s+bar/'

The -0777 option is the key. The whole file gets read into memory, where you can operate on it.

| improve this answer | |

For code, there's ack, it's awesome and can be language-specific or language-agnostic.

| improve this answer | |
  • 1
    I don't think that this is not line oriented. Since you pass how many lines around the match to display it seems to me it operates line by line – Jim Feb 12 '14 at 21:45
  • maybe give an example of what you're trying to match? – OneOfOne Feb 12 '14 at 21:47
  • ack searches are line-based, just like grep. – daniel kullmann Feb 12 '14 at 21:48

GNU coreutils documentation is nicely organized around files, lines, fields, and character oriented operations and tools that work on them. Check it out here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.