Anybody here can help me to figure thie perl script out?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Anybody here can help me to figure thie perl script out?
# 1  
Old 10-19-2011
Question Anybody here can help me to figure thie perl script out?

Gey guys,

I'm a new learner of perl. Now I encountered a problem when I tried to get the output below from the input file.

Input:
Code:
5'h1f, 16'h8210 
write, 5'h10, 16'h0000  
write, 5'h11, 16'h0000 
5'h1f, 16'hffd0 
write, 5'h1e, 16'h0000  
5'h1f, 16'h8310 
read, 5'h10, rd_data
5'h1f, 16'hffd0 
read, 5'h1e,  rd_data 
write, 5'h1e, aerstrap


Then I wanna the final output to be:
Code:
  
5'h1f,  16'h8210, write, 5'h10, 16'h0000 
5'h1f, 16'h8210, write, 5'h11, 16'h0000  
5'h1f, 16'hffd0, write, 5'h1e, 16'h0000 
5'h1f, 16'h8310, read, 5'h10,  rd_data
5'h1f, 16'hffd0, read, 5'h1e, rd_data 
5'h1f, 16'hffd0,write,  5'h1e, aerstrap

That means if we scanned a "5'h1f", we should cut and paste that line with the following n(n=1 or 2) lines which begin with "write" or "read".

Thank you for your help.

Moderator's Comments:
Mod Comment Please use code tags

Last edited by zaxxon; 10-20-2011 at 05:48 AM.. Reason: code tags
# 2  
Old 10-19-2011
Here is a non-perl possible solution:
Code:
#!/usr/bin/ksh
while read mLine; do
  m1to5=$(echo ${mLine} | cut -c1-5)
  if [[ "${m1to5}" = "5'h1f" ]]; then
    mBase=${mLine}
  else
    echo ${mBase}', '${mLine}
  fi
done < File

# 3  
Old 10-19-2011
Thx~

However I still need the perl script coz it is part of my current perl...

Best
# 4  
Old 10-20-2011
Hi sunbaby,

Try:
Code:
$ cat infile 
5'h1f, 16'h8210
write, 5'h10, 16'h0000
write, 5'h11, 16'h0000
5'h1f, 16'hffd0
write, 5'h1e, 16'h0000
5'h1f, 16'h8310
read, 5'h10, rd_data                                                                                                                                                                                                                         
5'h1f, 16'hffd0                                                                                                                                                                                                                              
read, 5'h1e, rd_data                                                                                                                                                                                                                         
write, 5'h1e, aerstrap                                                                                                                                                                                                                       
$ cat script.pl
use warnings;                                                                                                                                                                                                                                
use strict;                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
die qq[Usage: perl $0 input-file\n] unless @ARGV == 1;                                                                                                                                                                                       
                                                                                                                                                                                                                                             
my ($begin_line);                                                                                                                                                                                                                            
while ( <> ) {                                                                                                                                                                                                                               
        next if m/\A\s*\z/;                                                                                                                                                                                                                  
        chomp;                                                                                                                                                                                                                               
        if ( m/\A5'h1f/ ) {                                                                                                                                                                                                                  
                $begin_line = $_;                                                                                                                                                                                                            
                next;                                                                                                                                                                                                                        
        }                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                             
        printf qq[%s\n], join qq[, ], $begin_line, $_;                                                                                                                                                                                       
}                                                                                                                                                                                                                                            
$ perl script.pl infile                                                                                                                                                                                                    
5'h1f, 16'h8210, write, 5'h10, 16'h0000                                                                                                                                                                                                      
5'h1f, 16'h8210, write, 5'h11, 16'h0000                                                                                                                                                                                                      
5'h1f, 16'hffd0, write, 5'h1e, 16'h0000                                                                                                                                                                                                      
5'h1f, 16'h8310, read, 5'h10, rd_data                                                                                                                                                                                                        
5'h1f, 16'hffd0, read, 5'h1e, rd_data                                                                                                                                                                                                        
5'h1f, 16'hffd0, write, 5'h1e, aerstrap

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Oracle/SQLPlus help - ksh Script calling .sql file not 'pausing' at ACCEPT, can't figure out why

Hi, I am trying to write a script that calls an Oracle SQL file who in turns call another SQL file. This same SQL file has to be run against the same database but using different username and password at each loop. The first SQL file is basically a connection test and it is supposed to sort... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

4. Shell Programming and Scripting

Perl : embedding java script with cgi perl script

Hi All, I am aware that html tags can be embedded in cgi script as below.. In the same way is it possible to embed the below javascript in perl cgi script ?? print("<form action="action.htm" method="post" onSubmit="return submitForm(this.Submitbutton)">"); print("<input type = "text"... (1 Reply)
Discussion started by: scriptscript
1 Replies

5. UNIX for Dummies Questions & Answers

[Solved] ksh script - can't figure out what's wrong

Hi! (I guess this could of gone into the scripting forum, but Unix for Dummies seemed more appropriate. Please note that I am not in school, so Homework doesn't seem appropriate either. You guys can let me know if you think otherwise.) I am following an exercise in a book on ksh scripting which... (2 Replies)
Discussion started by: sudon't
2 Replies

6. Shell Programming and Scripting

cant figure out the error in this script (adding numbers in a string) using ubantu shell

hii please help me this is the script num=$1 sum=0 while do x=`expr $num % 10` sum=`expr $sum + $x` num=`expr $num / 10` done echo "Summation is $sum" it is giving error pratyush@ubuntu:~$ sh shell.sh 123 shell.sh: 11: 123: not found Summation is 0 (3 Replies)
Discussion started by: Pratyush Sakhle
3 Replies

7. Shell Programming and Scripting

calling a perl script with arguments from a parent perl script

I am trying to run a perl script which needs input arguments from a parent perl script, but doesn't seem to work. Appreciate your help in this regard. From parent.pl $input1=123; $input2=abc; I tried calling it with system("/usr/bin/perl child.pl $input1 $input2"); and `perl... (1 Reply)
Discussion started by: grajp002
1 Replies

8. Shell Programming and Scripting

Bash script - im missing something and cant's figure out what

I just put together a script for work that will essentially automate the migration of our Windows fileserver to my newly created Debian based SAMBA server. My script will create the necessary directories then copy the data over to my new server, after that it will set the ACL's by using... (3 Replies)
Discussion started by: binary-ninja
3 Replies

9. Shell Programming and Scripting

Spent all day trying to figure this script out...

Before I begin with the question, I just want to point out that I just started learning unix in the middle of last week, so my code (and knowledge of how unix operates) is weak sauce. I took my best stab at this question but it's just not working. Assignment: Create a script named... (1 Reply)
Discussion started by: ashkali1
1 Replies

10. UNIX for Dummies Questions & Answers

i can not figure this out

I am having problems scripting in UNIX. I am currently attending school and for the first time I am being introduced to scripting. My problem is I am supposed to enhance the spell_check by adding a third optional argument. The third argument is to specify a list of words to be added to the... (1 Reply)
Discussion started by: steph
1 Replies
Login or Register to Ask a Question