topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday March 18, 2024, 10:46 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: SOLVED: Insert text string to front / end of each line in a text file  (Read 25836 times)

flowowery

  • Participant
  • Joined in 2012
  • *
  • Posts: 4
    • View Profile
    • Donate to Member
Hello all.

I have a request for a small program or script to insert text to the front and end of each line within a next .txt file. Or any ideas if there's a program already for this, I'm not having any lucky finding one.

Say I have a bunch of lines of text eg...
ONE
TWO
THREE



Then I want to append text to either side of each line of text. So I want "BEFORE" to go in front, and "AFTER" at the end. Eg
BEFORE ONE AFTER
BEFORE TWO AFTER
BEFORE THREE AFTER


Here's an example of how I want to use it with image tags:
So each line has a new URL (but they don't always start the http://), something like this:
http.://img.01.jpg
http.://img.02.jpg


Gets tags on either side like this:
<img src="http.://img.01.jpg">
<img src="http.://img.02.jpg">


4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
I would think that any decent text editor that allowed RegEx search/replace could handle that.

eg. Notepad2-Mod

2012-07-22_23-09-32.jpg

^(.+)$ - Search for and put everything between the start, (^), and end, ($), of a line into a group.
<img src="\1"> - Replace with <img src=", followed by the original contents of the line \1, followed by ">.

Seemed to work fine here.
« Last Edit: July 22, 2012, 08:10 AM by 4wd »

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
+1 for 4wd there.

That is the fastest and easiest way to do it. Nothing else will be significantly easier. (Also, regex is uber-cool and will give you god-like power. And all the cool kids are using it. You do want to be cool, right? -- Sorry -- I get all freaky weird around regex. They just drive me wild 'cause they're so awesome!) ;D
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Just a note that my Clipboard Help+Spell is very well suited for performing operations like this on blocks of text that you select in any program.

If you have a bunch of text files to perform this on, then look elsewhere, but if you occasionally need to perform similar operations on a block of text, you can use CHS and even set up formatting presets to allow you to trigger it with a hotkey:

Screenshot - 7_22_2012 , 10_15_39 AM.png

flowowery

  • Participant
  • Joined in 2012
  • *
  • Posts: 4
    • View Profile
    • Donate to Member
Thanks all.

Thanks for the REGEXP code 4wd. I will play around with it. I was going to see if I could combine it into a .vbs. Think it might be easier if I save all uploaded links sorted to different files already and I can just run on selected files in my file manager. Did find another method of doing this with vbs, but has to find specific phrase (http) which could change and not just new line..


Mouser, your program looks really good, I think it might do what I need once I get it set up and has a hotkey to automate it. Still need to look at how all the options work and what they do. Will look at it better when when I'm less blurry eyed.  


One thing I am wondering, I wanted to complicate it a bit more, I want to do something different for each second line! Can either of these ways skip lines or do something different on each line, please tell me if either can?  

eg I have 2 links for each picture- 1st is large, second is small version. And I want this to happen.


HTTP.://IMG.1.LARGE.jpg
HTTP.://IMG.1.SMALL.jpg
HTTP.://IMG.2.LARGE.jpg
HTTP.://IMG.2.SMALL.jpg

<a href="HTTP.://IMG.1.LARGE.jpg" target="_blank">
<img src="HTTP.://IMG.1.SMALL.jpg"/></a>
<a href="HTTP.://IMG.2.LARGE.jpg" target="_blank">
<img src="HTTP.://IMG.2.SMALL.jpg"/></a>

Then I can just join each second line with the line before it. I found a vbs script to that.
 

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
One thing I am wondering, I wanted to complicate it a bit more, I want to do something different for each second line! Can either of these ways skip lines or do something different on each line, please tell me if either can? 

eg I have 2 links for each picture- 1st is large, second is small version. And I want this to happen.


HTTP.://IMG.1.LARGE.jpg
HTTP.://IMG.1.SMALL.jpg
HTTP.://IMG.2.LARGE.jpg
HTTP.://IMG.2.SMALL.jpg

<a href="HTTP.://IMG.1.LARGE.jpg" target="_blank">
<img src="HTTP.://IMG.1.SMALL.jpg"/></a>
<a href="HTTP.://IMG.2.LARGE.jpg" target="_blank">
<img src="HTTP.://IMG.2.SMALL.jpg"/></a>

Then I can just join each second line with the line before it. I found a vbs script to that.
 

Assuming that the thumbnail doesn't have the same name as the full size, (which logically it won't), just run two RegEx search/replace operations:

Code: Text [Select]
  1. Search: ^(.+LARGE\.jpg)$
  2. Replace: <a href="\1" target="_blank">

