LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using Array variables (https://www.linuxquestions.org/questions/linux-newbie-8/using-array-variables-877188/)

rwallingford 04-26-2011 10:59 AM

Using Array variables
 
I'm a relative new bash scripting guy, here's my dilemma.

I need to parse through a file which contains timestamps of transactions. What I am trying to do is come up with a Max Transactions Per Second (TPS) value. I was thinking that creating an array variable would be the way to go, but I'm having problems determining even how to start.

Any tips/tricks would be great.

Thanks in advance,
Rich

grail 04-26-2011 11:54 AM

Maybe if you show us some of the data we can assist? Remember to not only include the input file but also what your desired output would look like based on the data you provide.

rwallingford 04-26-2011 12:42 PM

more detail
 
I have one app that runs on 6 servers, with 5 instances on each server. I'm interested in parsing one log file which contains timestamp data. An excerpt of that file is included here. These files contain several thousand lines each. I am only interested in the timestamp column, and specifically just the hr/min/sec fields of that.

As far as output, I'd like to see the Max Transactions per Second - so, how many transactions occured and at what time. I'm very open on the look and feel.

.060, 2011/04/26, 10:20:09.223, /rws/services/Monitoring,
.012, 2011/04/26, 10:20:09.673, /rws/services/Monitoring,
.388, 2011/04/26, 10:20:10.753, /rws/services/Cardholder200702,
.012, 2011/04/26, 10:20:11.473, /rws/services/Monitoring,
.061, 2011/04/26, 10:20:11.742, /rws/services/Monitoring,
.218, 2011/04/26, 10:20:12.813, /rws/services/Cardholder2009,
.290, 2011/04/26, 10:20:16.700, /rws/services/Cardholder2009,
.060, 2011/04/26, 10:20:19.223, /rws/services/Monitoring,
.012, 2011/04/26, 10:20:19.703, /rws/services/Monitoring,
.012, 2011/04/26, 10:20:21.503, /rws/services/Monitoring,
.062, 2011/04/26, 10:20:21.702, /rws/services/Monitoring,
.215, 2011/04/26, 10:20:23.347, /rws/services/Cardholder2009,
.267, 2011/04/26, 10:20:23.373, /rws/services/Statements2010,
.061, 2011/04/26, 10:20:29.233, /rws/services/Monitoring,
.012, 2011/04/26, 10:20:29.693, /rws/services/Monitoring,

arizonagroovejet 04-26-2011 01:38 PM

This is rather quick and dirty, 'using array variables' doesn't come in to it, there might a a useless use of cat award in there, but it is bash scripting :)


Code:

$ cat file | cut -d , -f 3 | cut -d . -f 1 | sort | uniq -c | sort -n -r | head -1 | sed 's/^ *//' | cut -d ' ' -f 1
If you want to understand what it's doing, then run start off just running the command up to the second | and then add on the other commands one at a time. Look at the man pages for cut sort uniq and head to see what the options do.

With the data sample you provided the number that comes out is 2, which can easily verify is correct by looking at the data since there's not very much of it.

I expect someone can come up with something more elegant.

Arguably this would be better achieved with a scripting language like perl or python.

grail 04-26-2011 08:19 PM

Well there would need to be more work done to break down by change in minute and hour, but this may help:
Code:

awk -F"[\t ]*,[\t ]*" '{split($3,f,"[:.]");sec[f[3]]++}END{n = asort(sec);print sec[n]}' file
hours and minutes are f[1] and f[2] respectively.


All times are GMT -5. The time now is 03:10 PM.