Optimized way of doing the task in shell programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimized way of doing the task in shell programming
# 8  
Old 05-16-2008
It's a Perl script, not a shell script.
# 9  
Old 05-16-2008
Era,

Sorry for not mentioning my requirements correctly in the fist place.

My requirement is to to find out setting session lines which don't have corresponding return ws lines.

In this case, "setting session" line "returning WS" line does not need to be consecutive. for example the following sample is correct.

2008-05-15 02:15:04,675 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,731 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,681 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,740 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.

So i need to find out "setting session" lines which don't have corresponding "returning WS" lines and the linkage between those two is thread name for example [AQUEDUCT-WebContainer : 1]

Suppose if the follows pattern is there in a file then the first "setting session" line should be counted in missing things.

2008-05-15 02:15:04,675 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,731 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,681 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.


Hope this clarifies on my requirements .. i really appriciate your help on this ..

Thanks in advance
# 10  
Old 05-16-2008
Finding out what is missing is harder then, but maybe the following would work for you. It simply counts the numbers, plus for Setting and minus for Return.

Code:
#!/usr/bin/perl -n

if (m/(.*) \[DEBUG\] \[([^][]+)\] RMSConnectionFactory - Setting session/) {
    ++$open{$2};
    next;
}

if (m/(.*) \[DEBUG\] \[([^][]+)\] RMSConnectionFactory - Returning WS /) {
    if ($open{$2}) {
        --$open{$2};
    }
    else {
        warn "Return without opening session: $_";
    }
}

END {
    for my $k (keys %open)
    {
        print "$open{$k}: $k\n";
    }
}

# 11  
Old 05-16-2008
It looks good but what i would like to achieve is

the program should print the following lines of output in given sample data file

2008-05-15 02:15:05,293 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection

2008-05-15 02:15:06,127 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection


Here is the sample data file

2008-05-15 02:15:04,057 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,067 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,230 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,239 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,359 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,367 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,513 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,582 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,582 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,590 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,675 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,681 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:04,731 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:04,740 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:05,148 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:05,155 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:05,293 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:05,587 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:05,596 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:05,726 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:05,732 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:05,839 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:05,849 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:05,896 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:05,905 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:05,989 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:06,057 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:06,127 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:06,178 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:06,186 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Returning WS connection.
2008-05-15 02:15:06,323 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection.
2008-05-15 02:15:06,332 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Returning WS connection


Your inputs are great and thank you very much for that ..
# 12  
Old 05-17-2008
This one simply prints a warning if there are two consecutive Setting lines without a corresponding Returning in between.

Code:
#!/usr/bin/perl -n

if (m/(.*) \[DEBUG\] \[([^][]+)\] RMSConnectionFactory - Setting session/) {
    warn $open{$2} if $open{$2};
    $open{$2} = $_;
    next;
}

if (m/(.*) \[DEBUG\] \[([^][]+)\] RMSConnectionFactory - Returning WS /) {
    if ($open{$2}) {
	delete $open{$2} 
    }
    else {
        warn "Return without opening session: $_";
    }
}

END {
    for my $k (keys %open)
    {
        print "$open{$k}: $k\n";
    }
}

# 13  
Old 05-23-2008
Great

Thank you so much sir and sorry for late reply.Smilie
# 14  
Old 06-11-2008
Need help again

hi

I thought the previous program works for my requirement but delete is deleting all matching keys in the array and that could be the reason i am not getting missing setting state session statements from the array.

I found a very good utility 'StackedHash' perl module at

Data::StackedHash - Stack of PERL Hashes - search.cpan.org

I got it worked almost but some how it's not displaying any output. I thought of share with you so that i can get some advise on this.

Please find the complete details again here so that you don't need to read my previous email threads.

By the way, i was out of the town on medical urgency in my family and that's the reason for the delay.

Thank you in advance for your help..

Requirement:
==========
Find out setting session state connection statements which don't have corresponding Returning WS connections in a given input file

check_test_3_1.pl
==============
#!/usr/bin/perl -n

use Data::StackedHash;
tie %array1, Data::StackedHash;