followed by:

Code: Text [Select]
  1. Search: ^(.+SMALL\.jpg)$
  2. Replace: <img src="\1"/></a>

NOTE: I'm only doing this in Notepad2-Mod, which only allows single line RegEx - other editors may allow multi-line RegEx in which case it could probably all be done in one RegEx operation, including the joining of the lines.

If the thumbnails were all the same size then conceivably you wouldn't need the ...SMALL... lines in the file at all, you just specify the height/width attributes of the img tag.

eg.

Code: Text [Select]
  1. Search: ^(.+LARGE\.jpg)$
  2. Replace: <a href="\1" target="_blank"><img src="\1" height="42" width="42"/></a>

BAD: It means all the large images get loaded at once.
GOOD: Faster display when you click on a thumbnail since the images are already loaded.

BTW, you need to put URL stuff into a CODE box sometimes, the forum software sees 2012-07-23_11-55-31.jpg and thinks it's a link and replaces it with [/URL].

flowowery

  • Participant
  • Joined in 2012
  • *
  • Posts: 4
    • View Profile
    • Donate to Member
Ok cool I get it, thanks for the code examples 4wd. I will try and keep all my uploads uniquely named enough so that they keep the "small" and "large" label- my quick tests now is keeping them. Will hunt for editors that do search replace by line if my labeling gets messed up during uploads. But sounds good if I can find an editor that will do the lot in one.

Was actually thinking might not be able to skip lines easily and thought of resorting to a macro, which would prob be messy where I cut the first line in notepad and paste into first line in a template scroll down a line, and repeat haha.


Oh yes the height and width, I noticed that when was looking for html tags but was curious about the resized smaller ones still loading full MB. That might be the easiest way to solve this, and means only one upload. Just need to work out how many "MB" loading in one page would be too much for the average users connection and then weigh it up. Thanks for the example of how to repeat it in one tag.


Thanks again for your help. Excited about these ideas now and going to do some experimenting to find the cruisiest method.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
I will try and keep all my uploads uniquely named enough so that they keep the "small" and "large" label- my quick tests now is keeping them.

It's not necessary to have them labelled "small"/"large", as long as there's a way to differentiate between the two is all that's required.

eg. All the images are in one folder and all the thumbnails are in another but the thumbnails have the same name as the full size images.

HTTP.://IMG.1.LARGE.jpg
HTTP.://thumbnail/IMG.1.LARGE.jpg
HTTP.://IMG.2.LARGE.jpg
HTTP.://thumbnail/IMG.2.LARGE.jpg

Code: Text [Select]
  1. Search: ^(.+//IMG.+)$
  2. Replace: <a href="\1" target="_blank">

followed by:

Code: Text [Select]
  1. Search: ^(.+thumbnail.+)$
  2. Replace: <img src="\1"/></a>

Addendum: Here's an online multi-line RegEx text editor.

And if you set it up like so:

2012-07-23_16-04-29.jpgSOLVED: Insert text string to front / end of each line in a text file

Then click the RegExReplace button, (*), it will do what you want all in one hit :)

2012-07-23_16-04-14.jpgSOLVED: Insert text string to front / end of each line in a text file

NOTE: They use $ instead of \ for specifying backreferences, eg. $1 i.l.o. \1
« Last Edit: July 23, 2012, 01:11 AM by 4wd »

flowowery

  • Participant
  • Joined in 2012
  • *
  • Posts: 4
    • View Profile
    • Donate to Member
Hi, 4wd you are a great help and really good at that regexp, i made a lot of commands with it in my filemanager, but takes me a while to do them, and realised now if i haven't done it for a little while i forget everything..
That could be a neater way of keeping all the images also. Just thinking if label like that, I could just grab one link set and for each link repeat it twice for each tag and then add the extra bit for the thumbnail tag somehow, if I find regexp can do so.

Oh Just realized the images weren't loading, but got them loaded with IE. And cool. I see the new line part which I was messing with for something else while experimenting, but I didn't realize could use regexp to go through two lines and put together like that. I wouldn't have thought to see if I could do anything like that. I should be able to save a little bookmarklet form fill for this, making it even more breezy to get through.

Now I just need to get my images organized & really test this all out. Yay for no more copy pasting one by one! Thanks so so much. :D

JimboNWUK

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Hi I just came across this after coming back for a license key...

I wrote a small standalone app to do stuff like this precisely because I hate regexp and having to keep repeatedly experiment wit it to get it to do what I want. It's called Textreme and you can download it from my website at www.jollybean.co.uk

Let me know if you find it useful!

