LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to search and replace in text file - kinda... (https://www.linuxquestions.org/questions/programming-9/script-to-search-and-replace-in-text-file-kinda-249954/)

jeffreybluml 11-01-2004 03:51 PM

Script to search and replace in text file - kinda...
 
I'm trying (desperately) to find a "pre-canned" script that will take an html file and replace each reference to a .jpg file with the name of a .jpg file within the directory we are running the script (same one that contains the html file).

To be more clear, I have a monthly webpage with pictures of my newborn. The page has 24 jpg thumbnails which link to the large version of their respective picture. All 48 pictures, as well as the html file, are all in the same, unique directory. All of the thumbnails filenames end with .SMALL.THUMB.jpg and all of the larger size have the same name, but end only in .SMALL.jpg

So, is there some way to have a script just change all the old jpg references to match the current files in the directory, and do them in a way as to ensure that the thumbnails point to the correct picture? The thumb and the respective larger pic both have the same name, of course, with the addition of .THUMB on the thumbnail...

Oh, and I apologize, but I have no experience writing scripts. I'm running Mandrake 10, if that makes any difference. If you need any other info, please ask. I'll get back to you immediately.

Please help!

Thanks to you all!!!!!

Thank goodness for L.Q......

secesh 11-01-2004 03:53 PM

using your text editor, can't you just do a find/replace?

jeffreybluml 11-01-2004 04:04 PM

yeah, but seeing as I have a newborn, I'm using the excuse that I'm really busy and it takes too long to do by hand....

Perhaps it's just laziness, or a yearning for proficiency, but I'm getting tired of copy/pasitng the old names over the new. They're all different names (for each pair of thumbnail/larger image), so search and replace really only adds the step of typing in the OLD filename, whereas right now I just pipe "ls -l" in the directory to a text file and copy the filenames of the new images from there to the html file. It gets old.....

Please help....

KneeLess 11-01-2004 04:06 PM

perl -ep 's/onething/foranother; print;' << file

jeffreybluml 11-01-2004 04:09 PM

Don't I need to somehow have the script create a variable, read the names of the files in the current directory, and assign them sequentially to the variable as it changes each file name?

THe above looks to me as though I have to type in the old filename, then the / and then the new filename, which isn't really what I'm looking for...

Thanks so far though...

Tinkster 11-01-2004 04:30 PM

Code:

for file in `ls -1`; do sed -i 's/small/SMALL/g' $file ; done
If your sed is >= 4.x :)


If it's an older version
Code:

for file in `ls -1`; do sed 's/small/SMALL/g' $file > tmp; mv tmp $file; done

Cheers,
Tink

jeffreybluml 11-01-2004 04:46 PM

That's what I'm taling about!!!

Thanks Tink!

I haven't done it yet, as I have one question. Is there somewhere in that line that I need to name the file that's it's editing? For instance, nine_months.html? Or is it just going to edit the only text file in the working directory?

itsme86 11-01-2004 04:48 PM

*points at $file in Tink's example*

That's where you put your filename.

Tinkster 11-01-2004 04:51 PM

In my script it would modify every file it comes across...

to change that you need to modify the ls-statement in

for file in `ls -1`;

to

e.g.

for file in `ls -1 *months*html`;

HIH


Cheers,
Tink

jeffreybluml 11-01-2004 05:01 PM

I'm sorry, I'm usually not this much trouble. THANK YOU for getting me this far...

Here's what I deduced this should look like:

for file in `ls -1 *months*html`; do sed -i 's/small/SMALL/g' $file ; done

And nothing seems to have changed. Just for clarification, here's the contents of the current directory:

