![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Record count based on a keyword in the records | aemunathan | Shell Programming and Scripting | 4 | 03-03-2009 08:39 AM |
| validating a file based on conditions | trichyselva | Shell Programming and Scripting | 8 | 01-02-2009 08:51 AM |
| Based on num of records in file1 need to check records in file2 to set some condns | mavesum | Shell Programming and Scripting | 3 | 11-26-2008 10:48 AM |
| extract lines based on few conditions | prvnrk | Shell Programming and Scripting | 4 | 10-17-2008 05:24 AM |
| Count No of Records in File without counting Header and Trailer Records | guiguy | Shell Programming and Scripting | 2 | 06-07-2007 12:15 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
using awk to count no of records based on conditions
Hi
I am having files with date and time stamp as the folder names like 200906051400,200906051500,200906051600 .....hence everyday 24 files will be generated i need to do certain things on this 24 files daily file contains the data like Code:
200906050016370 0 1244141195225298lessrv3 BSNLSERVICE1 BSNLSERVICE1 2128 LOCATIONMANAGER SLIR 919443200299 MSISDN ASC 919443200299 0 SUCCESS 1244141195225298less 919443200299 124414 79.301938811.6885305NORMAL DELAY_ WGS84 200906050016440 0 1244141197503299lessrv3 BSNLSERVICE1 BSNLSERVICE1 2139 LOCATIONMANAGER SLIR 919449838266 MSISDN ASC 919449838266 0 SUCCESS 1244141197503299less 919449838266 124414 74.739722013.3302837NORMAL DELAY_ WGS84 200906050017070 0 1244141224604306lessrv3 BSNLSERVICE1 BSNLSERVICE1 2128 LOCATIONMANAGER SLIR 919448010097 MSISDN ASC 919448010097 1 SYSTEM FAILURE 1244141224604306less 919448010097 124414 NORMAL DELAY_ WGS84 200906050017110 0 1244141227460308lessrv3 BSNLSERVICE1 BSNLSERVICE1 2128 LOCATIONMANAGER SLIR 919449838266 MSISDN ASC 919448010098 1 SYSTEM FAILURE 1244141227460308less 919449838266 124414 NORMAL DELAY_ WGS84 20090605140148 1204702370366140lessrv3 RTMS 0 TRACKING tlrep 0 SUCCESS 1 WGS84 200906051402100 0 1195202147789210lessrv3 RTMS RTMS LOCATIONMANAGER SLIR 919446001620 MSISDN ASC 919446001620 526 INACTIVE SUBSCRIBER 1195202147789210less 124419 NORMAL DELAY_ WGS84 200906051402100 0 1195202147789210lessrv3 RTMS RTMS LOCATIONMANAGER SLIR 919446001618 MSISDN ASC 919446001618 526 INACTIVE SUBSCRIBER 1195202147789210less 124419 NORMAL DELAY_ WGS84 200906051402100 0 1195202147789210lessrv3 RTMS RTMS LOCATIONMANAGER SLIR 919446001617 MSISDN ASC 919446001617 526 INACTIVE SUBSCRIBER 1195202147789210less 124419 NORMAL DELAY_ WGS84 i need the scripts to do the following 1. It has to filter the records based on $4 and $6 (i.e $4==BSNLSERVICE1 AND $6==2128 ) and count the total records for the DAY (20090605*) OUTPUT REQUIRED: BSNLSERVICE1 2128 ==3 2. It has to filter the records based on $4 and $6 (i.e $4==BSNLSERVICE1 AND $6==2128) and count the total records for the DAY (20090605*) AND GROUP BASED ON $14 (i.e SUCCESS, FAILURE) OUTPUT REQUIRED: BSNLSERVICE1 2128 SUCCESS == 1 BSNLSERVICE1 2128 SYSTEM FAILURE ==2 3. It has to filter the records based on $4 and $6 (i.e $4==BSNLSERVICE1 AND $6==2128), GROUP BASED ON $9 (i.e 919448010098, 919446001618 ) and count the total records for the DAY (20090605*) FOR EACH DISTINCT $9 OUTPUT REQUIRED: 919449838266 2 919448010097 1 output should be of $4==BSNLSERVICE1 AND $6==2128 only ..other things ($4==RTMS) are not required. Help me out pls Last edited by aemunathan; 06-05-2009 at 08:33 AM.. |
|
||||
|
Try this:
Code:
awk -v day="20090605" -v serv="BSNLSERVICE1" -v val="2128" '
$1 ~ day && $4==serv && $6==val {
s1++;a[$14]++;b[$9]++
}
END{
print serv,val, "=" s1 "\n"
print serv,val, a["SUCCESS"]
print serv,val, a["SYSTEM"] "\n"
for(i in b){print i, b[i]}
}' file
|
|
||||
|
Hi panyam and Franklin
i followed the method suggested by panyam its giving useful result. here it goes as in the order i requested. Code:
1.
awk '$4=="BSNLSERVICE1"&&$6=="2128" { count++ } END { print "BSNLSERVICE1-->2128-->" count }' 20090604*
2.
awk '$4=="BSNLSERVICE1"&&$6=="2128"{ b[$14]++} END {for(i in b){print i, b[i]} }' 20090604*
3.
awk '$4=="BSNLSERVICE1"&&$6=="2128"{ b[$9]++} END {for(i in b){print i, b[i]} }' 20090604*
actually i need to schedule it every night at 2:00 am and i need to derive the filename from the date command lets take the example tonight 2.00 am the ouput of the Code:
date +'%Y%m%d' i need to give the file name as 20090605* in the filename part of the awk ... and for franklin ....i used this way Code:
#!/usr/xpg4/bin/awk
awk -v day="20090605" -v serv="BSNLSERVICE1" -v val="2128" '
$1 ~ day && $4==serv && $6==val {
s1++;a[$14]++;b[$9]++
}
END{
print serv,val, "=" s1 "\n"
print serv,val, a["SUCCESS"]
print serv,val, a["SYSTEM"] "\n"
for(i in b){print i, b[i]}
}' 200906051859
Quote:
|
|
||||
|
To get the date of yesterday you can use the datecalc script of Perderabo.
Place this script in the same directory of your script with the name datecalc and make it executable: days elapsed between 2 dates Your script should looks like: Code:
#!/bin/ksh
dat=$(./datecalc -a $(date +"%Y %m %d") - 1)
day=$(/usr/xpg4/bin/awk -v d="$dat" 'BEGIN {split(d,a," ");day=sprintf("%s%02s%02s",a[1],a[2],a[3]);print day}')
/usr/xpg4/bin/awk -v d="$day" -v serv="BSNLSERVICE1" -v val="2128" '
$1 ~ day && $4==serv && $6==val {
s1++;a[$14]++;b[$9]++
}
END{
print "Filename: " FILENAME "\n"
print serv,val, "=" s1 "\n"
print serv,val, a["SUCCESS"]
print serv,val, a["SYSTEM"] "\n"
for(i in b){print i, b[i]}
}' $day*
|
| Bits Awarded / Charged to Franklin52 for this Post | |||
| Date | User | Comment | Amount |
| 06-06-2009 | aemunathan | Excellent work done | 50,000 |
|
||||
|
Hi
Thanks a lot man...its nice to see the result... I need one more info. actually am using sqlloader to insert the result in to a table. Here i want to print the previous date as well in dd-mon-yyyy format i tried in this way Code:
#!/bin/ksh
dat=$(./datecalc -a $(date +"%Y %m %d") - 1)
da_te=$(date +'%d')
da=$(($da_te-1))
mon=$(date +'%b')
year=$(date +'%Y')
host=$(hostname)
day=$(/usr/xpg4/bin/awk -v d="$dat" 'BEGIN {split(d,a," ");day=sprintf("%s%02s%02s",a[1],a[2],a[3]);print day}')
/usr/xpg4/bin/awk -v d="$day" -v serv="BSNLSERVICE1" -v val="2128" -v daet="$(($da)-($mon)-($year))" -v ho="$host"'
$1~day && $4==serv && $6==val {
s1++
}
END{
print daet, host,s1
}' $day*
Thanks in advance Last edited by aemunathan; 06-07-2009 at 01:36 PM.. Reason: one more requirement!!!!!! |
|
||||
|
Hi
Please guide me for getting the previous date in the format dd-mon-yyyy (06-Jun-2009) to load in to the database table Thanks |
| Sponsored Links | ||
|
|