Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes.

 if (opcode="01110000000100000000" ) then --fadd
      result.opcode         := "01110000000100000000";
      result.s0     := '1';
      result.s1     := '1';
      result.s2     := '0';
    result.inst      := '0';
    result.scalar     := '1';
elsif (opcode="10010000100010000001" ) then --add
      result.opcode         := "10010000100010000001";
      result.s0      := '1';
      result.s1      := '1';
      result.s2      := '0';
    result.inst      := '0';
    result.scalar     := '1';

Now i want to write a PERL script which automatically copy only the wanted (which are inputs from another file) opcode related information of 7 lines (after iop match has found, the next 6 lines) for example only the second one

elsif (instr_opcode="10010000100010000001" ) then --add
      result.opcode         := "10010000100010000001";
      result.s0_valid      := '1';
      result.s1_valid      := '1';
      result.s2_valid      := '0';
    result.inst_128      := '0';
    result.scalar_64     := '1';

into another file. i am always thinking in c style of storing the next 6 linies in an array. But in perl there must be an easy way. Plz help me out. Thanks in advance

Hi,
Maybe as:

sed -n -e '/elsif/{$p;N;$p;N;$p;N;$p;N;$p;N;$p;N;p;}' file_source >file_dest

Here, where line content elsif, print line and six after.

Regards.

How about giving the ole'trusty'forgotten ex utility a try...

ex -sc '/opcode="10010000100010000001"/;+6 p | q!' file
1 Like
awk -v op='10010000100010000001' -v qq='"' '$0 ~ "opcode=" qq op qq {l=7} l&&l--' myFile

Hi all, Thanks for reply. But i need a perl command for the above requirement, assuming the opcode is in a variable which i got from another file .

As this ?

$ cat file1
 if (opcode="01110000000100000000" ) then --fadd
      result.opcode         := "01110000000100000000";
      result.s0     := '1';
      result.s1     := '1';
      result.s2     := '0';
    result.inst      := '0';
    result.scalar     := '1';
elsif (opcode="10010000100010000001" ) then --add
      result.opcode         := "10010000100010000001";
      result.s0      := '1';
      result.s1      := '1';
      result.s2      := '0';
    result.inst      := '0';
    result.scalar     := '1';
$ xx='10010000100010000001' perl -sane 'if($_ =~ /opcode="$ENV{xx}"/){$l=7}; if($l && $l--){print $_}' file1
elsif (opcode="10010000100010000001" ) then --add
      result.opcode         := "10010000100010000001";
      result.s0      := '1';
      result.s1      := '1';
      result.s2      := '0';
    result.inst      := '0';
    result.scalar     := '1';

Regards.

1 Like

That's an interesting passing of argument to perl in loop mode. New for me.
(The previous sed print can be smoothed {N;N;N;N;N;N;p;} )

the $p in my sed command is in case where number of line < 6

Regards.

1 Like