if (m/(.*) \[DEBUG\] \[([^][]+)\] RMSConnectionFactory - Setting session/) {
tied(%array1)->push({$2=>$_});
next;
}
if (m/(.*) \[DEBUG\] \[([^][]+)\] RMSConnectionFactory - Returning WS /) {
if ("$2" ne "" ) {
delete $array1{$2};
}
next;
}

END {
print values %array1;
}

Input file:
======
2008-06-05 03:29:37,752 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:37,761 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:38,061 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:38,069 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:38,292 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:38,301 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:38,442 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:38,506 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:38,572 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:38,579 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:38,762 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:38,768 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:38,961 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:38,969 [DEBUG] [AQUEDUCT-WebContainer : 0] RMSConnectionFactory - Returning WS connection.

2008-06-05 03:29:39,292 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection.

2008-06-05 03:29:39,772 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:39,839 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:40,261 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:40,272 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.

2008-06-05 03:29:40,701 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.

2008-06-05 03:29:41,291 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:41,300 [DEBUG] [AQUEDUCT-WebContainer : 3] RMSConnectionFactory - Returning WS connection.
2008-06-05 03:29:41,542 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:41,551 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Returning WS connection.

Expected Output:
=============
2008-06-05 03:29:40,701 [DEBUG] [AQUEDUCT-WebContainer : 1] RMSConnectionFactory - Setting session state for connection.
2008-06-05 03:29:39,292 [DEBUG] [AQUEDUCT-WebContainer : 2] RMSConnectionFactory - Setting session state for connection


What i am getting right now:
=====================
Nothing :-( program runs successfully but with no output ..


Run command:
============
perl -w check_test_3_1.pl input.txt

Dependency Perl Modules:
===================
Data::StackedHash - Stack of PERL Hashes - search.cpan.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need optimized awk/perl/shell to give the statistics for the Large delimited file

I have a file size is around 24 G with 14 columns, delimiter with "|" My requirement- can anyone provide me the fastest and best to get the below results Number of records of the file First column and second Column- Unique counts Thanks for your time Karti ------ Post updated at... (3 Replies)
Discussion started by: kartikirans
3 Replies

2. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Shell Programming and Scripting

Whether Shell script can do this task ???

In following attached 748phy.xls file, fifth column is ST_Date, which contains time and dates in this format 22-11-2012 7:54:54 PM in single column I want it to split in this format either 1st column 22/11/2012 and in second column 7:54:54 PM Or like this in separate... (13 Replies)
Discussion started by: Akshay Hegde
13 Replies

5. Shell Programming and Scripting

Can any programmer do this task in shell script...

input file's one set header is this, ----------------------------------------------------- OUTPUT FROM ASCII FILE: CAST #1 ----------------------------------------------------- CC Cruise Latitude Longitude YYYY MM DD Time Cast #Levels CA 8504 50.083 -144.883 1970 1 2... (6 Replies)
Discussion started by: Akshay Hegde
6 Replies

6. Homework & Coursework Questions

Shell scripting task

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi all, I am new to shell scripting. And I have a task to do, I tried all possible ways to solve this, but... (8 Replies)
Discussion started by: Strongid
8 Replies

7. Shell Programming and Scripting

Shell scripting task

Hi all, I am new to shell scripting. And I have a task to do, I tried all possible ways to solve this, but didn't. Iwas wondering maybe someone could help me. So I have a random file something like this Andrew John Mike Alfa Omega Beta And I need to create scrip witch would filter any... (1 Reply)
Discussion started by: Strongid
1 Replies

8. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

9. Shell Programming and Scripting

comment and Uncomment single task out of multiple task

I have a file contains TASK gsnmpproxy { CommandLine = $SMCHOME/bin/gsnmpProxy.exe } TASK gsnmpdbgui { CommandLine = $SMCHOME/bin/gsnmpdbgui.exe I would like to comment and than uncomment specific task eg TASK gsnmpproxy Pls suggest how to do in shell script (9 Replies)
Discussion started by: madhusmita
9 Replies

10. UNIX for Dummies Questions & Answers

searchin optimized

hi friens, :) if ther are files named .c++,.C++,.cpp,.Cpp,.CPp,.cPP,.CpP,.cpP,.c,.C wat is the pattern for finding them :confused: (1 Reply)
Discussion started by: arunsubbhian
1 Replies
Login or Register to Ask a Question