Your example suggests that the entire search is to be performed on data appearing once in a single line.

This is due to missing <code> tags. As evidenced by the HTML source code, the OP is as follows:


Hi,

I would like to know how to grep the first occurrence of a string above(upwards) a reference string.

for (e.x.)in the below set, I would like to get the first occurrence of "AAA" above "XXX".

AAA
BBB
AAA
CCC
XXX

Thanks,
Venkat


To the OP - if you rephrase your problem, you have the solution almost there:

I want to keep track of a pattern I seek for, and output the found string after finding a reference string

which is what ww did for you above already. Hence a possible solution is

#!/usr/bin/perl # file grep.pl my $sought = shift; # from @ARGV my $reference = shift; $sought && $reference or die "usage: $0 soughtstring refstring files\n +"; my $found; while(<>) { chop; # strip newline /$sought/ and $found = $_; # or, if you want the very first occurence, don't # overwrite the variable (see perlop for '||='): # /$sought/ and $found ||= $_; if (/$reference/) { print "$ARGV: '$found'\n" if $found; $found = ''; } }

to be used as

$ perl grep.pl AAA XXX example.txt

which invoked upon this example.text

1 YYY 2 first AAA 3 BBB 4 CCC 5 second AAA 6 freida 7 XXX 8 GGG 9 FFF 10 third AAA 11 XXX 12 ozymandis 13 BBB 14 blorflydick 15 XXX 16 fourth AAA

produces this:

example.txt: ' 5 second AAA' example.txt: ' 10 third AAA'

Lines 2 and 16 are not printed. If you use the commented alternative, line 2 is printed instead of line 5.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re^2: Grep the first occurrence of a string above a reference string by shmem
in thread Grep the first occurrence of a string above a reference string by venkat1312

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.