List Info

Thread: matching exact lines in files




matching exact lines in files
user name
2006-10-04 18:55:22
In article <ftOUg.4846$Y24.700newsread4.news.pas.earthlink.net>,
Jim Showalter  <jshowaltergmail.com> wrote:
>Specifically, I want is to find all lines in a.txt that
match any of

So far, this sounds like a grep.

>the following three groups of lines in b.txt:
>
>1-0
>[Event "ICC"]
[snip]

>1-0
>
>[Event "ICC"]
>
>should not match because there is an intervening blank
line.

Oh, a multi-line pattern. This does what you asked for:

perl -nle 'print "$prevn$_" if $prev =~ m^(1-0|0-1|1/2-1/2)$ &&
                                $_ eq q/[Event
"ICC"]/; $prev=$_' file

>Also, if anyone knows how I can accomplish this easily
with vim, that
>would even be better.  But I have never been able to
figure out how
>to include end-of-lines or blank-lines in vim searches.

I can search for newlines just fine with vim, using n.

>Or maybe I have to use some other tools?

If I can guess your intent (rather than just giving you
exactly what you
asked for), these are probably what you really want:

# Find all lines that exactly match [Event "ICC"]
and print them, together
# with 1 preceding line.
grep -B 1 -Fx '[Event "ICC"]' file

# Same thing, then get rid of the [Event "ICC"]
lines and the separators
# leaving only the lines that preceded the [Event
"ICC"] lines
grep -B 1 -Fx '[Event "ICC"]' file |
  grep -Fxv -e '[Event "ICC"]' -e '--'

# Building on the previous example, group and count the
occurrences of each
# result
grep -B 1 -Fx '[Event "ICC"]' file |
  grep -Fxv -e '[Event "ICC"]' -e '--' | sort |
uniq -c

-- 
The attackerx92s overall goal would very probably be to
convince other users
to run an unsafe program, by using the digital signature to
convince them
that it is actually bona fide Microsoft software and
therefore safe to run.
            -- security bulletin MS01-017 ushers in a new
definition of "safe"
_______________________________________________
help-gnu-utils mailing list
help-gnu-utilsgnu.org

http://lists.gnu.org/mailman/listinfo/help-gnu-utils
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )