The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   Filtering an output (http://hintsforums.macworld.com/showthread.php?t=36626)

Raven 03-16-2005 09:27 AM

Filtering an output
 
Right now the output I hav from a command is such as this:

100k /Volumes/ServerName/folder

I have a large list of this that I wishg to output to a text file. That part is easy... But what I can't find how to do (if it can be done through the command line) is to take out the "/Volumes/ServerName" part of it... It appears on each line and it doens't serve any purpose for what I need... I'll know from the name of the output what server the infor is coming from so I dopn't want the complete path to appear, Only the list of folders.
Any one know how to add to a command to filter this out ?

voldenuit 03-16-2005 09:34 AM

You'd just pipe the result of the first command through some awk/perl/python regexp to edit it out.

Or you could try to come up with a command that formats its output as you want it in the first place.

Raven 03-16-2005 09:42 AM

Quote:

some awk/perl/python regexp to edit it out
Not sure that I can do that yet as I don't know perl or python... have looked at awk but couldn't see how I could edit some parts out...

Quote:

Or you could try to come up with a command that formats its output as you want it in the first place
Any suggestions on what to look for ?

Sorry I'm quite profiscient with Macs but the command line is still rather new to me so I don't know all its capable of.

voldenuit 03-16-2005 09:45 AM

Quote:

Any suggestions on what to look for ?
Stating what you're really up to and how far you've got might be a good start ;) .

Raven 03-16-2005 11:02 AM

Point well taken there :rolleyes:

Here is the command that I have now to output

du -ch /Volumes/ServerName | col -b > ~/Desktop/ServerName.txt"

basically its to have users be able to see a list of folders and the size of those on their network shares...

voldenuit 03-16-2005 11:15 AM

I used to run a cronjob that would mail soon to be overquota users a du-output I piped through sort to have the biggest files/folders on top. That won't work with the h-option of du, but is probably more useful anyway.

You'll also need to tell sort to use numerical and not alpha-rules when sorting.

I wouldn't bother to cut the servername, if you get more than one, you're gonna take that part out anyway and I think it avoids confusion to the user to have a complete path.

vancenase 03-16-2005 11:50 AM

i think you can remove the /Volumes part by first changing directories to /Volumes, then executing the command:

Code:

#!/bin/sh
cd /Volumes
du -ch * | col -b > ~/Desktop/ServerName.txt"

or possibly remove the server name:
Code:

