Problem assigning a counter for particular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem assigning a counter for particular pattern
# 8  
Old 09-10-2009
@girish: Thanks for the reply. I am trying to understand your logic and get back to you once done.

@danmero: I really dont know how to put forward this in posts and as i felt this is bit tedious to explain through posts, i am pasting what actually i am trying to do.
This is what i am trying to do...
I have a file(mainfile) with the below format:
Code:
Date:Warning: client clientname1, grp grpouname1, sometext
Date:Warning: client clientname2, grp grpouname2, sometext
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname4, db dbname4,(sometext)
.
.
.
.

I am writing a script which will run for every 5 min read the content of main file and update a file(pcfile) to contain the client name (entire name as in mainfile ) along with the count of how many times the line has been repeated.
i have divided the script into following stages
1. Check the mainfile every time the script runs and store only the updated lines in that. Say this file as tmpfile.
2.I will compare each and every line in the tmpfile with the file called failedfile. Comparison will be done with the below format:
clientname1, grp grpouname1
clientname2, db dbname2

about failedfile:
----------------
failed file contains the filtered entries of mainfile. For the first run of script it will be empty.
Comparision starts:
3.
a. If the first comparision(line1) is positive, i will assign a counter,increment it and send the line with counter to pcfile. Keep in mind that each and every line is a different comparision and requires a seperate counter. Now append the line to pcfile and failedfile. For the next time the script runs, if the script doesn't find this line in tmpfile, the counter should be reset.
b. else, append the line to pcfile and failedfile.
4. Now remove the tmpfile.
----Script ends-----


Right now i am struggling with 3rd point.

Cheers,
Sai
# 9  
Old 09-10-2009
Quote:
Originally Posted by reddybs
every line is a different comparision and requires a seperate counter
Time to replace the beforementioned, single value by an array of values, then ... Smilie
# 10  
Old 09-11-2009
Quote:
Originally Posted by dr.house
Time to replace the beforementioned, single value by an array of values, then ... Smilie

Hi dr.house....!

Me just in!!!! Thanks for the reply.

Can you guide me how to start with!!! Mean while i will be going through 'arrays' in shell scripting - which is a bit new to me.

Many thanks,
Sai
# 11  
Old 10-13-2009
I am also having troubles to get what you want but could something like this be a start?

Here comes the example file:
Code:
Date:Warning: client clientname1, grp groupname1, sometext
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname4, db dbname4,(sometext)
Date:Warning: client clientname1, grp groupname1, sometext
Date:Warning: client clientname4, db dbname4,(sometext)
Date:Warning: client clientname4, db dbname4,(sometext)
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname1, grp groupname1, sometext
Date:Warning: client clientname1, grp groupname1, sometext
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname1, grp groupname1, sometext
Date:Warning: client clientname4, db dbname4,(sometext)
Date:Warning: client clientname1, grp groupname1, sometext
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname4, db dbname4,(sometext)
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname3, db dbname3,(sometext)
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname2, grp groupname2, sometext
Date:Warning: client clientname3, db dbname3,(sometext)

Counting the occurence per client:
Code:
awk -F" |," '{_[$3]+=1} END{for(a in _){print a,_[a]}}' infile
clientname2 7
clientname3 8
clientname4 5
clientname1 6

Now you would like to compare this with a file you've written 5 mins ago?
# 12  
Old 10-13-2009
Hello Zaxxon,

Many thanks for the reply.

Yes, i will now need to compare this with the updated contents of the file and check if the pattern has occurred again or not and accordingly i should increment/decrement the counter.

I will try again and explain things in a better way so that it can be understood properly - in the next post ASAP.

However, many thanks for your time.

Cheers,
Sai
# 13  
Old 10-15-2009
The command that Zaxxon suggested worked out. I have modified some parts of my script and embedded this to get the problem resolved.

@Zaxxon: Many thanks for your reply.

