PDA

View Full Version : Been done before? (before I waste my time)



Virchanza
12-04-2008, 11:31 AM
I'm thinking of coding a program but I dunno if it's been done before so I wanna ask here first before I waste my time.

Take a program like "find" that prints strings to the screen. Let's say you want to perform an operation on every string that it prints, and let's say that the operation is as follows:

rm X

(where X is the string that comes out of "find")

Well the idea is that I'd have a program called "many" that you use as follows:

find | many rm X

What it does is perform the operation "rm X" on every string that gets printed by "find".

I came up with this idea because I was moving all my stuff from my MSWindows partition across to my Linux partition (I'm abandoning MSWindows in favour of Linux). So anyway, my Windows folders had all sorts of stupid names like "My Videos", so I wanted a quick way of doing the following to every file and directory:
1) Replace uppercase characters with lowercase
2) Replace spaces with underscores
3) Remove "My " at the start

To do the filename conversion, I could have an extremely simple little command line program that takes an input such as "My Home Videos" and gives the output as "home_videos". Let's say this program is call "name_linifier".

It would be great to be able to use "name_linifier" as follows:

find | many mv X `name_linifier X`

Does such a program already exist? It would be so immensely useful that I get the idea it's been done before, but if it hasn't then I'll get coding it right away.

(By the way I realise I'll have a little problem parsing `name_linifier X` but it won't be a problem, I've already got a workaround in mind, I can just replace the apostrophe character with some other character of choice)

terminal86
12-04-2008, 11:48 AM
Hey Virchanza,
first thing i would recommend you change the tite , it isn't very descriptive.

To your question , doesn't sed perform a part of your project?
I think it's possible to combine it with other commands (direct or by script).

And at all , don't you think it would be faster to do it by hand than writing a program for it?

Cheers,
terminal

Virchanza
12-04-2008, 11:59 AM
Just to be clear, the project in question is "many", not "name_linifier".


To your question , doesn't sed perform a part of your project?Perhaps it does, I haven't used "sed" before but I've read a little bit about it in Linux tutorials.


I think it's possible to combine it with other commands (direct or by script)Basically I'm looking for a way to execute a command on every string that gets outputed from a program, so to create an individual folder for each of your groups, you could do:

groups | many mkdir X


And at all , don't you think it would be faster to do it by hand than writing a program for it?Not really I'd quite a few files. Anyway the idea is that this will be reusable, not only by me but by the whole *nix community.

Virchanza
12-04-2008, 12:52 PM
Never mind, I have since learned that this can be done quite easily using "xargs" in conjunction with scripts.

For instance, you could write a script as follows:



mv $0 `name_linifier $0`


and then use it as follows:



find | xargs name_linifier


It does exactly what I'm looking for, plus you have the freedom of scripting to make it do whatever you want.