Page 1 of 1

processing output from 'ls'

Posted: Sun Feb 08, 2009 2:24 pm
by flip-mode
I have a directory of images and I was hoping i could 'ls' that directory and process the output to automatically generate html image tags. What is the best way to do this? There is a program called 'sed' that looked like it might be the one. Dunno. But bassically all I need to do is prepend each line of output with <img src=" and then append each line with " /> and write that output to an html file. At least that is my thinking. Help?

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 2:33 pm
by bitvector
Yep, you could do that with sed:
ls | sed -e 's%^%<img src="%' -e 's%$%" />%'

The basic sed syntax used here is 's/<match>/<replace>/' (which means search for match and replace it with replace), but sed lets you choose any delimiter character instead of /, so I use % (so we don't have to escape the actual / character you want to put in your output). In this case we match the regexp special character ^, which means the beginning of the line and "replace" it with the stuff you want prepended. In the second expression we match the special regexp character $, which means end of line.

On Unix-like systems, there's more than on way to skin a cat. You could also do it with xargs:
ls | xargs -n1 -I@ echo '<img src="@" />'

That xargs command runs a command once per output from ls. In the command expression, it replaces anywhere @ appears with the output it got from ls.

Usually when I want to get a list of files to pipe to these commands, I'd generally use find instead of ls, though:
find . -type f
It gives me a lot more control and ls is sometimes creatively aliased, etc.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 2:42 pm
by flip-mode
Great. Good tip regarding find. Thanks BV, I'll give it a shot and tell you how it goes.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 2:45 pm
by bitvector
And speaking of find, you could do this directly with find's exec functionality, too:
find . -type f -exec echo '<img src="{}" />' \;

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 3:04 pm
by flip-mode
You are blowing my mind. Must... cognize...

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 3:05 pm
by flip-mode

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 4:06 pm
by just brew it!
A favorite tactic of mine to build up complex find/sed commands is to do it interactively at the shell prompt. Pipe the output of find to sed, and let the output display in the terminal window. After each iteration, hit up-arrow to recall the previous command, and keep tweaking the find/sed options until you're happy with the commands it is generating. Then just tack a "| bash" onto the very end of the command line to execute the sed output instead of displaying it.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 4:18 pm
by flip-mode
That is a cool tip JBI but I am not sure what, in this particular instance, would happen if if piped the output of sed to bash.

Matter of a fact, I didn't know anything could be piped to bash.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 4:19 pm
by just brew it!
Anything piped to bash gets executed as if it was commands typed in a terminal window.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 8:44 pm
by SecretSquirrel
Awk is your friend too...

ls -1 | awk '{print "<img src=\""$1"\"/>"}'

--SS

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 10:00 pm
by flip-mode
SecretSquirrel wrote:
Awk is your friend too...
ls -1 | awk '{print "<img src=\""$1"\"/>"}'
--SS


Wow, is there such a thing as having too many friends :lol:

I've studied up on and practiced with sed for the last two hours to finally have it down pretty well. It is nice.

But I'd like to understand those other methods too.

It is pretty damn sweet to be able to take a directory listing and do some voodoo with it.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 10:20 pm
by notfred
flip-mode wrote:
Wow, is there such a thing as having too many friends :lol:
Yes, there are a ton of ways to do just about anything in the Unix world. I'm quite surprised - and thankful :P - that nobody has posted a perl version as well.

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 10:49 pm
by titan
notfred wrote:
flip-mode wrote:
Wow, is there such a thing as having too many friends :lol:
Yes, there are a ton of ways to do just about anything in the Unix world. I'm quite surprised - and thankful :P - that nobody has posted a perl version as well.

Give me a week while I learn it real quick. I'm sure PHP could be used in some way too....

Re: processing output from 'ls'

Posted: Sun Feb 08, 2009 10:51 pm
by bitvector
notfred wrote:
Yes, there are a ton of ways to do just about anything in the Unix world. I'm quite surprised - and thankful :P - that nobody has posted a perl version as well.
I think most of us here are not complete maniacs and therefore prefer Python to Perl. :lol:

Speaking of Python, why not:
import os
lst = ['<img src="' + f + '"/>' for f in os.listdir(".") if os.path.isfile(f)]
for f in lst:
    print(f)

Re: processing output from 'ls'

Posted: Mon Feb 09, 2009 7:52 am
by SecretSquirrel
notfred wrote:
flip-mode wrote:
Wow, is there such a thing as having too many friends :lol:
Yes, there are a ton of ways to do just about anything in the Unix world. I'm quite surprised - and thankful :P - that nobody has posted a perl version as well.


ls | perl -e 'while(<STDIN>) {chomp; print "<img src=\"" . $_ ."\"/>\n";}'

So that's the "cheating version". I was to lazy to write out the pure Perl, readdir, based version. There are about as many ways to do things in Unix as there are people doing them...

--SS

Re: processing output from 'ls'

Posted: Mon Feb 09, 2009 8:31 am
by Usacomp2k3
When I come upon a similar type of situation, I usually take a slightly different (window-centric) approach.
1/ dir /b -> output.html
2/ open up the output.html in notepad
3/ copy the lines into excel
4/ add whatever formatting I want (whether that be splitting up the file to give both a url and an image tags)
5/ copy it back into notepad

Re: processing output from 'ls'

Posted: Mon Feb 09, 2009 8:44 am
by just brew it!
Usacomp2k3 wrote:
When I come upon a similar type of situation, I usually take a slightly different (window-centric) approach.
1/ dir /b -> output.html
2/ open up the output.html in notepad
3/ copy the lines into excel
4/ add whatever formatting I want (whether that be splitting up the file to give both a url and an image tags)
5/ copy it back into notepad

...and I think you've indirectly illustrated why many people who make the jump to *NIX eventually come to prefer it. The Windows-centric approach is easier to do the first time; but harder to automate with a script.

Re: processing output from 'ls'

Posted: Mon Feb 09, 2009 8:46 am
by mattsteg
just brew it! wrote:
Usacomp2k3 wrote:
When I come upon a similar type of situation, I usually take a slightly different (window-centric) approach.
1/ dir /b -> output.html
2/ open up the output.html in notepad
3/ copy the lines into excel
4/ add whatever formatting I want (whether that be splitting up the file to give both a url and an image tags)
5/ copy it back into notepad

...and I think you've indirectly illustrated why many people who make the jump to *NIX eventually come to prefer it. The Windows-centric approach is easier to do the first time; but harder to automate with a script.

For argument's sake, I've done similar (except using LaTeX instead of HTML) in windows using only DOS batch scripting.

Re: processing output from 'ls'

Posted: Mon Feb 09, 2009 9:25 am
by Usacomp2k3
just brew it! wrote:
Usacomp2k3 wrote:
When I come upon a similar type of situation, I usually take a slightly different (window-centric) approach.
1/ dir /b -> output.html
2/ open up the output.html in notepad
3/ copy the lines into excel
4/ add whatever formatting I want (whether that be splitting up the file to give both a url and an image tags)
5/ copy it back into notepad

...and I think you've indirectly illustrated why many people who make the jump to *NIX eventually come to prefer it. The Windows-centric approach is easier to do the first time; but harder to automate with a script.

I can't argue with that.