![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help search in files | pantera975 | UNIX for Dummies Questions & Answers | 4 | 03-17-2008 07:13 AM |
| Search for a file and get the next two files | sunny_03 | UNIX for Dummies Questions & Answers | 3 | 02-15-2008 04:18 PM |
| search for files excluding binary files | user_prady | Shell Programming and Scripting | 2 | 09-04-2007 09:18 PM |
| search files | mpang_ | Shell Programming and Scripting | 3 | 07-28-2006 10:06 PM |
| search for files | problems | Shell Programming and Scripting | 1 | 04-28-2006 04:31 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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...,,, |
| Forum Sponsor | ||
|
|
|
|||
|
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 Code:
#!/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);
perl path/to/sum_counts.pl > path/to/outfile.txt |
|
|||
|
Well, in pure shell.
Code:
#!/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
Code:
./script B20080203-15* counter1=10+100=110 counter2=11+40=51 counter3=20=20 |