Author Topic: Simple coding help  (Read 498 times)

0 Members and 1 Guest are viewing this topic.

Offline BlueStar

  • General Member
Simple coding help
« on: Friday 20 December 2019, 11:37:12 AM »
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:

Spoiler
[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?

Offline thomas

  • NO's Best Foreigner - Participant
  • General Member
  • now and forever, like CATS
Re: Simple coding help
« Reply #1 on: Friday 20 December 2019, 01:59:47 PM »
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


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


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

Offline BlueStar

  • General Member
Re: Simple coding help
« Reply #2 on: Friday 20 December 2019, 03:13:27 PM »
Aha, this is very useful, thanks!  I'm getting an error for the ($CHR(10)) or 12, saying invalid character, so I cant quite work out what it should be.

Offline Troll

  • Book Wanker
  • General Member
  • Sacramento, California
Re: Simple coding help
« Reply #3 on: Friday 20 December 2019, 03:30:00 PM »
Python was made for this kind of thing.


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)
       


Offline thomas

  • NO's Best Foreigner - Participant
  • General Member
  • now and forever, like CATS
Re: Simple coding help
« Reply #4 on: Friday 20 December 2019, 05:36:26 PM »
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.

Offline Troll

  • Book Wanker
  • General Member
  • Sacramento, California
Re: Simple coding help
« Reply #5 on: Friday 20 December 2019, 10:18:22 PM »
You can filepath.open() and filepath.close(), but with is easier.

I bet you like Perl.

Offline thomas

  • NO's Best Foreigner - Participant
  • General Member
  • now and forever, like CATS
Re: Simple coding help
« Reply #6 on: Friday 20 December 2019, 10:30:30 PM »
perl is f***ing 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.

Offline Troll

  • Book Wanker
  • General Member
  • Sacramento, California
Re: Simple coding help
« Reply #7 on: Friday 20 December 2019, 10:50:46 PM »
perl is f***ing 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 f***ing devil though.

Offline thomas

  • NO's Best Foreigner - Participant
  • General Member
  • now and forever, like CATS
Re: Simple coding help
« Reply #8 on: Friday 20 December 2019, 11:12:07 PM »
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 s*** if the code examples out there are anything to go by :lol:

Offline SEMTEX

  • General Member
Re: Simple coding help
« Reply #9 on: Saturday 21 December 2019, 04:08:40 AM »
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 f***ing programmers and they can't do s***.

Anyway, back to Help.

Offline BlueStar

  • General Member
Re: Simple coding help
« Reply #10 on: Saturday 21 December 2019, 12:09:55 PM »
Cheers, I'll have a go with that python script!

Online neesy111

  • General Member
  • Madrid, ES
Re: Simple coding help
« Reply #11 on: Monday 23 December 2019, 09:21:05 AM »
C# is my main language.  Also know VB, Java, JS and a few others.

Offline BlueStar

  • General Member
Re: Simple coding help
« Reply #12 on: Tuesday 7 January 2020, 09:23:22 AM »
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.

Offline Gorilla

  • Likes gambling
  • General Member
Re: Simple coding help
« Reply #13 on: Tuesday 7 January 2020, 02:29:50 PM »
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.

Offline BlueStar

  • General Member
Re: Simple coding help
« Reply #14 on: Tuesday 7 January 2020, 04:54:09 PM »
Aha, I'll have a look into that. Cheers

Offline Gorilla

  • Likes gambling
  • General Member
Re: Simple coding help
« Reply #15 on: Sunday 12 January 2020, 07:22:49 PM »
Aha, I'll have a look into that. Cheers
PM me if you have any problems.  It's probably a 5 minute job to write max.

Online neesy111

  • General Member
  • Madrid, ES
Re: Simple coding help
« Reply #16 on: Sunday 12 January 2020, 08:17:41 PM »
Yeah, I can help as well if needed.