Scripting advice, writing output to files |
![]() ![]() |
Scripting advice, writing output to files |
Jan 31 2012, 11:47 PM
Post
#1
|
|
|
Atomican Overlord ![]() |
I'm hoping to get some advice for building a script here. My script-fu doesn't extend beyond Windows batch files, and I think what I'm trying to do is too complicated for simple DOS commands.
I have a text file with 1100 email addresses. I want to parse the file to extract one email address at a time, write that address to a specific spot in a pre-defined block of text, and then save the text to a unique file. The script then needs to loop back and go onto the next address. At the end I want to have 1100 unique text files, each with one address in it. Ideas? Note that I don't have Python installed on my computer. I do have Perl, if anyone is so inclined. Alternatively, if this can be done with a batch file that would be most excellent (only because it will Just Plain Work on my Windows computer, plus I may actually have a chance of understanding and learning from it!). Thanks in advance for any help. -------------------- The poster formerly known as Chazzozz.
* Still an enthusiastic Opera user. Try it, you'll like it: http://www.opera.com/download/ * I love torrenting MAME and Pinball emulators. One day I may even use the software. |
|
|
|
Feb 1 2012, 06:11 AM
Post
#2
|
|
|
Hero Titan ![]() |
Sounds like a job for Microsoft Word's Mail Merge :p
-------------------- http://www.raggedracing.com
http://www.robertgray.net.au @RobertGray |
|
|
|
Feb 1 2012, 08:14 AM
Post
#3
|
|
|
Atomican Overlord ![]() |
Sounds like a job for Microsoft Word's Mail Merge :p You know, I did briefly consider it, but I'm feeding the resulting text files into an SMTP stream so it's imperative they're plain-text and nothing else. Experience has taught that Word loves to sneak in the odd invisible binary character when you least expect it, and I'm not keen to hand-edit 1100 files. -------------------- The poster formerly known as Chazzozz.
* Still an enthusiastic Opera user. Try it, you'll like it: http://www.opera.com/download/ * I love torrenting MAME and Pinball emulators. One day I may even use the software. |
|
|
|
Feb 1 2012, 11:06 AM
Post
#4
|
|
|
Atomican Champion |
Powershell?
CODE $text = "This is a pre-defined block of text with an email in it for replacement" $emails = Get-Content emails.txt $filecount=0 foreach($email in $emails){ $text -replace "email",$email >> ('email_' + $filecount + '.txt') $filecount++ } That will grab the emails from a file (I tested using a file with 1 email per line) and output them into individual files with name "email_0.txt. email_1.txt" etc. Change the filenaming and text block to suit. |
|
|
|
Feb 3 2012, 11:26 PM
Post
#5
|
|
|
Atomican Overlord ![]() |
Most Excellent!!
Thanks very much, linke, that gave me exactly the starting advice I was looking for. Since I was adding the email address into the middle of a multi-line text file I had to find something else that would work with a larger block of text. I did some further investigation and it turns out the Set-Content cmdlet was what I needed to use, so I adapted your example script and came up with this: CODE # Specify the source file here $emails = Get-Content addy.txt # Specify the destination file here $body = Get-Content body.txt # This initialises the incrementation to be used in file names $filecount=0 # Here is where the addresses from the source file are used # to replace the placeholder in the destination file, then # written to a new file. foreach($email in $emails){ $body -replace "email",$email | Set-Content ('sub_' + $filecount + '.txt') $filecount++ } # The above function only works with files in the same directory. # To copy the resulting files to another directory, uncomment # the function below and specify the target directory at the # '-destination' parameter. Copy-Item -path .\*.txt -destination C:\example # To delete files from the original source directory uncomment # the function below. # # USE AT YOUR OWN RISK! Remove-Item .\*.txt As you can see, when I execute the script it only writes files back to the same directory. I tried using the -path parameter in the Set-Content cmdlet like so: CODE Set-Content -path C:\example\('sub_' + $filecount + '.txt') ...but PowerShell didn't like the incrementing file names function and threw an exception. Do you know if there's a way to specify a path and still increment the names? -------------------- The poster formerly known as Chazzozz.
* Still an enthusiastic Opera user. Try it, you'll like it: http://www.opera.com/download/ * I love torrenting MAME and Pinball emulators. One day I may even use the software. |
|
|
|
Mar 14 2012, 10:24 PM
Post
#6
|
|
|
Atomican Champion ![]() |
sc -pat $("C:\example\sub_{0}.txt" -f $filecount) |
|
|
|
Jun 1 2012, 11:11 AM
Post
#7
|
|
|
Atomican Charge ![]() |
For perl , is that what you mean?
CODE #!c:/perl/bin/perl -w # $addrCount=0; $nm=0; $addresses = "c:/myaddresses.txt"; $template = "c:/mytemplatetext.txt"; $outfilename="c:/outfileprefix"; $templtxt = ""; open(TEMPLATE,"<${template}"); @templtxt=<TEMPLATE>; $lp=scalar(@templtxt); while($nm<$lp){ $templtxt.=" ".$templtxt[$nm]; $nm+=1; } #endwhile close(TEMPLATE); # open(ADDRESS,"<${addresses}"); # @addr=<ADDRESS>; close(ADDRESS); $addrCount=scalar(@addr); $nm=0; while($nm<$addrCount){ ## # # match or substitute and cut substrings here of $templtxt to insert $addr[$nm] # reassign to $templtxt and can be used below to write to file ## $outfilename.="".$nm.".txt"; # create file open(OUTFILE,"<${outfilename}"); close(OUTFILE); # end create file # open file for writing to open(OUTFILE,">${outfilename}"); print $templtxt."\n"; close(OUTFILE); $nm+=1; } -------------------- http://sidewinder11sucommanderxer.podserver.info/SideWinderColourBalancer(EBLA4-Linux-Win-Oct-2012(Oct-fix)).zip
|
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 18th May 2013 - 05:50 PM |