search through files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search through files
# 1  
Old 02-03-2008
Data 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...,,,
# 2  
Old 02-03-2008
Quote:
Originally Posted by 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:
Code:
B20080203-????

# 3  
Old 02-04-2008
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
# 4  
Old 02-04-2008
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
# 5  
Old 02-04-2008
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

# 6  
Old 02-06-2008
Thanks guys for help ,I will try these scripts and return back to you..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search files with today date and files come anytime in between 10 pm to 1 am

Hi, i want to make script. In a directory everyday( exclude sat and sun) in between 10 pm to 1 am there are 2 files comes and when file comes it will mail us. Format for files is mentioned below. please help me on making this, and also have year end consider and if files come after 12 am it... (6 Replies)
Discussion started by: pallvi_mahajan
6 Replies

2. Shell Programming and Scripting

search information in multiple files and save in new files

hi everyone, im stuck in here with shell :) can you help me?? i have a directory with alot files (genbank files ... all ended in .gbk ) more than 1000 for sure ... and i want to read each one of them and search for some information and if i found the right one i save in new file with new... (6 Replies)
Discussion started by: andreia
6 Replies

3. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

4. Shell Programming and Scripting

"Search for files in a file and write these files to another file which sould be in some other direc

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (1 Reply)
Discussion started by: dssyadav
1 Replies

5. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

6. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

7. Shell Programming and Scripting

search files

Hello folks I Hope everyone is fine. I have list of files below aa.tph p3iu.tph a984c.c3c p8xc4.pql I want to search all above files path from /mydata. How to do it with find command with script? (1 Reply)
Discussion started by: learnbash
1 Replies

8. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

9. UNIX for Dummies Questions & Answers

Help search in files

Hi guys. I had alot of luck last time I came here but not so much this time. I've searched around the forums for awhile but can't find exactly what I'm looking for. Here is my problem. I wrote a program that askes the user some questions. Then based off the info they entered it looks to find if... (4 Replies)
Discussion started by: pantera975
4 Replies

10. Shell Programming and Scripting

search for files excluding binary files

Hi All, I need a solution on my following find command find ./.. -name '*.file' -print BTW This gives me the output as belows ./rtlsim/test/ADCONV0/infile/ad0_dagctst.file ./rtlsim/test/ADCONV0/user_command.file ./rtlsim/test/ADCONV0/simv.daidir/scsim.db.dir/scsim.db.file... (2 Replies)
Discussion started by: user_prady
2 Replies
Login or Register to Ask a Question