开发者

Process list of files to format filenames for web (easy)

开发者 https://www.devze.com 2023-03-02 14:23 出处:网络
I realize this is dead simple but I don\'t write many scripts so risking stupid pride I\'m hoping to learn a thing or two by asking such a basic question.

I realize this is dead simple but I don't write many scripts so risking stupid pride I'm hoping to learn a thing or two by asking such a basic question.

Given UNIX (Mac) how might you approach turning list (.txt) of filenames:

P4243419.JPG
P4243420.JPG
P4243423.JPG
...continues...

into .html something like:

<img src="http://imgs.domain.com/event/P4243419.JPG" title="Image File P4243419.JPG" />
<img开发者_开发问答 src="http://imgs.domain.com/event/P4243420.JPG" title="Image File P4243420.JPG" />
<img src="http://imgs.domain.com/event/P4243423.JPG" title="Image File P4243423.JPG" />
...continues...

I know Ruby...but I would value additional language examples for such a simple task. What I'm not sure of is how to parameterize each line of the txt file (or filename in a directory) into the input for processing. The output is simple enough.


puts Dir['*.JPG'].map{ |f| "<img src='#{f}' title='Image File #{f}' />" }

Edit: Sorry, I misread. So you have a file with a bunch of filenames in it?

IO.read('myfile.txt').scan(/\S+/).map{ |f| "...#{f}..." }


perl -lnwe 'print "<img src=\"http://host/$_\">"' filelist.txt


In Bash:

Using a directory list:

for a in `ls *.JPG`; do echo "<img src=\"http://imgs.domain.com/event/$a\" title=\"Image File $a\" />"; done

From a file (file called list):

cat list | while read a;  do echo "<img src=\"http://imgs.domain.com/event/$a\" title=\"Image File $a\" />"; done


This will be not the most effective solution, but easy to understand: Make a file img2link.sh

for file
do
    cat "$file" | grep -i jpg | while read image
    do
        echo "<img src=\"http://imgs.domain.com/event/$image\" title=\"Image File $image\" />"
    done
done

you can use your new command:

sh img2link.sh filename_with_images.txt another_filename_with_images.txt

The grep ensure than you will not process empty lines in the given files.


for bash scripting, I see a couple of answers with cat -- not needed

format_string="<img src='http://imgs.domain.com/event/%s' title='Image File %s' />\n"
while read f; do
    printf "$format_string" "$f" "$f"
done < filename.txt
0

精彩评论

暂无评论...
验证码 换一张
取 消