Regards,

Jim.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #10 on: October 16, 2012, 08:15 AM »
Welcome to the site Jim  :up:

Suggestion: Think about some screenshots to your web pages.

Suggestion #2: Check out this thread on our forum, about our NANY event; it might be something you would like to participate in.

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #11 on: October 16, 2012, 08:47 AM »
Welcome to the site Jim  :up:

Suggestion: Think about some screenshots to your web pages.

Suggestion #2: Check out this thread on our forum, about our NANY event; it might be something you would like to participate in.

+1 for screenshots. I visited the page earlier, and was wondering a bit what it was about.

One approach to screenshots is to "tell a story", but that takes planning, and isn't really applicable to everything. It's rarely used, except in tutorials, but is extremely effective.

Oh, and Screenshot Captor is an excellent program for going just that! (I'll plug it for mouser as he's far too humble most of the time. :) And I've been using it for about 7 years or so, and it's 1 of the first (very few) programs I put on any new computer install.)  ;)
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

JimboNWUK

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #12 on: October 16, 2012, 12:56 PM »
Thanks for the feedback guys, I don't know why it never occurred to me to add a few screenshots, especially as they are all essentially single-screen apps. I guess I thought people would be more in need of the hardware photos. Ironically it was Screenshot Captor that I came to get the license key for!

I'm not a natural website coder, preferring to use a tool like Serif WebPlus than sully my hands with HTML so it will take me a couple of days to make the changes.

I like the idea of the NANY event but as my stuff is all "fait accomplis" rather than "work in progress" I guess it doesn't really qualify.

I'll repost when the screenshots are in, perhaps there's a more appropriate thread for me to highlight the site? If not I'll repost here.

Regards,

Jim.

JimboNWUK

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #13 on: October 17, 2012, 01:45 PM »
OK I have fought my way through the help files of Serif WebPlus X4 and added some screenshots (to be honest, the Serif help files were actually pretty good, otherwise I would not be posting this update!) so whenever you folks are ready, I would like some feedback if you can be bothered!  :o)    (you can email me from the site itself if you like as I have an email filter for mails from my site)

DeVamp

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 122
  • Let the coding begin :-)
    • View Profile
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #14 on: October 25, 2012, 06:16 AM »
Hi I just came across this after coming back for a license key...

I wrote a small standalone app to do stuff like this precisely because I hate regexp and having to keep repeatedly experiment wit it to get it to do what I want. It's called Textreme and you can download it from my website at www.jollybean.co.uk

Let me know if you find it useful!

Regards,

Jim.

This is a noce tool. Will be added to my collection of tools.

Thank you very much :-)

JimboNWUK

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #15 on: October 25, 2012, 11:58 AM »
OK folks, as a result of flowowery's requests from up above, I have released a version 2.0 hot on the heels of version 1.2 that does exactly what he was asking: it lets you (a) begin the processing at a line of your choice (b) skip however many lines you like incrementally and (c) insert line numbers zero-padded to whatever length you want.

I had to move the embedded spaces stripping to the function list but the whole blank line exclusion is still on its own in "General".

I am expecting the next request to be "can we have a user-defined separator after the line number" (because it is what I thought of as I was implementing it!) but for now I will let it be (I do actually use this myself to process logfiles, errorlogs, audit trail files etc. -- which was what prompted me to write it -- so I want to give it some 'play testing' myself!)

The way I have structured the code makes it pretty easy to implement extra functions, so if you think of a good 'un please let me know either via this forum or click "Contact Jollybean Webmaster" on the page. BTW the screenshot, help file, and zip download are all up to date with rel. 2.0 AFAIK but if you find otherwise let me know ASAP.

Best regards to all,

Jim.
« Last Edit: October 28, 2012, 01:18 PM by JimboNWUK, Reason: misspelt website reference »

JimboNWUK

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: SOLVED: Insert text string to front / end of each line in a text file
« Reply #16 on: October 28, 2012, 05:04 PM »

Additional update: increased to release 2.1 to add a "Replace[]With:" setting to the ini file. I hit this problem not long after the last update so the fix has had to go in the quickest way I could think of. Because the statement parser relies upon the presence of square brackets to find the parameters within the statement this causes problems if one of your parameters is, or contains, a square bracket. This setting allows you to choose 2 other characters with which to replace them (and is set to the 'curly brackets' by default). You can then use the curly brackets (or your chosen replacement) wherever you originally intended to use the square brackets. This replacement will take place before any other operation on the text. If your text contains square brackets that do not form part of your parameters, and that you do not want changed, simply set the replacements to "[]".

Regards,

Jim.