Find the occurence of particular string in log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the occurence of particular string in log file
# 1  
Old 12-17-2012
Find the occurence of particular string in log file

I have a log file which looks like this:

Code:
[2012-12-04 13:54:15,744] <845185415165:STATUS:5/0:0:0:0:0|ghy59DI5zasldf87asdfamas8df9asd903tGUVSQx4GJVSQ==>

I have to extract DATE and number of times the keyword STATUS is shown on each date.

Input is :
Code:
[2012-12-04 13:54:15,744] <1354625655744:STATUS:5/0:0:0:0:0|ghy59DI5ztGUVSQx4GJVSQ==>

Output: in excel file, Column1(date) Column2(Number of occurences)
My excel file should also contain all the dates, if on any particular day, STATUS keyword is not found, it will show count as 0

Please help me ASAP. I am in very urgent need of this shell script.

Last edited by Scrutinizer; 12-17-2012 at 05:32 AM.. Reason: code tags
# 2  
Old 12-17-2012
Code:
perl -e 'while(<>){ 
  $counts{$1} = 0 if (/\[(\d{4}-\d{2}-\d{2})/ ) && (! defined $counts{$1})
  $counts{$1}++ if /\[(\d{4}-\d{2}-\d{2}).+STATUS/;
}
for $date (sort keys %counts){
  print "$date , $counts{$date}\n";
} '  filename.log >status_count.csv

# 3  
Old 12-17-2012
thanks for the very quick reply..

But, can you please help me in writing in normal bash script for the same
# 4  
Old 12-17-2012
Why do you need bash specifically perl can be called within a bash script, it's just another utility, a very powerful one that you can use to write complex programs in, but also a command line utility.
# 5  
Old 12-17-2012
skrynesaver...


how do i call a bash script from the perl??
# 6  
Old 12-17-2012
Perl allows you to call a shell command in three ways, exec, system and with backticks or using the qx(), quote executable, operator
Code:
exec ("$ENV{HOME}/bin/my_script.sh --f - l -a - g -s");
system ("$ENV{HOME}/bin/my_script.sh -f - l -a - g -s");
$result = qx($ENV{HOME}/bin/my_script.sh -f - l -a - g -s);

exec hands control over to the called operation and the entire process dies with the called script, whereas system forks a new process and waits for it to complete. The qx operator is like system except it returns the output of the command which allows us to assign it.

Last edited by Skrynesaver; 12-18-2012 at 04:48 AM.. Reason: I to mention forgot backticks AKA qx()
This User Gave Thanks to Skrynesaver For This Post:
# 7  
Old 12-17-2012
Actually, I have no idea how to write the script. This is actually my first time.
I wrote the code like this:
Code:
#!/usr/local/bin/perl

MAIN:
{

        while(<>)
        {
                $counts{$1} = 0 if (/\[(\d{4}-\d{2}-\d{2})/ ) && (! defined $cou
nts{$1})
                $counts{$1}++ if /\[(\d{4}-\d{2}-\d{2}).+STATUS/;
        }

        for $date (sort keys %counts)
        {
                print "$date , $counts{$date}\n";
        }
}

On running the code, I am getting following error:
Code:
bash-3.00$ perl test.pl license.log > status.log
Scalar found where operator expected at test.pl line 9, near ")
                $counts"
        (Missing operator before $counts?)
syntax error at test.pl line 9, near ")
                $counts"
syntax error at test.pl line 9, near "++ if"
syntax error at test.pl line 16, near "}"
Execution of test.pl aborted due to compilation errors.

Can you help me in this?

Last edited by Franklin52; 12-17-2012 at 10:46 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

2. Shell Programming and Scripting

Get nth occurence of string from a file

I have file in which the data looks like this, 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ 03,test1,41203016,,/ 01,0000000,xxxxxxx/ 02,xxxxxxxx,yyyyyy/ ... (16 Replies)
Discussion started by: r@v!7*7@
16 Replies

3. Shell Programming and Scripting

Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with. VLCSTART='<vlc:id>' VLCV=0 VLCEND='</vlc:id>' echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCENDOutput file contains several occurences... (10 Replies)
Discussion started by: TiedCone
10 Replies

4. Shell Programming and Scripting

How to find the number of occurence of particular word from a text file?

example: i have the following text file... i am very tired. i am busy i am hungry i have to find the number of occurence of a particular word 'am' from the text file.. can any one give the shell script for it (34 Replies)
Discussion started by: sheela
34 Replies

5. Shell Programming and Scripting

Split a fixed length file bases on last occurence of string

Hi, I need to split a file based on last occurece of a string. PFB the explanation I have a file in following format aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccc ddddddddddddddddddddddddddd 3186rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr... (4 Replies)
Discussion started by: Neelkanth
4 Replies

6. UNIX for Dummies Questions & Answers

To find the Nth Occurence of Search String

Hi guys, I like to find the Line number of Nth Occurence of a Search string in a file. If possible, if it will land the cursor to that particualar line will be great. Cheers!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

shell script to find the second occurence of the alphabet in a string

this is my assignment question. i'm supposed to submit it tommorow. can somebody please help me with it? Do not post homework questions in the main forums. Please post in the homework forum using the correct template. (0 Replies)
Discussion started by: vijjy
0 Replies

8. Shell Programming and Scripting

Find index of last occurence of a character within a string

I need to find the index of last '|' (highlighted in bold) in awk : |ifOraDatabase.Lastservererr<>0then|iferr_flag<>0then|end if Please suggest a way... Thanks (5 Replies)
Discussion started by: joyan321
5 Replies

9. Shell Programming and Scripting

How to find vowel's occurence in a string

Hi All, I want to search the string for vowel's occurence and find the no of occurence of each vowels, Could anyone help me out? This is urgent to me...I m new to Shell programming.. Thanks and Regards, Nids:b: (4 Replies)
Discussion started by: Nidhi2177
4 Replies

10. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 Replies
Login or Register to Ask a Question