@Others: My heart full thanks for all those whose supported me regarding this. I don't remember all the names, but i hope my thanks should reach you with this FINAL POST.

and Finally, to UNIX.COM for this excellent forum. Really - I love this.

Regards,
Sai
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Assigning matched pattern within filename to variable

I am writing a bash script where I use two types of files that both contain a numerical pattern of the type 123.4567 (always groups of three and four digits separated by period) within their filenames. I need to assign the numerical patterns of these filenames to variables (inside a for loop),... (6 Replies)
Discussion started by: Mauve
6 Replies

2. Shell Programming and Scripting

problem with counter

i having a file xxxxxxxxxxxxxxx1234 ...........value can be change xxxxxxxxxxxxxxx1235 xxxxxxxxxxxxxxxx1236 . . . . xxxxxxxxxxxxxxxxx1300 ...........value can be change i want to cut last four characters of first line and last line and find the missing pattern. output should... (4 Replies)
Discussion started by: sagar_1986
4 Replies

3. Shell Programming and Scripting

[Solved] problem assigning value

Hi, This is the script that am trying to execute. a= sar 1 5 | grep ^A | awk '{print $5}' echo $a i am getting output. 99 i get a blank space for echo $a. Why is the value not getting assigned to a?? Thanks in Advance. How to use code tags (6 Replies)
Discussion started by: aksijain
6 Replies

4. Shell Programming and Scripting

AWK counter problem

Hi I have a file like below ############################################ # ParentFolder Flag SubFolders Colateral 1 Source1/Checksum CVA 1 Source1/Checksum Flexing 1 VaR/Checksum Flexing 1 SVaR/Checksum FX 1 ... (5 Replies)
Discussion started by: manas_ranjan
5 Replies

5. Shell Programming and Scripting

problem in assigning variable

suppose in my script i have written a1=2 a2=4 read option # I directly want to see the value of a1 or a2 (i:e; 1 or2 )depending upon i/p given like a1 or a2 to option var.so what should i give .Suppose if I give a1 to option then how can I see the value. echo $$option --- doesn't work pls... (3 Replies)
Discussion started by: maitree
3 Replies

6. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

7. Shell Programming and Scripting

assigning counter to same keys in a file

Hi, I've a data file with similar keys coming in. I want to assign an incremental counter to those records and attach to a file for example File 10001 ABCD 10002 PQRS 10001 ABCD 10003 QWER 10001 ABCD 10002 PQRS 10004 POIU output as 10001 ABCD 1 10002 PQRS 1 10001 ABCD 2 10003... (3 Replies)
Discussion started by: rudoraj
3 Replies

8. Shell Programming and Scripting

problem assigning values to variable

Date of Request: 20080514 10:37 Submitted By: JPCHIANG i want to get the value "JPCHIANG" only in read a file, however, when i do this: name=`"$line"|cut -d " " -f8` it display all the line and append 'not found' at the end of the statement the $line is actually a variable in a... (2 Replies)
Discussion started by: finalight
2 Replies

9. Shell Programming and Scripting

counter problem

Hi, I'm attempting to take the following input list and create an output file as shown below. I've monkeyed around for long enough. Can anyone help? NOTE: fs*** will be header and I want to get a count on NY**. fs200a NY7A fs200b NY7B NY7B NY7B fs200c NY7C NY7C NY7C NY7C... (2 Replies)
Discussion started by: jwholey
2 Replies

10. UNIX for Dummies Questions & Answers

counter / increment problem within echo stmt

Simple script trying to increment a counter within an echo statement never gets past 1 - PLEASE HELP! Thanks. ~~~~~~~~~~~ #!/bin/sh stepup() { STEP=`expr $STEP + 1` echo $STEP } # # Initialize variables # STEP=0 echo "Counter Value: `stepup`" echo "Counter Value:... (2 Replies)
Discussion started by: blaze
2 Replies
Login or Register to Ask a Question