Jump to content

Simple coding help


BlueStar
 Share

Recommended Posts

So I'm trying to find a really quick way of doing some basic text replacement, without having to open Word or Excel.  Basically I need to add a blank line between each row and a symbol and space to the start of each row.  So input:

 

Lorem ipsum dolor sit amet

consectetur adipiscing elit

sed do eiusmod tempor incididunt

 

Output

 

$ Lorem ipsum dolor sit amet

 

$ consectetur adipiscing elit

 

$ sed do eiusmod tempor incididunt

 

I've managed to make a batch file that points to a vbs file and can replace one word with another.

 

VBS code:

 

 

Const ForReading = 1   

Const ForWriting = 2

 

strFileName = Wscript.Arguments(0)

strOldText = Wscript.Arguments(1)

strNewText = Wscript.Arguments(2)

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

strText = objFile.ReadAll

objFile.Close

 

strNewText = Replace(strText, strOldText, strNewText)

Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)

objFile.Write strNewText  'WriteLine adds extra CR/LF

objFile.Close

 

 

Batch code:

 

cscript replace.vbs "F:\test.txt" "Lorem" "Dorem"

 

So if I save the input text to test.txt, it'll replace Lorem with Dorem and that works.  But what I need to do is replace "(carriage return)" with "(carriage return)(carriage return)$(space)" and I can't seem to find a syntax to do that.  Is it even possible with this method, or am I barking up the wrong tree and need to look at java or python or something?

Link to post
Share on other sites

probably instead of objFile.ReadAll you need to do something like ReadLine - https://stackoverflow.com/questions/15533214/how-do-i-read-a-file-line-by-line-in-vb-script

 

[pre]

filename = "C:\Temp\vblist.txt"

outfilename = "C:\Temp\vbOut.txt"

 

Set fso = CreateObject("Scripting.FileSystemObject")

Set fs2 = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile(filename)

set outFile = fso.OpenTextFile(outputfilename)

 

Do Until f.AtEndOfStream ' read until end of file of input file

  currentLine = f.ReadLine ' grab single line

  ' do your string replacement here ????

  currentLine = "$ " + currentLine ' prepend the $

  outFile.Write(currentLine) ' write it out

  outFile.Write($CHR(10)) ' or CHR(12) or however you manually output a carriage return/line feed.

  ' and then loop

Loop

[/pre]

 

apologies if apostrophes aren't still comments in VBS, I'm going off my old VB days.

Link to post
Share on other sites

Python was made for this kind of thing.

 

[pre]

filepath = "lorem.txt"

newfile = "lorem_new.txt"

prefix = "$ "

 

with open(filepath) as fp:

    lines = fp.read().splitlines()

 

with open(filepath, "w") as fp:

    for line in lines:

        print(prefix + line + "\n", file=fp)

        [/pre]

 

Link to post
Share on other sites

Look at this noob depending on the language to close his file handles. Booo. Boo I say. Down with python. I don’t respect a language that respects white space.

 

Edit: I am actually, IRL, vehemently anti python.

Link to post
Share on other sites

perl is fucking gorgeous tbh.:lol: made my first monies professionally programming in it.

 

but these days I'm node/react/angular and c# and life is dumb+good.

 

I like C#, but nothing beats Python for DNA analysis, which is essentially working with enormous text files. 

 

Hope we can agree that R is the fucking devil though.

Link to post
Share on other sites

All I know of R is microsoft early signaling that it was going to be their "big data" standard a few years ago when they purchased some big R firm. Thankfully never had to do anything with it. looks like shit if the code examples out there are anything to go by :lol:

Link to post
Share on other sites

We have bio-statisticians at work that repeatedly INSIST something is wrong with the server they're working on when it freezes up. Without a shadow of a doubt, the issue is with their code in R/SAS. Every single god damn time.

 

Also why are biostatisticians the least computer savvy people in the world? They're computer fucking programmers and they can't do shit.

 

Anyway, back to Help.

Link to post
Share on other sites

  • 3 weeks later...

Been looking at this again after Christmas and come to the conclusion I'm a bit out of my depth. :lol: Made a few small changes (swapped the $ symbol for a #, changed the txt files to input.txt and output.txt), downloaded a portable version of python and I'm running the script with a command Python\App\Python script.py, which doesn't give an error but nothing seems to happen.  Was kinda hoping there was an easy way to turn a script into a windows executable but that seems a rather complex task.  Have looked at setting up a a macro in Word or Excel, but I'd like other people to be able to use it and seeing as in the 90s people spread viruses and worms with office macros it's pretty impossible to share them easily.

Link to post
Share on other sites

Been looking at this again after Christmas and come to the conclusion I'm a bit out of my depth. :lol: Made a few small changes (swapped the $ symbol for a #, changed the txt files to input.txt and output.txt), downloaded a portable version of python and I'm running the script with a command Python\App\Python script.py, which doesn't give an error but nothing seems to happen.  Was kinda hoping there was an easy way to turn a script into a windows executable but that seems a rather complex task.  Have looked at setting up a a macro in Word or Excel, but I'd like other people to be able to use it and seeing as in the 90s people spread viruses and worms with office macros it's pretty impossible to share them easily.

Easiest way is to do it in c# and make it a console app.  Visual Studio Community Edition is free.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...