Fwd: Re: File search using regex

Daryl Tester dt-linuxsa at handcraftedcomputers.com.au
Thu Apr 19 08:24:58 CST 2007


D'oh, sent from wrong address.

-------- Original Message --------
Subject: Re: File search using regex
Date: Wed, 18 Apr 2007 22:03:14 +0930
From: Daryl Tester <Daryl.Tester at iocane.com.au>
To: chrisorgan at adam.com.au
CC: linuxsa at linuxsa.org.au
References: <47403.1176883613 at adam.com.au>

Chris Organ wrote:

> I have lost a severe amount of hair looking for a multi-line
> search based on exact pattern matching.

How big are the files that you're looking for this string in?
The problem with a multiline match (probably, any match) is
that if the string straddles over two or more reads then you're
going to get a false negative because the string won't match.
It's far less likely to occur with a single line match as
utilities tend to read a newline delimited strings in their
entirety.

> The test I am searching for is (exactly, including LF) is the following 4 lines:
> 
> !D2
> Invoice
> !C
> AUSTALIA EIGHT

Off the top of my medicated flu-ridden head -

Put the above into a file called /tmp/header.  Run the following:

find <dir name> -type f -print | while read f
do
    if awk '/^!D2$/,/^AUSTALIA EIGHT$/ { print }' $f | diff /tmp/header - > /dev/null
    then
        echo $f
    fi
done

The above makes a lot of assumptions, one specifically is that
what you're searching for appears at most once in the file.

-- 
Regards,
  Daryl Tester


-- 
Regards,
  Daryl Tester


More information about the linuxsa mailing list