[jeff@localhost 10_months]$ ls -l
total 2988
-rw-r--r-- 1 jeff jeff 45849 Nov 1 16:53 abby10.SMALL.jpg
-rw-r--r-- 1 jeff jeff 13358 Nov 1 16:53 abby10.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 65804 Nov 1 16:53 abby_apple_pickin.SMALL.jpg
-rw-r--r-- 1 jeff jeff 15066 Nov 1 16:53 abby_apple_pickin.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 94106 Nov 1 16:53 abby_n_dad_10mos.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16064 Nov 1 16:53 abby_n_dad_10mos.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 55666 Nov 1 16:53 abby_n_dada2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 13536 Nov 1 16:53 abby_n_dada2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 79241 Nov 1 16:53 abby-n_dada_kisses.SMALL.jpg
-rw-r--r-- 1 jeff jeff 14668 Nov 1 16:53 abby-n_dada_kisses.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 92416 Nov 1 16:53 abby_n_dadas_littlefalls2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 7669 Nov 1 16:53 abby_n_dadas_littlefalls2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 164227 Nov 1 16:53 abby_n_mama_10mos.SMALL.jpg
-rw-r--r-- 1 jeff jeff 20741 Nov 1 16:53 abby_n_mama_10mos.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 135318 Nov 1 16:53 abby_n_mama_littlefalls.SMALL.jpg
-rw-r--r-- 1 jeff jeff 21619 Nov 1 16:53 abby_n_mama_littlefalls.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 38743 Nov 1 16:53 abigail.SMALL.jpg
-rw-r--r-- 1 jeff jeff 10243 Nov 1 16:53 abigail.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 91492 Nov 1 16:53 asleep_in_dadas_hat2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16479 Nov 1 16:53 asleep_in_dadas_hat2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 44076 Nov 1 16:53 blocks.SMALL.jpg
-rw-r--r-- 1 jeff jeff 11999 Nov 1 16:53 blocks.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 74100 Nov 1 16:53 bunny_ears.SMALL.jpg
-rw-r--r-- 1 jeff jeff 7473 Nov 1 16:53 bunny_ears.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 108307 Nov 1 16:53 chillin.SMALL.jpg
-rw-r--r-- 1 jeff jeff 19024 Nov 1 16:53 chillin.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 78831 Nov 1 16:53 dadas_hat_closeup.SMALL.jpg
-rw-r--r-- 1 jeff jeff 15706 Nov 1 16:53 dadas_hat_closeup.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 53549 Nov 1 16:53 elmo.SMALL.jpg
-rw-r--r-- 1 jeff jeff 13074 Nov 1 16:53 elmo.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 73895 Nov 1 16:53 happy_girl.SMALL.jpg
-rw-r--r-- 1 jeff jeff 7247 Nov 1 16:53 happy_girl.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 148869 Nov 1 16:53 hugs.SMALL.jpg
-rw-r--r-- 1 jeff jeff 18988 Nov 1 16:53 hugs.SMALL.THUMB.jpg
-rwxr--r-- 1 jeff jeff 2512 Nov 1 16:53 image001.gif*
-rw-r--r-- 1 jeff jeff 96960 Nov 1 16:53 my_room.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16014 Nov 1 16:53 my_room.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 37287 Nov 1 16:53 oct_rose_10months.SMALL.jpg
-rw-r--r-- 1 jeff jeff 14245 Nov 1 16:53 oct_rose_10months.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 68315 Nov 1 16:53 on_a_walk2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 15421 Nov 1 16:53 on_a_walk2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 52700 Nov 1 16:53 on_a_walk.SMALL.jpg
-rw-r--r-- 1 jeff jeff 19100 Nov 1 16:53 on_a_walk.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 82408 Nov 1 16:53 raspberries.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16770 Nov 1 16:53 raspberries.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 144473 Nov 1 16:53 sittin.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16448 Nov 1 16:53 sittin.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 94992 Nov 1 16:53 so_many_teeth.SMALL.jpg
-rw-r--r-- 1 jeff jeff 20888 Nov 1 16:53 so_many_teeth.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 132270 Nov 1 16:53 sunflowers.SMALL.jpg
-rw-r--r-- 1 jeff jeff 21314 Nov 1 16:53 sunflowers.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 4239 Nov 1 16:54 ten_months.html
-rwxr--r-- 1 jeff jeff 290817 Nov 1 16:53 Thumbs.db*


So again, I'm trying to replace the names of the jpeg references in the ten_months.html (which are names not seen in this directory) with the names you see above.

Nothing seems to have changed after I did the code above...

