Welcome Guest.   Make a donation to an author on the site April 20, 2014, 01:02:05 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Read the full one-year retrospective report on DonationCoder.com.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: SOLVED: Insert text string to front / end of each line in a text file  (Read 5672 times)
flowowery
Participant
*
Posts: 4


View Profile Give some DonationCredits to this forum member
« on: July 22, 2012, 06:13:21 AM »

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">

Logged
4wd
Supporting Member
**
Posts: 3,222



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #1 on: July 22, 2012, 08:01:45 AM »

I would think that any decent text editor that allowed RegEx search/replace could handle that.

eg. Notepad2-Mod



^(.+)$ - 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:01 AM by 4wd » Logged

Four wheel drive: Helping you get stuck faster, harder, further from help...........and it's no different on this forum Evil
Renegade
Charter Member
***
Posts: 10,361



Tell me something you don't know...

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #2 on: July 22, 2012, 08:14:40 AM »

+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!) Grin
Logged

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
*****
Posts: 32,674



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #3 on: July 22, 2012, 10:16:01 AM »

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:


Logged
flowowery
Participant
*
Posts: 4


View Profile Give some DonationCredits to this forum member
« Reply #4 on: July 22, 2012, 11:48:01 AM »

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"/>[/url]
<a href="HTTP.://IMG.2.LARGE.jpg" target="_blank">
<img src="HTTP.://IMG.2.SMALL.jpg"/>[/url]

Then I can just join each second line with the line before it. I found a vbs script to that.
 
Logged
4wd
Supporting Member
**
Posts: 3,222



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #5 on: July 22, 2012, 08:58:13 PM »

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"/>[/url]
<a href="HTTP.://IMG.2.LARGE.jpg" target="_blank">
<img src="HTTP.://IMG.2.SMALL.jpg"/>[/url]

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:

Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  1. Search: ^(.+LARGE\.jpg)$
  2. Replace: <a href="\1" target="_blank">

followed by:

Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  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.

Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  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 and thinks it's a link and replaces it with [/URL].
Logged

Four wheel drive: Helping you get stuck faster, harder, further from help...........and it's no different on this forum Evil
flowowery
Participant
*
Posts: 4


View Profile Give some DonationCredits to this forum member
« Reply #6 on: July 22, 2012, 11:50:22 PM »

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.
Logged
4wd
Supporting Member
**
Posts: 3,222



see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #7 on: July 23, 2012, 12:05:21 AM »

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

Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  1. Search: ^(.+//IMG.+)$
  2. Replace: <a href="\1" target="_blank">

followed by:

Formatted for Generic Code with the GeSHI Syntax Highlighter [copy or print]
  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:



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



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

Four wheel drive: Helping you get stuck faster, harder, further from help...........and it's no different on this forum Evil
flowowery
Participant
*
Posts: 4


View Profile Give some DonationCredits to this forum member
« Reply #8 on: July 23, 2012, 09:45:52 AM »

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. cheesy
Logged
JimboNWUK
Participant
*
Posts: 6

View Profile Give some DonationCredits to this forum member
« Reply #9 on: October 16, 2012, 06:03:44 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.
Logged
mouser
First Author
Administrator
*****
Posts: 32,674



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #10 on: October 16, 2012, 08:15:18 AM »

Welcome to the site Jim  thumbs 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.
Logged
Renegade
Charter Member
***
Posts: 10,361



Tell me something you don't know...

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #11 on: October 16, 2012, 08:47:04 AM »

Welcome to the site Jim  thumbs 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. smiley 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.)  Wink
Logged

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
*
Posts: 6

View Profile Give some DonationCredits to this forum member
« Reply #12 on: October 16, 2012, 12:56:31 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.
Logged
JimboNWUK
Participant
*
Posts: 6

View Profile Give some DonationCredits to this forum member
« Reply #13 on: October 17, 2012, 01:45:36 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!  ohmy)    (you can email me from the site itself if you like as I have an email filter for mails from my site)
Logged
DeVamp
Supporting Member
**
Posts: 90



Let the coding begin :-)

see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #14 on: October 25, 2012, 06:16:26 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 :-)
Logged
JimboNWUK
Participant
*
Posts: 6

View Profile Give some DonationCredits to this forum member
« Reply #15 on: October 25, 2012, 11:58:39 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:40 PM by JimboNWUK; Reason: misspelt website reference » Logged
JimboNWUK
Participant
*
Posts: 6

View Profile Give some DonationCredits to this forum member
« Reply #16 on: October 28, 2012, 05:04: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.
Logged
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.123s | Server load: 0.03 ]