The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting data from files.. anchal_khare Shell Programming and Scripting 1 04-03-2008 07:56 AM
Problem in extracting vector data ahjiefreak Shell Programming and Scripting 2 03-18-2008 06:09 AM
Extracting Data from a File oop UNIX for Dummies Questions & Answers 0 07-31-2007 12:48 PM
Using loop reading a file,retrieving data from data base. Sonu4lov Shell Programming and Scripting 1 01-19-2007 03:38 AM
Extracting Data From Sendmail calex Shell Programming and Scripting 3 01-15-2007 08:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-26-2001
nitin's Avatar
nitin nitin is offline
Registered User
  
 

Join Date: Aug 2001
Location: Toronto, ON
Posts: 88
For loop problem extracting data

I have a problem with my loops. I have a file called users.dat, it has all the users in it. Then I extracted a list of users sending out number of mails with date from Netscape logs. The extracted list (mailuse.dat) has 3 fields: username, number of mails, date. (One user can show up several times). I would like to add up all the numbers in the 2nd field. Here is my lame effort:
--first loop gets users, second loop tries to get the 2nd field and adds them up.---

Code:
for i in `cat users.dat | awk '{print $2}'
total=0 
do
  for j in `grep $i mailuse.dat | awk '{sum+=$2}'
  do
  total=`expr $total + $j`
  echo $total, $j
  done
done

-----------
TIA, (plese help!)
Nitin

added code tags for readability --oombera

Last edited by oombera; 02-20-2004 at 12:39 PM..
  #2 (permalink)  
Old 11-26-2001
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,127
The users.dat file serves no useful purpose and it is leading you into a double loop. Try this...

Code:
#! /usr/bin/ksh

firsttime=1

sort mailuse.dat | while read user count date ; do
        if ((firsttime)) ; then
                firsttime=0
                olduser=$user
                sum=0
        fi

        if [[ $user != $olduser ]] ; then
                echo $olduser $sum
                olduser=$user
                sum=0
        fi
        ((sum=sum+count))

done

echo $olduser $sum

exit 0

  #3 (permalink)  
Old 11-27-2001
nitin's Avatar
nitin nitin is offline
Registered User
  
 

Join Date: Aug 2001
Location: Toronto, ON
Posts: 88
Perderabo,
Thanks a million! Your script is fast, it doesn't grep each name in the user.dat thingy.

I also tried this thing, thought I would just paste it. (I do realize that this is very bad programming and slow too!)


Code:
#!/bin/sh
rm /tmp/mails.log  # remove old logs
for i in `cat walusers.dat | awk '{print $1}'`
do
 total=0;
 echo $i; # shows which users have gone by!!
 for j in `grep $i maildeli.final | awk '{print $2}'`
  do 
  echo $j;
  total=$total+$j;
  done;
 echo $i " " $total >>/tmp/mails.log;
done

After using Perderabo's script, I decided to join the script output with users.dat. That gave me a a list of "power mail users".
-----
join -a 1 -o Nomails -e 1.1 2.2 user.dat mails.log >usage.list
-----
Thanks again!

added code tags for readability --oombera

Last edited by oombera; 02-20-2004 at 12:39 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0