Any ideas what I'm doing wrong?

Thanks again

matthurne 11-01-2004 05:07 PM

What does that -i do exactly?

Tinkster 11-01-2004 05:57 PM

Quote:

Originally posted by jeffreybluml
I'm sorry, I'm usually not this much trouble. THANK YOU for getting me this far...

Here's what I deduced this should look like:

for file in `ls -1 *months*html`; do sed -i 's/small/SMALL/g' $file ; done

And nothing seems to have changed. Just for clarification, here's the contents of the current directory:

[jeff@localhost 10_months]$ ls -l
total 2988
-rw-r--r-- 1 jeff jeff 45849 Nov 1 16:53 abby10.SMALL.jpg
-rw-r--r-- 1 jeff jeff 13358 Nov 1 16:53 abby10.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 65804 Nov 1 16:53 abby_apple_pickin.SMALL.jpg
-rw-r--r-- 1 jeff jeff 15066 Nov 1 16:53 abby_apple_pickin.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 94106 Nov 1 16:53 abby_n_dad_10mos.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16064 Nov 1 16:53 abby_n_dad_10mos.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 55666 Nov 1 16:53 abby_n_dada2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 13536 Nov 1 16:53 abby_n_dada2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 79241 Nov 1 16:53 abby-n_dada_kisses.SMALL.jpg
-rw-r--r-- 1 jeff jeff 14668 Nov 1 16:53 abby-n_dada_kisses.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 92416 Nov 1 16:53 abby_n_dadas_littlefalls2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 7669 Nov 1 16:53 abby_n_dadas_littlefalls2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 164227 Nov 1 16:53 abby_n_mama_10mos.SMALL.jpg
-rw-r--r-- 1 jeff jeff 20741 Nov 1 16:53 abby_n_mama_10mos.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 135318 Nov 1 16:53 abby_n_mama_littlefalls.SMALL.jpg
-rw-r--r-- 1 jeff jeff 21619 Nov 1 16:53 abby_n_mama_littlefalls.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 38743 Nov 1 16:53 abigail.SMALL.jpg
-rw-r--r-- 1 jeff jeff 10243 Nov 1 16:53 abigail.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 91492 Nov 1 16:53 asleep_in_dadas_hat2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16479 Nov 1 16:53 asleep_in_dadas_hat2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 44076 Nov 1 16:53 blocks.SMALL.jpg
-rw-r--r-- 1 jeff jeff 11999 Nov 1 16:53 blocks.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 74100 Nov 1 16:53 bunny_ears.SMALL.jpg
-rw-r--r-- 1 jeff jeff 7473 Nov 1 16:53 bunny_ears.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 108307 Nov 1 16:53 chillin.SMALL.jpg
-rw-r--r-- 1 jeff jeff 19024 Nov 1 16:53 chillin.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 78831 Nov 1 16:53 dadas_hat_closeup.SMALL.jpg
-rw-r--r-- 1 jeff jeff 15706 Nov 1 16:53 dadas_hat_closeup.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 53549 Nov 1 16:53 elmo.SMALL.jpg
-rw-r--r-- 1 jeff jeff 13074 Nov 1 16:53 elmo.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 73895 Nov 1 16:53 happy_girl.SMALL.jpg
-rw-r--r-- 1 jeff jeff 7247 Nov 1 16:53 happy_girl.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 148869 Nov 1 16:53 hugs.SMALL.jpg
-rw-r--r-- 1 jeff jeff 18988 Nov 1 16:53 hugs.SMALL.THUMB.jpg
-rwxr--r-- 1 jeff jeff 2512 Nov 1 16:53 image001.gif*
-rw-r--r-- 1 jeff jeff 96960 Nov 1 16:53 my_room.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16014 Nov 1 16:53 my_room.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 37287 Nov 1 16:53 oct_rose_10months.SMALL.jpg
-rw-r--r-- 1 jeff jeff 14245 Nov 1 16:53 oct_rose_10months.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 68315 Nov 1 16:53 on_a_walk2.SMALL.jpg
-rw-r--r-- 1 jeff jeff 15421 Nov 1 16:53 on_a_walk2.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 52700 Nov 1 16:53 on_a_walk.SMALL.jpg
-rw-r--r-- 1 jeff jeff 19100 Nov 1 16:53 on_a_walk.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 82408 Nov 1 16:53 raspberries.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16770 Nov 1 16:53 raspberries.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 144473 Nov 1 16:53 sittin.SMALL.jpg
-rw-r--r-- 1 jeff jeff 16448 Nov 1 16:53 sittin.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 94992 Nov 1 16:53 so_many_teeth.SMALL.jpg
-rw-r--r-- 1 jeff jeff 20888 Nov 1 16:53 so_many_teeth.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 132270 Nov 1 16:53 sunflowers.SMALL.jpg
-rw-r--r-- 1 jeff jeff 21314 Nov 1 16:53 sunflowers.SMALL.THUMB.jpg
-rw-r--r-- 1 jeff jeff 4239 Nov 1 16:54 ten_months.html
-rwxr--r-- 1 jeff jeff 290817 Nov 1 16:53 Thumbs.db*


