not a programmer but need simple ? thing done

not a programmer but need simple ? thing done

Post by shab » Wed, 08 May 2002 21:10:41



For a directory I need the size of each file in it divided by 7 and
displayed - a small shell script should do this for me but I can't
figure out how to go about this - shell programming is not something
I'm up to speed on.

anyone have a simple (probably one line!) answer?

Thanks, shab.

 
 
 

not a programmer but need simple ? thing done

Post by Mike » Thu, 09 May 2002 07:38:40




Quote:> For a directory I need the size of each file in it divided by 7 and
> displayed - a small shell script should do this for me but I can't
> figure out how to go about this - shell programming is not something
> I'm up to speed on.
> anyone have a simple (probably one line!) answer?

Hmm, don't know about simple.  The following script might do something
along the lines of what you want.  If it was me, I think I'd likely do
it either in Perl, or write a small program (for speed).   If you need
this, contact me direct and I can do it for you  (it's a simple enough
program...)

You'll need to have "bc" installed for the division.  If you only need
integer division (20 / 7 = 2) then you can use bash's built-in maths.

It "works" by using "ls -lG" to get a listing of the directory,  reads
each line, calls "bc" to work out what one seventh of the filesize is,
then displays the result.  This isn't particularly efficient, but does
work (more or less).

The output is simply the filesize, a tab, and the name of the file.  I
hope you didn't want them listed in columns or anything like that;  if
so I'd be re-doing it in Perl.

Cut between the two sigdashes.
--
#!/bin/sh

# set $directory to the first arugment
directory="$1";

# if no arguments, display usage info
if [ -z "$directory" ];
then
        echo "Syntax:
$0 <directory>

Lists all files in the given directory, with their filesize divided by 7."
        exit 1;
fi

# run ls, pipe output through our commands...
ls -lG "$directory" |
{
        # throw away the "total" line
        read;

        # read each line of output
        while read perm links owner size month day time file;
        do
                # work out what 1/7th of the file size is, with the
                # help of bc.
                seventh=$(echo "scale=5 ; $size / 7" | bc);

                # alternatively, comment out the line above, and use
                # this one instead (for integer division)
                #seventh=$[$size / 7];

                # display the result
                echo -e "$seventh\t$file";
        done

Quote:}

exit 0;
--
Mike.   Remove "-spam" to mail me.

 
 
 

not a programmer but need simple ? thing done

Post by Garry Knigh » Thu, 09 May 2002 06:31:31




Quote:> For a directory I need the size of each file in it divided by 7 and
> displayed - a small shell script should do this for me but I can't
> figure out how to go about this - shell programming is not something
> I'm up to speed on.

> anyone have a simple (probably one line!) answer?

It would be probably be more than one line however you do it. Here's one
way to do it in Perl. If you save it as, say, 'fs7', then you'd call it
with 'fs7 dirname' where 'dirname' is the directory to list. The output
shows this kind of thing:

[garry]$ ./fs7 tmp
x  947 bytes
z  16 bytes
x.bak  837 bytes
faq.txt  2950 bytes
hdparm-timings  53 bytes
fix-services  185 bytes
music1.png  1050 bytes
website  80 bytes

If you want it displayed differently, play with the printf statement or ask
me nicely.  :o)

#!/usr/bin/perl -w
use strict;
my $dir = shift or die "Usage: $0 dirname";
chdir $dir or die "Can't change to $dir: $!\n";
opendir DIR, $dir or die "Can't open $dir: $!\n";
while (defined(my $file = readdir(DIR))) {
  next unless -f $file;
  printf "$file  %d bytes\n", (-s $file) / 7;

Quote:}

closedir DIR

--
Garry Knight

Linux registered user 182025

 
 
 

not a programmer but need simple ? thing done

Post by Baard Ove Kopper » Thu, 09 May 2002 08:11:19



> For a directory I need the size of each file in it divided by 7 and
> displayed - a small shell script should do this for me but I can't
> figure out how to go about this - shell programming is not something
> I'm up to speed on.

> anyone have a simple (probably one line!) answer?

Try this gawk script on your command-line:

$  ls -l | gawk '{ print $9 "\t" ($5/7) }'

*If your 'ls' shows blocks rather than kB,
 use 'ls -lk' instead.
