Sorting Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting Question
# 1  
Old 02-13-2008
Sorting Question

Hi All
I have a CSV file where 3rd and 4th fields are date and time
it's a session log file printing the date and time per session
Now, I want to create a statistics of how many sessions per date and Time
I managed a script for sessions per hour

Code:

#!/bin/bash

#This Shell Script recieves as input a csv file, removes the first line of the file and calculates the number of sessions instances per hour
#Enjoy :-)


#deleting the statistics file and file with no first line from the system

rm -rf /tmp/scripts/stat_file
rm -rf /tmp/scripts/New_File_Name

#Inputing the File name to be analyzed

read -p "Enter The File Name to Analyze -> " File_Name

#Removing the first line which contains the field names

tail +2 $File_Name > /tmp/scripts/New_File_Name

#printing the time field from the csv file, printing just the hour, counting instances of each hour and redirecting to a statistics file

awk -F ',' '{ print $4 }' /tmp/scripts/New_File_Name | awk -F ':' '{ print $1 }' | uniq -c >> /tmp/scripts/stat_file

#printing the first and second field from the output file with explanations.

awk '{ print "there were "$1" sessions at "$2" AM" }' /tmp/scripts/stat_file


now, I want to add the dates also. date is field $3 in the original file and is in dd-mm-yy format.

I'm stuck :-(
Any ideas?

thanks in advance
The owl
# 2  
Old 02-13-2008
How do you want to "add" the dates? Do you want to count how many times each day appears?

Code:
awk -F, '{ date_count[$4]++; } END { for (d in date_count) { print d,"-",date_count[d]; }}'

After this, you can sort it however you want. You should first, however, convert the months:

Code:
awk 'sub("Jan",1) || sub("Feb",2) || sub("Mar",3)  (... etc)' | \
sort -t - -k 4n -k 3n -k 2n

# 3  
Old 02-14-2008
Thank you, but not quite what I need :-(

Hi
This is command I'm using

awk -F ',' '{ print $3, $4 }' /tmp/scripts/New_File_Name | awk '{ print $1, $2 }'

This is the output

27-01-08 03:44:41.018
27-01-08 03:47:19.711
27-01-08 03:49:22.438
27-01-08 03:54:07.578
27-01-08 03:56:05.518
27-01-08 04:06:24.786
27-01-08 04:08:17.956
27-01-08 04:08:26.964
27-01-08 04:09:33.353
27-01-08 04:11:28.766
27-01-08 04:11:35.338
27-01-08 04:11:41.080
27-01-08 04:12:47.780
27-01-08 04:12:55.726
27-01-08 04:13:02.687
27-01-08 04:13:08.742
27-01-08 04:13:17.915
27-01-08 04:22:59.725
27-01-08 04:24:14.726
27-01-08 04:26:21.373
27-01-08 04:27:37.617
27-01-08 04:29:49.979
27-01-08 04:31:28.018
27-01-08 04:32:00.765
27-01-08 04:38:26.139
27-01-08 04:40:18.062
27-01-08 04:41:35.777
27-01-08 04:42:39.187
27-01-08 04:44:52.026
27-01-08 04:51:33.561
27-01-08 04:53:49.810
27-01-08 05:01:08.307
27-01-08 05:02:02.499
27-01-08 05:03:03.190
27-01-08 05:03:50.279
27-01-08 05:04:53.827
27-01-08 05:05:48.014
27-01-08 05:07:38.179
27-01-08 05:08:26.018
27-01-08 05:09:57.883
27-01-08 05:11:32.100
27-01-08 05:54:34.025
27-01-08 05:55:39.882
27-01-08 05:56:44.151
27-01-08 05:58:53.207
27-01-08 06:01:05.527
27-01-08 06:02:35.431
27-01-08 07:10:04.648
28-01-08 07:11:04.648
28-01-08 07:12:04.648

What I want to do is create a responce which sais:
on the 27-01-08 the following lines appeard:
5 lines at 03:00
26 lines at 04:00

etc...

how do I do that?

thanks again
The Owl
# 4  
Old 02-14-2008
Create an array element for each date and hour. So for simplicity, filter this output to awk again:
Code:
awk -F"[: ]" '{ ts[$1 "." $2]++;}  END { for (key in ts) { print ts[key] " lines at " key ":00"; }'

Do you see it now? Array indexes in awk are just strings (called associative arrays or hashes). So you create the index based on the first field (the date) and the second field (the hour). If the array index already exists, the code increments what's already there. Otherwise, it just becomes 1. You can also do two-dimension arrays in awk. Check the man pages for how to do that, though.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question about sorting -- how to pass an array to a function

Hi, guys I just wanted to sort the elements of an array ascendingly. I know the following code does work well: array=(13 435 8 23 100) for i in {0..4} do j=$((i+1)) while ] do if } -le ${array} ]] then : else min=${array} ${array}=${array} ${array}=$min fi... (5 Replies)
Discussion started by: franksunnn
5 Replies

