![]() |
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 ? |
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. |
Quote:
Quote:
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. |
Quote:
|
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... |
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. |
i think you can remove the /Volumes part by first changing directories to /Volumes, then executing the command:
Code:
#!/bin/shCode:
#!/bin/sh |
here's a little starter some fun with python :
Code:
#!/usr/bin/env python |
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 |
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. |
Problem is that the server name changes for every user... so I guess I'm stuck with the slow AS way... :(
|
Quote:
if you want to make it the script more user friendly, you could execute it using platypus |
How about good old sed:
Code:
du -ch /Volumes/ServerName | col -b | sed 's/\/.*\/.*\///' > ~/Desktop/ServerName.txtcheers, pink |
Quote:
chris |
Quote:
But I will try it out ! and report back here ... |
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 |
Quote:
chris |
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. |
Quote:
pink |
Quote:
And it actually works properly... Just had to remeber than in AS I needed double escaped backslashes and then everything became all pretty :D |
Quote:
|
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 ? ;) |
Quote:
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 |
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.