Printing next 6 lines from of pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing next 6 lines from of pattern match
# 1  
Old 12-04-2013
Question Printing next 6 lines from the line of pattern match in PERL

Hi, i have a big file having many opcodes.
Code:
 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
Code:
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

Last edited by twistedpair; 12-04-2013 at 01:52 PM..
# 2  
Old 12-04-2013
Hi,
Maybe as:
Code:
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.
# 3  
Old 12-04-2013
How about giving the ole'trusty'forgotten ex utility a try...
Code:
ex -sc '/opcode="10010000100010000001"/;+6 p | q!' file

This User Gave Thanks to shamrock For This Post:
# 4  
Old 12-04-2013
Code:
awk -v op='10010000100010000001' -v qq='"' '$0 ~ "opcode=" qq op qq {l=7} l&&l--' myFile


Last edited by vgersh99; 12-04-2013 at 12:50 PM..
# 5  
Old 12-04-2013
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 .
# 6  
Old 12-04-2013
As this ?
Code:
$ 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';

Code:
$ 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.
This User Gave Thanks to disedorgue For This Post:
# 7  
Old 12-04-2013
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;})
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help! Printing out CSV output from awk Pattern Match

Hi, I need to search for a word using Awk and print out the line the word is in and every line after the search phrase until I hit this #------------. Then I need to send it to a csv file. So basically the input file format is like this:... (1 Reply)
Discussion started by: An0mander
1 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

6. UNIX for Dummies Questions & Answers

Printing nth and n+1th line after a pattern match

Hi , I want to print the nth and n+1 lines from a file once it gets a pattern match. For eg: aaa bbb ccc ddd gh jjjj If I find a match for bbb then I need to print bbb as well as 3rd and 4th line from the match.. Please help..Is it possible to get a command using sed :) (6 Replies)
Discussion started by: saj
6 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

9. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

10. Shell Programming and Scripting

SED printing just parts of lines that match an expression

Hi - I am guessing this is fairly simple for someone .. but I can not quite figure it out. I need a sed command to print just parts of lines from a file. e.g. filea.txt 4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk Message log started Message log ended 4710452 : Success : MODIFY :... (7 Replies)
Discussion started by: sniper57
7 Replies
Login or Register to Ask a Question