Personal computing discussed

Moderators: renee, SecretSquirrel, notfred

 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

processing output from 'ls'

Sun Feb 08, 2009 2:24 pm

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?
 
bitvector
Grand Gerbil Poohbah
Posts: 3293
Joined: Wed Jun 22, 2005 4:39 pm
Location: San Francisco, CA

Re: processing output from 'ls'

Sun Feb 08, 2009 2:33 pm

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.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: processing output from 'ls'

Sun Feb 08, 2009 2:42 pm

Great. Good tip regarding find. Thanks BV, I'll give it a shot and tell you how it goes.
 
bitvector
Grand Gerbil Poohbah
Posts: 3293
Joined: Wed Jun 22, 2005 4:39 pm
Location: San Francisco, CA

Re: processing output from 'ls'

Sun Feb 08, 2009 2:45 pm

And speaking of find, you could do this directly with find's exec functionality, too:
find . -type f -exec echo '<img src="{}" />' \;
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: processing output from 'ls'

Sun Feb 08, 2009 3:04 pm

You are blowing my mind. Must... cognize...
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: processing output from 'ls'

Sun Feb 08, 2009 3:05 pm

 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: processing output from 'ls'

Sun Feb 08, 2009 4:06 pm

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.
Nostalgia isn't what it used to be.
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: processing output from 'ls'

Sun Feb 08, 2009 4:18 pm

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.
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: processing output from 'ls'

Sun Feb 08, 2009 4:19 pm

Anything piped to bash gets executed as if it was commands typed in a terminal window.
Nostalgia isn't what it used to be.
 
SecretSquirrel
Minister of Gerbil Affairs
Posts: 2726
Joined: Tue Jan 01, 2002 7:00 pm
Location: North DFW suburb...
Contact:

Re: processing output from 'ls'

Sun Feb 08, 2009 8:44 pm

Awk is your friend too...

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

--SS
 
flip-mode
Grand Admiral Gerbil
Topic Author
Posts: 10218
Joined: Thu May 08, 2003 12:42 pm

Re: processing output from 'ls'

Sun Feb 08, 2009 10:00 pm

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.
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: processing output from 'ls'

Sun Feb 08, 2009 10:20 pm

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.
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: processing output from 'ls'

Sun Feb 08, 2009 10:49 pm

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....
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
bitvector
Grand Gerbil Poohbah
Posts: 3293
Joined: Wed Jun 22, 2005 4:39 pm
Location: San Francisco, CA

Re: processing output from 'ls'

Sun Feb 08, 2009 10:51 pm

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)
 
SecretSquirrel
Minister of Gerbil Affairs
Posts: 2726
Joined: Tue Jan 01, 2002 7:00 pm
Location: North DFW suburb...
Contact:

Re: processing output from 'ls'

Mon Feb 09, 2009 7:52 am

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
 
Usacomp2k3
Gerbil God
Posts: 23043
Joined: Thu Apr 01, 2004 4:53 pm
Location: Orlando, FL
Contact:

Re: processing output from 'ls'

Mon Feb 09, 2009 8:31 am

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
 
just brew it!
Administrator
Posts: 54500
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: processing output from 'ls'

Mon Feb 09, 2009 8:44 am

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.
Nostalgia isn't what it used to be.
 
mattsteg
Gerbil God
Posts: 15782
Joined: Thu Dec 27, 2001 7:00 pm
Location: Applauding the new/old variable width forums
Contact:

Re: processing output from 'ls'

Mon Feb 09, 2009 8:46 am

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.
...
 
Usacomp2k3
Gerbil God
Posts: 23043
Joined: Thu Apr 01, 2004 4:53 pm
Location: Orlando, FL
Contact:

Re: processing output from 'ls'

Mon Feb 09, 2009 9:25 am

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.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On