Ksh Searching for a string within a file and keeping a count= get the following Sample01=: command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ksh Searching for a string within a file and keeping a count= get the following Sample01=: command
# 1  
Old 07-02-2012
Ksh Searching for a string within a file and keeping a count= get the following Sample01=: command

I have a script that goes through a 24 hr logfile, And i want to count the instances of a Test01 to 83 and output the sum of all the instances over 24hrs



Code:
 #/bin/ksh
  
 cat $parse_data | awk '/'$time$i'/ {for(x=0; x<=16; x++) {getline; print}print "--" }' > _hr.txt

                for y in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 81 82 83 84 85
                do



                tot_alarms=`cat _hr.txt | grep -c "Network=Test$y"`
                


                Sample$y= `expr $Sample$y + $tot_alarms `
                echo " The no of alarms for this is $Sample$y"




              
                done

              
done






The output is as follows:

Code:
./Test.sh: line 37: Sample01=: command not found
 The no of alarms for this is 01



What is the issue with using the following in the code:

Code:
Sample$y= `expr $Sample$y + $tot_alarms `


Last edited by Scrutinizer; 07-02-2012 at 05:01 AM.. Reason: code tags
# 2  
Old 07-02-2012
For one, there should be no spaces around the assignment operator.

Not related to this, there is a space before the shebang on line 1 that should not be there.
# 3  
Old 07-02-2012
Thanks i changed around the script, I am still puzzeled

Code:
echo $Sample$y+$tot_alarms | bc > Sample$y
                echo $Sample$y

the debug gives the following


i need the foillowing Sample01=36774, Sample02=3333 .....

however when i try to add or assign contents to these variables it seems to use the index of the SampleXX



Code:
+ bc
+ echo 01+3674
+ 1> Sample01
+ echo 01
01

Code:
#!/bin/ksh
set -x

 
 cat $parse_data | awk '/'$time$i'/ {for(x=0; x<=16; x++) {getline; print}print "--" }' > _hr.txt
                for y in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 81 82 83 84 85
                do
 
                tot_alarms=`cat _hr.txt | grep -c "Sample=$y"`
                echo "Totals no of alarms for Sample=$y for $time$i is =====> $tot_alarms"
                echo $Sample$y+$tot_alarms | bc > Sample$y
                echo $Sample$y
 
 
                #totalAlarms=`expr $tot_alarms + $totalAlarms `
                done
                echo "Total no of alarms for this hour is : $totalAlarms"
done

Code:
+ + cat _hr.txt
tot_alarms=3674
+ echo Totals no of alarms for Sample=01 for 02-07-12:00 is =====> 3674

+ bc
+ echo 01+3674
+ 1> Sample01
+ echo 01
01

Moderator's Comments:
Mod Comment Please view this link: How to use [code]...[/code] tags

Last edited by k00061804; 07-02-2012 at 06:01 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Searching for file types by count in specific folder in RHEL 6

So I'm trying to search for the top 10 or 15 items under a directory by file type. I want to run a command on a directory and get something like the following: Example of expected output.. .PDF: 100, .txt: 95, .word: 80.. What would be the best way of going about this? I've searched around... (2 Replies)
Discussion started by: shackle101
2 Replies

2. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

3. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

4. Shell Programming and Scripting

Searching for a specific string in a file

Hi I am trying to search for a certain set of patterns within a file, and then perform other commands based on output. testfile contents: password requisite pam_cracklib.so lcredit=-1 ucredit=-1 ocredit=-1 script: D="dcredit=-1" if then echo $D exists else echo $D doesnt... (8 Replies)
Discussion started by: bludhemn
8 Replies

5. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

6. Shell Programming and Scripting

Mail cleanup from ksh script, keeping 50 most recent msgs

I found some posts describing how to completely clean out a mailbox in Unix/Linux. But I want to keep the 50 most recent messages. Any ideas out there? Thanks! (3 Replies)
Discussion started by: OPTIMUS_prime
3 Replies

7. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

8. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. UNIX for Dummies Questions & Answers

searching for a string though file system

Is there a way to search an entire file system for the occurance of a string..... other than grep. I have a large directory structure and I'm not certain that grep <string> */*/*/*... is all that effective - especially as I can't be sure of the number of levels to go down without heaps of... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question