*It may round off decimals.
*It may not work if you have filenames
 with spaces or tabs in them.
*You may get a line with a zero or
 something, due to 'ls -l' showing a
 line with the "total" in the beginning.
*Name and Size/7 is separated by a tab,
 which may result in wierd output if
 the length of the filenames differs
 too much.
*Swap $9 with ($5/7) if you need the
 size *before* the name.
*You may perhaps be able to use 'ls -s'
 instead.  You'd have to rewrite the
 script a bit ( $9 -> $2, $5 -> $1 ),
 and the result would be round-offs...
 but then it may work with filenames
 with spaces.

Apart from that, it should work.
Good luck.
-Koppe

 
 
 

not a programmer but need simple ? thing done

Post by shab » Thu, 09 May 2002 22:52:38


Thank you all for your solutions - I forgot to mention that the file
sizes will always be divisable by 7.  I'm going to use the one liner
and keep the other 2 for reference when I need to do something.

Thanks again!

shab

 
 
 

not a programmer but need simple ? thing done

Post by s. keeli » Sun, 19 May 2002 09:47:47




> > For a directory I need the size of each file in it divided by 7 and
> > displayed - a small shell script should do this for me but I can't
> > figure out how to go about this - shell programming is not something
> > I'm up to speed on.

> > anyone have a simple (probably one line!) answer?

> Try this gawk script on your command-line:

> $  ls -l | gawk '{ print $9 "\t" ($5/7) }'

Surely you mean:

   du | awk '{print $1/7, "\t",  $2}'

Five less chars to type.

--
Any technology distinguishable from magic is insufficiently advanced.
(*)  TopQuark Software & Services.  Contract programmer, server bum.
- -             Give up Spammers; I use procmail.
How to quote: http://learn.to/quote (Ger.) http://quote.6x.to (Eng.)

 
 
 

1. Doing simple things with AIX PS/2

I am in a small group of mathematicians who are trying to upgrade
from DOS to unix by putting AIX (v 1.1) on PS/2's (Model 80's and 70's).
We have an IBM Token Ring local network on which coexist AIX machines
using TCP/IP and DOS machines using IBM PC-LAN.

We have no full-time system help; we are end-users who want to
use these machines for scientific computing and visualization.  
IBM has not been much help, so we are appealing to the community.

Problem:  We can't seem to do some very basic things that even our dumb
DOS machines can do.

1) Terminal emulation: When we telnet or rlogin as a vt100 to a non-AIX
machine (eg SUN or VMS VAX) vi doesn't work properly.  For example, when
you type ^U the top line changes but not the rest of the screen; when you
delete a line, it is not deleted from the screen; etc.  Terminal emulation
to AIX machines is ok.

2) Scientific word processing: AIX comes with troff, but drivers
only for IBM Graphics Printers.  We have an HP Laserjet and Epson LQ-800's.
We also want to do LaTeX.  I can do troff (using Textware) or
plain TeX (using PCTeX) under DOS Merge, but this seems crazy, and
anyway LaTeX kills the DOS process instantly.

3) Why not use SCO Xenix?  We tried this a couple of years ago,
and had lots of problems.  There was no support for Token Ring, there
were hardware incompatibilities, and VP/IX never materialized.  They seem
more oriented to the "industry standard" machines like Compaq, etc.
Some of our people will stick with DOS and we hope that IBM will have
better arrangements for this.  We plan to try AIX Access for DOS Users
so that they can use the AIX machines as print servers and the PC-LAN
software can be junked. Does anyone have experience with this?

We hope AIX becomes very popular so that lots of 3rd party software
gets written for it, but unless these kinds of problems are fixed
it will die an early death.

Beset in Bethesda,
Arthur Sherman

(301) 496-4325

2. http://linuxbox/~user/ problem

3. Need help for a so simple thing...

4. HP tape drive compatibility

5. Need simple help from a good programmer..

6. PC Linux on Orange Micro 620?

7. 101 Things you can do I Linux but not in DOS/Win

8. 2 printers on 1 lpt port

9. need advice on doing the upload/download thing between PCs and Unix system

10. 101 things you can do in Linux but not in DOS

11. Dos programmer needs help on unix

12. DOS programmer needs CURSES teacher