processing output from 'ls'

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

processing output from 'ls'

Postposted on 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?
flip-mode
Gerbil Khan
Silver subscriber
 
 
Posts: 9084
Joined: Thu May 08, 2003 12:42 pm
Location: Cincinnati, OH

Re: processing output from 'ls'

Postposted on 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.
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 4:39 pm
Location: Mountain View, CA

Re: processing output from 'ls'

Postposted on 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.
flip-mode
Gerbil Khan
Silver subscriber
 
 
Posts: 9084
Joined: Thu May 08, 2003 12:42 pm
Location: Cincinnati, OH

Re: processing output from 'ls'

Postposted on 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="{}" />' \;
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 4:39 pm
Location: Mountain View, CA

Re: processing output from 'ls'

Postposted on Sun Feb 08, 2009 3:04 pm

You are blowing my mind. Must... cognize...
flip-mode
Gerbil Khan
Silver subscriber
 
 
Posts: 9084
Joined: Thu May 08, 2003 12:42 pm
Location: Cincinnati, OH

Re: processing output from 'ls'

Postposted on Sun Feb 08, 2009 3:05 pm

flip-mode
Gerbil Khan
Silver subscriber
 
 
Posts: 9084
Joined: Thu May 08, 2003 12:42 pm
Location: Cincinnati, OH

Re: processing output from 'ls'

Postposted on 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.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37705
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: processing output from 'ls'

Postposted on 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.
flip-mode
Gerbil Khan
Silver subscriber
 
 
Posts: 9084
Joined: Thu May 08, 2003 12:42 pm
Location: Cincinnati, OH

Re: processing output from 'ls'

Postposted on Sun Feb 08, 2009 4:19 pm

Anything piped to bash gets executed as if it was commands typed in a terminal window.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37705
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: processing output from 'ls'

Postposted on Sun Feb 08, 2009 8:44 pm

Awk is your friend too...

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

--SS
SecretSquirrel
Gerbil Jedi
Gold subscriber
 
 
Posts: 1715
Joined: Tue Jan 01, 2002 7:00 pm
Location: The Colony, TX (Dallas suburb)

Re: processing output from 'ls'

Postposted on 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.
flip-mode
Gerbil Khan
Silver subscriber
 
 
Posts: 9084
Joined: Thu May 08, 2003 12:42 pm
Location: Cincinnati, OH

Re: processing output from 'ls'

Postposted on 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.
notfred
Grand Gerbil Poohbah
 
Posts: 3736
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: processing output from 'ls'

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

Re: processing output from 'ls'

Postposted on 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:
Code: Select all
import os
lst = ['<img src="' + f + '"/>' for f in os.listdir(".") if os.path.isfile(f)]
for f in lst:
    print(f)
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 4:39 pm
Location: Mountain View, CA

Re: processing output from 'ls'

Postposted on 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
SecretSquirrel
Gerbil Jedi
Gold subscriber
 
 
Posts: 1715
Joined: Tue Jan 01, 2002 7:00 pm
Location: The Colony, TX (Dallas suburb)

Re: processing output from 'ls'

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

Re: processing output from 'ls'

Postposted on 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.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37705
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: processing output from 'ls'

Postposted on 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.
...
mattsteg
Gerbil God
 
Posts: 15760
Joined: Thu Dec 27, 2001 7:00 pm
Location: Applauding the new/old variable width forums

Re: processing output from 'ls'

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


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 3 guests