The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-03-2008
Registered User
 

Join Date: Jul 2007
Posts: 41
Stumble this Post!
Unhappy search through files

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...,,,
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-03-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 709
Stumble this Post!
Quote:
Originally Posted by mm00123 View Post
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:
Code:
B20080203-????
Reply With Quote
  #3 (permalink)  
Old 02-03-2008
Registered User
 

Join Date: Jul 2007
Posts: 41
Stumble this Post!
Question

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
Reply With Quote
  #4 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 306
Stumble this Post!
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);
usage:

perl path/to/sum_counts.pl > path/to/outfile.txt
Reply With Quote
  #5 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Stumble this Post!
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
Hope it'll be good for you, ouput of the script :

Code:
./script B20080203-15*
counter1=10+100=110
counter2=11+40=51
counter3=20=20
Reply With Quote
  #6 (permalink)  
Old 02-05-2008
Registered User
 

Join Date: Jul 2007
Posts: 41
Stumble this Post!
Thanks guys for help ,I will try these scripts and return back to you..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0