LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2004, 08:58 AM   #1
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Rep: Reputation: 15
ksh math script


I'm writing a script to take a list of directories in a file(formatted: dir,spaceused) and echo the 5 largest to the screen. I can't figure out the math operation/loop to handle this. Any help?
 
Old 10-12-2004, 09:10 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
From the command line it might look something like this ....
du -hm | sort -n | tail -n 5
 
Old 10-12-2004, 09:44 AM   #3
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks but that wouldn't work for my need. I've got a listing in a file(looking back it's not important that it's directories) that is created by another script. Not exactly what I'm looking for but it's got me thinking.

I could use a sed to remove the directory names that are in the file created by the original script, sort what is left which would be the size on disk and then grep for the largest file sizes but, unless I'm overthinking or don't know a way to pull it off, this would only print the largest 5 times and probably take a bit to run...
 
Old 10-12-2004, 09:58 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Just sort the file, don't remove the file names.

Try
Code:
sort -n -k -o newfile
to specify column positions within the source file
 
Old 10-12-2004, 09:59 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
PS:
If your requirment is to print just numbers, use cut.
 
Old 10-12-2004, 10:32 AM   #6
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
thanks for the replys, that sort command doesn't seem to be working. not sure what I should be putting after the -k, man page isn't helping much. I thought it should be the character where I want the sort to start but that's not what it's doing. I do need the directory names printed as well.
 
Old 10-12-2004, 11:00 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I think it depends on the separator. Sort probably takes white-spaces by default as the separator between columns. I think theres some switch for it. To skip a field, +m. (+1 skips one field).

So `cat InFile | sort +1nr`

sorts on the second field, by byte count (numeric) and reversed.

End
 
Old 10-12-2004, 12:58 PM   #8
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
k, that won't work then, there is no whitespace in the other file. Settled on the sed/sort/tail command and then doing a loop back through the original. not to difficult but I can't think apparently, I'm not getting this loop to work.
 
Old 10-12-2004, 01:28 PM   #9
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
while [ $# -gt 0 ]

do
sed -e 's/\(.*,\)\(.*\)/\2/' $1.dir | sort -n | tail -n 5 >> tempd.out
for dir in `cat tempd.out`
echo "`grep $dir $1.dir`"
done >> outfile



that's the code as it is now. I need two loops as there are two files that the sed/sort/tail/cat/grep needs to run through. As I've been doing it, I either end up with it not doing anything, endless loop, or unexpected do/done/echo.
 
Old 10-13-2004, 06:33 AM   #10
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
sorted: overthinking... now I just have to figure out how to organize what I got out...
 
Old 10-13-2004, 08:06 AM   #11
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
K, trying to finish this up. Now need to organize the data a specific way. I had it just do two seperate "charts" but that's apparently not good enough. I had it do:

User Disk Usage
-------------------------

Directory Disk Usage
-----------------------------

but what is wanted is:

User User Disk Usage Directory Directory Disk Usage
------------------------------------------------------------------------------


the data gets pulled from one file in the format:
user,19023745
user2,429375
user3,398157
user4,193265
user5,6572
dir1,10594601982
dir2,63195987
dir3,56818763
dir4,315617
dir5,156399

any help getting it in the format wanted would be greatly appreciated.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gpg from ksh script Risc91 AIX 2 05-11-2005 10:45 AM
ksh directory comparison script SeT Programming 4 08-30-2004 11:57 AM
Can somebody help me with a ksh script? twentymil AIX 7 01-21-2004 09:55 AM
ksh script problem pldobs Programming 2 12-24-2003 11:38 PM
KSH script AquamaN Programming 2 12-08-2003 11:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration