Visit Our UNIX and Linux User Community


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
# 1  
Old 09-09-2009
Problem assigning a counter for particular pattern

Hi,
I have a script that compares two files(which are updated dynamically by a daemon) and evaluate results from the comparision.
For the first line of comparision from the file1, i will grep some part of the line in file with file1 and set a counter for that particular comparison. So for each and every line there should be a counter variable assigned and set.

So now the first run of script ends... and the next run starts.
For the next run, the script may find some more patterns from file and will grep for that pattern in file1 and set a counter for the new patterns...., above process continues.

Here, I have a problem storing a counter variable for a particular pattern. If script matches the particular pattern on its next run, it should increment the counter assigned for that pattern - else it should leave the counter for that pattern as it is.

This is what i have tried:

Code:
#!/bin/bash
file="/home/reddybs/test/failures.log"
file1="/home/reddybs/test/failures.log1"
file3="/home/reddybs/test/pc.chk"
ff=0
while read LINE
do
 
         i=`echo $LINE | awk '{print $4,$5}'`
#echo $i
 
         grep -w "$i" $file1 >>/dev/null
 
         if [ `echo $?` -eq 0 ]
         then
                echo "matched"
#Please guide me to assign a counter for this particular pattern               
         ff=`expr $ff + 1`
                echo "$ff ,Matched count for $i" >>$file3
         else
                echo "Yet to implement"
         fi
 
done < $file

Struk in assiging/set a counter variable for the particluar pattern/match/comparision/loop.

Thanks in advance for all your time.....

Cheers,
Sai
# 2  
Old 09-09-2009
what problem you are facing in you code?
# 3  
Old 09-09-2009
Quote:
Originally Posted by anchal_khare
what problem you are facing in you code?
Hi anchal: Thanks for the reply...

Problem in assiging a counter for particular match.

I would be happy if you can read my post again. The script posted in the post doesn't have any problems but it is not that intelligent to assign/store a counter for the particular macth.

Hope you are getting me!!!
# 4  
Old 09-09-2009
Basically, to "transfer" a value from one script (run) to another, you may want to store the value in question to a plain text file, e.g. by updating the "intro" of your script approximately as follows:

Code:
if [ -f value.data ]
then
  VALUE=$( more value.data ) && rm value.data
else
  VALUE=0 # for better understanding only ;-)
fi

# 5  
Old 09-10-2009
Hi dr.house....
Thanks a ton for your response...

Yes - your logic is fair enough to implement... I've tried some thing related to that.....! But this works only if script finds the same pattern in the next run...

Let me elaborate things...

Once the comparision is done, i store that in a file(file4 in script) and for the next time the script runs, If the pattern matches, i'll do a grep of the pattern and do a wc -l of the pattern on file4 and print that output to the file that i want.

So know the first part of IF works fine i.e assigning a counter. I will have to reset the counter - so what ever the entry that goes into file4 for the nth run, should be removed from the file4 if it n+1th run doesn't find it/them...

How can this be achieved????

Any different logic is also accepted with great pleasure and i'd give it a try definetly.

Script:
Code:
while read LINE
do
        i=`echo $LINE | awk '{print $4,$5}'`
        grep -w "$i" $file1 >>/dev/null
        if [ `echo $?` -eq 0 ]
        then
#               echo "matched"
                echo "$i" >>$file4  -----> stroe in some file
                ff=`grep "$i" "$file4" | wc -l` --------> get the count
#               echo "$ff ,Failure count for $i" >>$file3
                echo "FF $ff, $LINE" >>$file3
                echo "$LINE" >>$file1
        else
                echo "$LINE" >>$file3
                echo "$LINE" >>$file1
        fi
done < $tmpfile1

Cheers,
Sai

Last edited by reddybs; 09-10-2009 at 01:14 PM.. Reason: problem with explanation
# 6  
Old 09-10-2009
#create the following file before yor script runs
Code:
touch count_file

Type the following after the line "#Please guide me to assign a counter for this particular pattern "
Code:
var=`grep -w "$i" count_file`
if [ `echo $?` -eq 0 ]
then
grep -v "$i" count_file >> temp
mv temp count_file
new_count=`grep -w "$i" $file1 | wc -l | tr -s " " " " | cut -d " " -f3`
echo $i" "$new_count >>count_file
fi


Last edited by girish1428; 09-10-2009 at 01:28 PM..
# 7  
Old 09-10-2009
Maybe you should post a sample data for each file and required output.
echo , awk and grep don't look nice, I think that awk can do all in one shot.

Previous Thread | Next Thread
Test Your Knowledge in Computers #749
Difficulty: Medium
The ZX81 had a base configuration of 1 KB of on-board memory that could officially be expanded externally to 64 KB.
True or False?

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

Featured Tech Videos