So again, I'm trying to replace the names of the jpeg references in the ten_months.html (which are names not seen in this directory) with the names you see above.

Nothing seems to have changed after I did the code above...

Any ideas what I'm doing wrong?

Thanks again

I don't know what to say :)

If you edit a html file - what change (except for the obvious
in the time-stamp) do you expect to see in the directory listing?

As for my replacement string, that was also just taken from the
example in the perl command.
You obviously need to change that to whatever you're REALLY
trying to achieve (I wasn't quite sure from the original post - you
referred to large as opposed to old, and I wasn't sure what you
meant ;}).




Cheers,
Tink

Tinkster 11-01-2004 05:58 PM

Quote:

Originally posted by matthurne
What does that -i do exactly?
Read
man sed



Cheers,
Tink

jeffreybluml 11-01-2004 06:30 PM

Sorry, I'm probably a bit spolied, as I expected to be able to just "copy/paste" your example without major modifications.

The old vs large etc thing was just as follows, and in retrospect I really should have given an example of the html code I'm looking to edit with this effort...

################################html code#####################################################

<tr height="270"><td width="240"><center><a href="10_months/new_sweater.SMALL.jpg"><img src="10_months/new_sweater.SMALL.THUMB.jpg"></a></td><td
width="240"><center><a href="10_months/playin_girls.SMALL.jpg"><img src="10_months/playin_girls.SMALL.THUMB.jpg"></a></td><td width="240"><center><a href="10_months/standin_9mos.SMALL.jpg"><img src="10_months/standin_9mos.SMALL.THUMB.jpg"></a></td></tr>
<tr height="270"><td width="240"><center><a href="10_months/standin_in_crib.SMALL.jpg"><img src="10_months/standin_in_crib.SMALL.THUMB.jpg"></a></td>
<td width="240"><center><a href="10_months/standin_table.SMALL.jpg"><img src="10_months/standin_table.SMALL.THUMB.jpg"></a></td><td width="240"><center><a
href="10_months/sunflowers.SMALL.jpg"><img src="10_months/sunflowers.SMALL.THUMB.jpg"></a></td></tr>

################################end cose#######################################################

So, I need to replace every filename (old files) you see after an occurance of 10_months/ (in the code above) with the filenames of the images in the directory listing, and hopefully get them to line up so that each <a href="xxxxx.SMALL.THUMB.jpg"> point to the correct <img src="xxxxx.SMALL.jpg">. The above code repeats 4 more times, so copy/pasting this manually takes a few and it makes me procrastinate doing it, so I'm trying to speed this up. Plus, this will really help me start to teach myself this kind of stuff, as I'm still considering myself a newbie to the linux world....and lovin it....

Seriously, I appreciate your patience and help...

Jeff

Tinkster 11-01-2004 06:38 PM

But in terms of the file-names I still don't know
(or couldn't figure out, whatever) what is the
old filename, and what you want to change it to.

Or, in other words, can you give me a before/after
scenario of one line of hrf /imgsrc combos?



Cheers,
Tink


All times are GMT -5. The time now is 11:55 PM.