#!/bin/sh
cd /Volumes
du -ch */* | col -b > ~/Desktop/ServerName.txt"


vancenase 03-16-2005 12:09 PM

here's a little starter some fun with python :

Code:

#!/usr/bin/env python

import os

basedir = '/Volumes/'
dirlist = os.listdir(basedir)
for y in dirlist:
  if os.path.isdir(y):
    os.chdir(basedir)
    os.system('du -ch '+y+' | col -b > ~/Desktop/'+y+'.txt')

there might be a better way of doing this, but this could get you started thinking in a scripting language :)

Raven 03-16-2005 12:56 PM

Thanks for all the advice, but in the present case, the alpha order is what I wanted as output as when users look at their folders they are listed in this order anyways.
As for scripting its part of an AppleScript app for now, so changing the directory is not an option when using a "do shell script" command.
As for the Python suggestion... I just wouldn't know how to include that in my script... And the suggestion you gave, I do understand the code and what it does, but would I just paste this in xCode ? Sorry if I seem think, but the only xCode scripting I've done is AS :o

voldenuit 03-16-2005 01:30 PM

You'd just need to call the python script from AS by having it executed by the shell.

Unless the code.police gets you ;) , nothing stops your from doctoring the text file with AS if you insist...
Don't expect lightning speed and the other options suggested are probably more elegant, but whatever works for you is ok.

Raven 03-16-2005 02:02 PM

Problem is that the server name changes for every user... so I guess I'm stuck with the slow AS way... :(

vancenase 03-16-2005 02:37 PM

Quote:

Originally Posted by Raven
Problem is that the server name changes for every user... so I guess I'm stuck with the slow AS way... :(

the python script will search any/all directories in /Volumes, so it should work for any user.

if you want to make it the script more user friendly, you could execute it using platypus

pink 03-16-2005 02:37 PM

How about good old sed:
Code:

du -ch /Volumes/ServerName | col -b | sed 's/\/.*\/.*\///' > ~/Desktop/ServerName.txt
this would remove the first three / in each line and everything in between.

cheers, pink

DaleCooper 03-16-2005 04:21 PM

Quote:

Originally Posted by pink
How about good old sed:
Code:

du -ch /Volumes/ServerName | col -b | sed 's/\/.*\/.*\///' > ~/Desktop/ServerName.txt
this would remove the first three / in each line and everything in between.

cheers, pink

I'm sorry but it wouldn't. The * is greedy so the regex removes everything up to the last slash. A better way is to use sed 's/\/[^/]*\/[^/]*\///' which explicitly states that only characters that aren't slashes are allowed between slashes. It doesn't work, however, if there are fewer than three slashes. sed 's/\(\/[^/]\+\)\{,2\}\///' should work for that. And yes, sed syntax is very pretty indeed.

chris

Raven 03-16-2005 04:35 PM

Quote:

And yes, sed syntax is very pretty indeed.
Closer to headache for me... How do you guys understand what to do wisth sed ? To me it makes no sense :D
But I will try it out ! and report back here ...

derekhed 03-16-2005 04:48 PM

s = substitute
s/stuff/other stuff/ = substitute 'stuff' with 'other stuff'
\ = escape, or treat the next character as a literal, i.e., without any special meaning.
[^/] = a 'class'. In this case, used with the ^ (negation) to create a class of all characters EXCEPT a slash.
{,2} = a 'range'. Used to find N number of matches, in this case from none to two.
+ = "One or more of the preceeding". This is tied to the [^/] stuff previous, so it helps gobble up any junk before the next slash.

Does that help? :D

DaleCooper 03-16-2005 05:00 PM

Quote:

Originally Posted by Raven
Closer to headache for me... How do you guys understand what to do wisth sed ? To me it makes no sense :D
But I will try it out ! and report back here ...

So, derekhed beat me to a detailled answer but let it be said that a strong background in perl helps; though perl's regex syntax is quite a bit cleaner as there are far fewer backslashes involved.

chris

voldenuit 03-16-2005 05:47 PM

There is an excellent O'Reilly book "Mastering Regular Expressions" that will turn what looks like modem noise first into expressions that actually start making sense :) .

BBEdit also includes a pretty well done tutorial.

pink 03-17-2005 03:34 AM

Quote:

Originally Posted by DaleCooper
I'm sorry but it wouldn't. The * is greedy so the regex removes everything up to the last slash. A better way is to use sed 's/\/[^/]*\/[^/]*\///' which explicitly states that only characters that aren't slashes are allowed between slashes. It doesn't work, however, if there are fewer than three slashes. sed 's/\(\/[^/]\+\)\{,2\}\///' should work for that. And yes, sed syntax is very pretty indeed.

chris

Sorry, my fault. I was in a hurry and tested on the output of du -d3, where it appears to work, obviously.

pink

Raven 03-17-2005 08:45 AM

Quote:

Originally Posted by voldenuit
There is an excellent O'Reilly book "Mastering Regular Expressions" that will turn what looks like modem noise first into expressions that actually start making sense :) .

BBEdit also includes a pretty well done tutorial.

Guess I'll look into it... That book may at least be a good reference so I understand onther posts you gusy put in !

And it actually works properly... Just had to remeber than in AS I needed double escaped backslashes and then everything became all pretty :D

derekhed 03-17-2005 11:28 AM

Quote:

Originally Posted by Raven
Guess I'll look into it... That book may at least be a good reference so I understand onther posts you gusy put in !

You could start here

Raven 03-17-2005 12:43 PM

One thing I realized... It does filter out three levels... That I understood, but I realize now that I only need to take out the first two... and I don't know whot o mod the sed command you gave me to do only two...
Some more help please ? ;)

DaleCooper 03-17-2005 02:17 PM

Quote:

Originally Posted by Raven
One thing I realized... It does filter out three levels... That I understood, but I realize now that I only need to take out the first two... and I don't know whot o mod the sed command you gave me to do only two...
Some more help please ? ;)

you can adjust the level by modifying the number in curly brackets. If you want at most two levels to be deleted, write \{,1\} instead of \{,2\}.

If you are using GNU sed you can substitute the entire brackets part with \? which means zero or once. GNU sed is as far as I know not a standard part of Mac OS X but can be installed with fink.

chris

Raven 03-17-2005 02:51 PM

I actually found a way "around" this in my AS... Because I wanted to take out part of a path, an issue came up with the slashes in what I wanted to remove... a friend of mine, used to perl, told me that I could try to replace those with = signs... and it does work...
So what I did is, indstead of trying to remove the first two parts, I added variables... I have a part that will change form user to user, but that one they have to input at the begining of the screipt anyways... So, since I have that part as a string, and know the first part is always /Volumes/, it created a new variable containing both in the right order and then entered them in sed like this:

du -ch " & variable & " | col -b | sed 's=" & variable & "==' > ~/Desktop/ServerName.txt

variable = /Volumes/server_name_variable

And now I have my nice cleaned up output !
AS does have features that can be very usfull some times...

Thanks for all the help !


All times are GMT -5. The time now is 06:23 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.