Trying to Parse An Inherited Command/Routine


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to Parse An Inherited Command/Routine
# 1  
Old 04-27-2016
Trying to Parse An Inherited Command/Routine

I am am one of these people that it isn't good enough just to say, "Here, try this...". it is important for me to understand how and why something works (or doesn't work.)

All that being said, I am trying to parse out a command that we use that was handed down to me by someone I can no longer ask questions from, and I **think** I have most of it, but not quite.

The output of this routine prints a list of amount of space used by each subdirectory, sorted by size, low to high.

The command/routine looks like so:
(and it works like a charm, btw)

Code:
du -h | perl -e 'sub h{%h=(K=>10,M=>20,G=>30);($n,$u)=shift=~/([0-9.]+)(\D)/;
return $n*2**$h{$u}}print sort{h($a)<=>h($b)}<>;'

From what I can muddle out, this routine:
1. Runs the "du -h" command
2. Pipes the results into a subroutine executed by perl
3. Splits each line of the output into two pieces of data each (size and subdirectory) and puts that into a hash
3. Does some kind of math on the size of each element in the hash (the "**" throws me off, and I can't locate info on what that means)
4. And prints the result with subdirectory, sorted by size, ascending

Do I have this even close to being right?

Thanks in advance for any feedback.
Smilie

Last edited by he204035; 04-27-2016 at 06:44 PM..
# 2  
Old 04-27-2016
Quote:
Originally Posted by he204035
3. Does some kind of math on the size of each element in the hash (the "**" throws me off, and I can't locate info on what that means)
The ** is an exponential. 2 ** 10 is 2 to the 10th power, or 1024.

It's looking up the values of K, M, and G in the hash and getting 10, 20, 30 to calculate the sizes of 2^10, 2^20, and 2^30 for kilobytes, megabytes, and gigabytes respectively. Each line gets pushed into an array, and finally, the giant array is sorted, and printed.

Quote:
Do I have this even close to being right?
Pretty close -- but I have no idea why they made it so hard on themselves. If you don't want to bother translating K/M/G values, don't ask for them in the first place.
Code:
# smallest to largest
du | sort -n
# largest to smallest
du | sort -rn

I see that sort of thing a lot. People will script ls -l all the time, then use a three-deep pipe chain to strip out everything but the filename... emulating plain ls with no flags.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-27-2016
Thank you!!!

That was driving me BANANAS!!

I like having the K/M/G displayed. Makes it pretty tidy.

Thanks again!
# 4  
Old 04-27-2016
Quote:
Originally Posted by he204035
I like having the K/M/G displayed. Makes it pretty tidy.
I can see that, but why sort after? It means doing convert, un-convert, sort, re-convert. Why not sort the machine-readable, and print the human-readable?

Code:
du -B 1| sort -n | 
        awk '{ N=1; while((N<4) && (($1+0) > 1024)) { N++ ;  $1 /= 1024 }
        $1=sprintf("%d%s", $1, substr(" KMG", N,1)); } 1' OFS="\t" -

It divides by 1024 until it's either less than 1024, or big enough it's run out of letters. It picks space, K, M, or G depending on the number of times it divided.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Need command to parse data

Hi Friends, I have data like below t064266 I want output into this format t064266 Data are space delimited and i want parse third column data. Thanks (9 Replies)
Discussion started by: Jagaat
9 Replies

2. UNIX for Advanced & Expert Users

Permission inherited by users

Hi Felas I am running Fedora and have this issue on my system: I have the following users: jane:x:552:100::/usr1/users/jane:/bin/bash jules:x:553:100::/usr1/users/jules:/bin/bash I have the following group which all users belong to: lab:x:100:root,jules,jane I have the... (4 Replies)
Discussion started by: ndaka123488
4 Replies

3. Shell Programming and Scripting

Parse ouput within an AWK Command

I have a script that I am writing to parse the output of information from a command. Here is one of the lines of the output exported into the variable OUTPUT: export OUTPUT=”name_of_system,0,5,9,55,ip_address,another_value,/PATH/OF/A/VALUE/I/NEED" I can get the output I need... (5 Replies)
Discussion started by: jake0391S
5 Replies

4. Shell Programming and Scripting

Perl - Call a sub routine in a command

Hi all I have written a simple perl script that has different options i.e. myscript -l -p etc i have it so when it runs without any switches it runs a subroutine called nvrm_norm i want to be able to do a -p option and run pall -w -f and then called the subruotine pall is... (1 Reply)
Discussion started by: ab52
1 Replies

5. Shell Programming and Scripting

Parse a string as a command

I've a problem parsing a string as a command: Consider script stefano.sh as following: #!/usr/bin/sh txtshell="./parser.sh /ews/MyEventHandler/data/handler/StopAndMail.php eventid=StopAndMail.MVIN.6300 lot_number=1122FXB facility=EWSF3 'mailto=prova.prova@nohost.com, prova.test@nohost.com'... (2 Replies)
Discussion started by: buonstefano
2 Replies

6. AIX

Inherited VIO server an LPARs

Lucky me, someone has installed a server and got it running with the best intentions, but leaving me a headache. :wall: We have a simple p520 with 4 disks. 2x145Gb & 2x300Gb. The smaller disk pair have been built into a VIO mirrored rootvg, and quite right too. The other two disks form a... (3 Replies)
Discussion started by: rbatte1
3 Replies

7. Shell Programming and Scripting

Parse variable into a command

What I am trying to do is write a script that makes some commands easier, now one of the commands is to rebuild mail boxes in plesk, which would would be something like /usr/loca/psa/websrvmng --vhost-reconfigure --vhost-name=domain.com but the domain.com bit is going to need to change with... (3 Replies)
Discussion started by: foz
3 Replies

8. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

9. Shell Programming and Scripting

Trying to manage an inherited Ksh script

Hi I am trying to get to know a script I have inherited. The original author is no longer available. Does anyone know if there is a utility to parse a ksh script and generate a report/output which details all the defined functions and their arguments? Maybe even provide a 'call trace' of how the... (1 Reply)
Discussion started by: ajcannon
1 Replies

10. SCO

Inherited server - need help

I have inherited admin duties on an old server running SCO Open Server 5. No changes have been made to this thing for a very long time. The server has started randomly disconnecting itself from the network. I have to reboot the server to get it to find the network again. It happens about once a... (3 Replies)
Discussion started by: jmickens
3 Replies
Login or Register to Ask a Question