2. UNIX for Dummies Questions & Answers

Quick Question: Sorting

Hi fellow linuxers I have a quick question... I would like to sort the numbers in each line that are within a file, and I want to sort them so that the numbers in each line go from SMALLEST to HIGHEST. For example, my file looks like this: 6 4 2 3 1 5 7 8 15 16 11 13 12 14 20 18 19 17 24 26... (7 Replies)
Discussion started by: lucshi09
7 Replies

3. Shell Programming and Scripting

sorting

Hi all, Does anyone can help me the following question? I would like to write an AWK script. In the following input file, each number in "start" is paired with numbers in column "end". No Start End A 22,222,33,22,1233,3232,44 555,333,222,55,1235,3235,66... (7 Replies)
Discussion started by: phoeberunner
7 Replies

4. Shell Programming and Scripting

Sorting HELP

Hi, I have posted related topic but as i continue the research I find more need to sort the data. AS(2607:f278:4101:11:dead:beef:f00f:f), AS786 AS6453 AS7575 AS7922 AS(2607:f2e0:f:1db::16), AS786 AS3257 AS36252 AS786 AS3257 AS36252 AS(2607:f2f8:1700::2), AS786 AS6939 AS25795 ... (6 Replies)
Discussion started by: sam127
6 Replies

5. UNIX for Advanced & Expert Users

HELP on sorting

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this; 192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible... (1 Reply)
Discussion started by: sam127
1 Replies

6. UNIX for Dummies Questions & Answers

Sorting help

i have list of files: Wang De Wong CVPR 09.pdf Yaacob AFGR 99 Second edition.pdf Shimon CVPR 01.pdf Den CCC 97 long one.pdf Ronald De Bour CSPP 04.pdf ..... how can i sort this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its... (1 Reply)
Discussion started by: nirnir26
1 Replies

7. UNIX for Dummies Questions & Answers

Sorting question

i have list of files: Wang CVPR 09.pdf Yaacob AFGR 99.pdf Shi CVPR 04.pdf ..... how can i sort with single line this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its sort by the year) example: 1999 CVIU ... (2 Replies)
Discussion started by: nirnir26
2 Replies

8. Shell Programming and Scripting

sorting question......

HI, I have been racking my brain for a while on this..... was hoping one of the guru's could point me in the right direction.... Basically I have the following data in a file : ---- 2009-01-01 10665 Begin 02:25:23 2009-01-02 10665 Begin 20:54:11 2009-01-03 10665 Begin 05:31:17... (4 Replies)
Discussion started by: mashy
4 Replies

9. UNIX for Dummies Questions & Answers

sorting question

How can I sort lines in a file in a consistent, user-defined way that is not alphabetical, numeric, or patterned in any other way (e.g., sort a bunch of 5 line files to always print lines 2,5,3,4,1)? Thanks in advance. (4 Replies)
Discussion started by: darwin_886
4 Replies

10. UNIX for Dummies Questions & Answers

Noob sorting question

Ok here is the deal, I have a command given to me by some systems guy who I cannot get ahold of on the weekend without paying him alot of money to help me. I need to get this done before Monday as I am just getting pummeled by DOS attacks. The comand given was.... netstat -ntu | awk '{print... (1 Reply)
Discussion started by: Hexabah
1 Replies
Login or Register to Ask a Question