Sponsored Content
Top Forums Shell Programming and Scripting [perl script] print the assembly instruction and count the occurence Post 302932851 by ongoto on Sunday 25th of January 2015 07:16:09 PM
Old 01-25-2015
@acdc
I played with your original code a little and shamelessly stole the %COUNTS array from derekludwig. I came up with this without much regex...
Code:
#!/usr/local/bin/perl -w

use warnings;
use strict; 

# my @s_words = 0;
# my $fun_name = 0;
# my $file_name = 0;
# my $remove_hex = 0;
# my $word = 0;

my $filename = 'C:\data1.txt';
my @opcode_var = 0;
my $output_var = 0;
my %COUNTS = ();

open(my $FILE, '<', $filename) or die "Could not read from $filename";
my @lines = <$FILE>;
chop @lines;

foreach my $line(@lines) 
{
    if ($line =~ /0x*/)
    {
        chop ($line);
        @opcode_var = split(/ /,$line);
        
        if($opcode_var[2] =~ /0x*/)
        {
            $output_var = "$opcode_var[3] $opcode_var[4]";
        }
        else
        {
            if($opcode_var[1] =~ /0x*/)
            {
                if ($opcode_var[3] =~ /[;]+/)
                {
                    $output_var = "$opcode_var[2]";
                }
                else
                {
                    $output_var = "$opcode_var[2] $opcode_var[3]";
                }
            }
        }
    }
    print "$output_var\n";
    $COUNTS{$output_var}++;
}
print "\n";
while (my ($w, $n) = each %COUNTS)
{
    # printf STDERR "\(\%d\) \%s\n", $n, $w;
    printf STDERR "%-14s - occurred %-2d %s\n", $w, $n, $n < 2 ? "time" : "times";
}

close($FILE);

# eof #

# output
# ------
# BRA.l 0x60
# RETI
# MOV R0L,R0L
# RETI
# MOV R0L,R0L
#
# BRA.l 0x60     - occurred 1  time
# MOV R0L,R0L    - occurred 2  times
# RETI           - occurred 2  times
# or ...
# (1) BRA.l 0x60
# (2) MOV R0L,R0L
# (2) RETI


Last edited by ongoto; 01-26-2015 at 02:02 AM.. Reason: changed $filename
This User Gave Thanks to ongoto For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the number of occurence of perticular word from file

I want to count the number of occurence of perticular word from one text file. Please tell me "less" command is work in ksh or not. If it is not working then instead of that which command will work. :confused: (40 Replies)
Discussion started by: rinku
40 Replies

2. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies

3. UNIX for Advanced & Expert Users

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution.:) (1 Reply)
Discussion started by: sumit207
1 Replies

4. UNIX for Dummies Questions & Answers

How to count the occurence of a character in a line

Suppose i have data like :- 1,2,3,4,5 a,b,c x,y,z,t I want to count the occurence of , (comma) in every line. Waiting for a solution. (5 Replies)
Discussion started by: sumit207
5 Replies

5. Programming

'seg' assembly instruction in .s file

Is this x86? I encountered this instruction and can't seem to find any info on what it does anywhere. Any ideas? This is how it appears: seg es (4 Replies)
Discussion started by: stevenswj
4 Replies

6. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

7. Shell Programming and Scripting

Count number of character occurence but not from quotation marks

I have the following string: 31-01-2012, 09:42:37;OK;94727132638;"Mozilla/5.0 (Linux; U; Android 2.2.1)";3G;WAP;I need a script which is counting the occurrence of semicolons ( ; ) but exclude the ones from the quotation marks. In the string given as example there are 8 semicolons but the script... (3 Replies)
Discussion started by: calinlicj
3 Replies

8. UNIX for Dummies Questions & Answers

How to print first occurence

Hi there, how can i print the first pattern occurrence in a .log file? I want to print the filename of the first 17262? I tried but all I can do is print all the lines with the number 17262? I tried using awk and sed but nothing!:wall: I just want filename! Here´s an example: 17259... (3 Replies)
Discussion started by: BMatter
3 Replies

9. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

10. Programming

Python Count Number Of Occurence

Hello, I have a programming assignment to count number of occurrences of hours in particular file. Below is the code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" largest = None fh = open(fname) counts = dict() test = list() for line in fh: ... (2 Replies)
Discussion started by: infinitydon
2 Replies
Mail::Mbox::MessageParser::Cache(3pm)			User Contributed Perl Documentation		     Mail::Mbox::MessageParser::Cache(3pm)

NAME
Mail::Mbox::MessageParser::Cache - A cache-based mbox folder reader SYNOPSIS
#!/usr/bin/perl use Mail::Mbox::MessageParser; my $filename = 'mail/saved-mail'; my $filehandle = new FileHandle($filename); # Set up cache Mail::Mbox::MessageParser::SETUP_CACHE( { 'file_name' => '/tmp/cache' } ); my $folder_reader = new Mail::Mbox::MessageParser( { 'file_name' => $filename, 'file_handle' => $filehandle, 'enable_cache' => 1, } ); die $folder_reader unless ref $folder_reader; warn "No cached information" if $Mail::Mbox::MessageParser::Cache::UPDATING_CACHE; # Any newlines or such before the start of the first email my $prologue = $folder_reader->prologue; print $prologue; # This is the main loop. It's executed once for each email while(!$folder_reader->end_of_file()); { my $email = $folder_reader->read_next_email(); print $email; } DESCRIPTION
This module implements a cached-based mbox folder reader. It can only be used when cache information already exists. Users must not instantiate this class directly--use Mail::Mbox::MessageParser instead. The base MessageParser module will automatically manage the use of cache and non-cache implementations. METHODS AND FUNCTIONS The following methods and functions are specific to the Mail::Mbox::MessageParser::Cache package. For additional inherited ones, see the Mail::Mbox::MessageParser documentation. $ref = new( { 'file_name' => <mailbox file name>, 'file_handle' => <mailbox file handle>, }); <file_name> - The full filename of the mailbox <file_handle> - An opened file handle for the mailbox The constructor for the class takes two parameters. file_name is the filename of the mailbox. This will be used as the cache key, so it's important that it fully defines the path to the mailbox. The file_handle argument is the opened file handle to the mailbox. Both arguments are required. Returns a reference to a Mail::Mbox::MessageParser object, or a string describing the error. BUGS
No known bugs. Contact david@coppit.org for bug reports and suggestions. AUTHOR
David Coppit <david@coppit.org>. LICENSE
This software is distributed under the terms of the GPL. See the file "LICENSE" for more information. HISTORY
This code was originally part of the grepmail distribution. See http://grepmail.sf.net/ for previous versions of grepmail which included early versions of this code. SEE ALSO
Mail::Mbox::MessageParser perl v5.10.1 2009-08-09 Mail::Mbox::MessageParser::Cache(3pm)
All times are GMT -4. The time now is 05:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy