1

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?

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

4 Answers 4

2

grep

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.

Example

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 
sample1.txt
sample2.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 
sample1.txt:10
sample1.txt:100
sample2.txt:10
sample2.txt:100

pcregrep

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*
sample1.txt:11
12
sample2.txt:11
12
2

Perl gives you a "file slurp" mode:

echo "foo
bar
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.

1

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

3
  • 2
    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, 2014 at 21:45
  • maybe give an example of what you're trying to match?
    – OneOfOne
    Feb 12, 2014 at 21:47
  • ack searches are line-based, just like grep. Feb 12, 2014 at 21:48
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .