Dears,
I have a directory that contains many files,name of the file has a speciefied format like:
B<date>-time
B20080203-1510,B20080203-1520,.....etc
these files contains many counters in the following format:
<conter name>
<counter value>
I need to filter these files based on thier names (like filter all files with speciefied date) and then search them for a specified counter name and value ,the expected result will be :
countername=<value>
example:
/directory
B20080203-1510
B20080203-1520
B20080202-1110
B20080201-1201
filter1:
B20080203-1510
B20080203-1520
let's say B20080203-1510 contains:
counter1
<10>
counter2
<11>
counter3
<20>
also file B20080203-1520
counter1
<100>
counter2
<40>
the result will be as following:
counter1=10+100=111
counter2=11+40=51
how could I a cheive this using unix commands,,,????
Thanks in advance...,,,
mm00123:
counter1
<10>
counter2
<11>
counter3
<20>
also file B20080203-1520
counter1
<100>
counter2
<40>
the result will be as following:
counter1=10+100=111
counter2=11+40=51
Assuming counter1 calculation is a typo here...
To filter the files you can use wildcards on the commandline.
eg to get all files for a given date:
B20080203-????
How to find a specified counter and get its value and then sum all these values to acheive total counter value???
file1:
-----
counter1
<value>
counter2
<value>
file2:
-----
counter2
<value>
counter3
<value>
counter1
<value>
counter1=value from first file+ vlaue from second one
if perl is an option:
sum_counts.pl
#!/usr/bin/perl
use strict;
use warnings;
@ARGV = <path/to/B2008*-*>;
my %counts = ();
while (<>) {
chomp;
chomp(my $count = <>);
$count =~ tr/<>//d;
$counts{$_}+=$count;
}
print "$_=$counts{$_}$/" for (sort keys %counts);
usage:
perl path/to/sum_counts.pl > path/to/outfile.txt
V3l0
February 4, 2008, 5:38am
5
Well, in pure shell.
#!/bin/ksh
ifile=0
# Pass 1: creating working files
for file in $*
do
let ifile=$ifile+1
grep -v "<" $file > $file.$$-1
grep "<" $file | sed -e 's/<//g' | sed -e 's/>//g' > $file.$$-2
pr -m -t -s= $file.$$-1 $file.$$-2 > $file.work.$$
rm -f $file.$$*
done
# Pass 2: Creating 1st part of result file with counters names
cat *.$$ | cut -d= -f1 | sort | uniq > result$$.txt
# Pass 3: Getting values in $$ing files
cnt_list=$(cat result$$.txt)
for cnt in $cnt_list
do
list=$(grep $cnt *.$$ | cut -d= -f2)
list=$(echo $list | sed -e 's/ /+/g')
val=$(echo $list | bc)
echo $cnt=$list=$val
done
rm -f *.$$ result$$.txt
Hope it'll be good for you, ouput of the script :
./script B20080203-15*
counter1=10+100=110
counter2=11+40=51
counter3=20=20
Thanks guys for help ,I will try these scripts and return back to you..