Converting a list of words into a comma-separated row

Hi guys, I hope you are well and can help.

 I have a text file (Original.txt) that I wish to manipulate.


 ============================================ Original.txt
 Blurred Vision
 Glare
 Distortion
 veil
 glasses
this line has multiple words
Any number of words on one line

 -------------------
 So, the format is pretty much, that it has a list of words, with varying number of words


 -----------------------------------
 The result I am seeking is as seen below,in Processed.txt

 ============================================ Processed.txt
 Blurred Vision; Glare; Distortion; veil; glasses; this line has multiple words; Any number of words on one line;

 ------------------------------------------------

 Basically, Im trying to convert multiple lines in the original document (in original.txt), to one line (in Processed.txt), with all words that "belong" to the same line in original.txt, ended by a semicolon.


 I hope this makes sense.

 Any help greatly appreciated.
LVL 1
Simon336697Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
How about this for a batch file:

To screen:

@echo off
for /f "tokens=*" %%a in ('type original.txt') do (
  set /p =%%~a; <NUL
)

Open in new window


To file output.txt

@echo off
(for /f "tokens=*" %%a in ('type original.txt') do (
  set /p =%%~a; <NUL
)
echo.)> output.txt

Open in new window


[Edited to add carriage return to last line]
Bill PrewIT / Software Engineering ConsultantCommented:
If VBscript is acceptable, here's a small example of that.  Save as a VBS, and run passing the input and output file names on the command line like this:

cscript EE28681200.vbs in.txt out.txt

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input and output filenames from command line
strInFile = WScript.Arguments(0)
strOutFile = WScript.Arguments(1)

' Read entire file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

' Concatenate all lines into one
strData = Replace(strData, vbCrLf, "; ")

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write strData
objFile.Close

Open in new window

~bp
Paul TomasiCommented:
No need for a VB bill... A simple batch file will do the job. See below.

Steve almost got it right except for when handling the last line: (1) inclusion of space at end of line, (2) no carriage return

NOTE: The semi-colon at the end of the line is optional and can easily be omitted if not required. It's up to the author to decide.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=2 delims=:" %%a in ('find /c /v "" original.txt') do set /a line=%%a

for /f "tokens=*" %%a in (original.txt) do (
  set /a line-=1
  
  if !line! gtr 0 (
    set /p =%%~a; <nul
  ) else (
    echo %%~a;
  )
)>>processed.txt

Open in new window

OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Steve KnightIT ConsultancyCommented:
Paul, long time no see around here.... and yes your invention of set /p useful in this case.  I thought an additional ; at the end may or not be an issue which is why I went for the simple effectively one liner... in fact from a command line:

(@for /f "tokens=*" %a in ('type original.txt') do @set /p =%~a; <NUL)> output.txt

Steve
Paul TomasiCommented:
Steve

Yeah, long time no see... Nice to see you're still here as well as peeps like ~bp.

I agree a single one-line one-off command is what WE as Gurus would use followed by manually fiddling with PROCESSED.TXT to remove the trailing [space] character from the end of the line AND THEN force a carriage return/newline (although CR/NL is only an assumption here).
Bill PrewIT / Software Engineering ConsultantCommented:
Hello Paul, nice to see you joining in again.  So when do I get my HANGMAN points?  :)

When I saw the BAT solutions were in motion, I decided to include a VBscript solution as well.  Need is always a tough thing to judge, but I figure EE is as much about teaching as solving, so I like to include some additional options in case people are looking for a platform they didn't make clear originally.  VBS is often easier for posters to understand than the powerful but sometimes cryptic BAT or PS1 scripts.

Too bad DOS never included a native "search and replace" tool, would come in handy for things like this.

~bp
Paul TomasiCommented:
Bill, just looking at your code...

For some reason, the output inconsistantly contains a mix of ";[][]" and ";[]" (where "[]" is a space character) - or is it just my Windows 8.1 playing up?

Also, like Steve's code, PROCESSED.TXT contains a trailing [space] character AND no carriage return/linefeed.

Just thought you might like to know...
Paul TomasiCommented:
Oops! Bill, our posts crossed... lol

Will look at the hagman thing again now that you've prompted me...
Paul TomasiCommented:
Sorry people if I appear to be picky about minor details... as a perfectionist, I just like the challange of getting it perfect in DOS batch code.
Bill PrewIT / Software Engineering ConsultantCommented:
Here's a slight change to (1) remove trailing space character from new line, and (2) add a <CR><LF> to the end of the output file.

I added the trailing semi-color at the end of the line since that is what the OP asked for, and was included in the sample desired output.  Easily modified if that wasn't accurate.

The extra spaces you are seeing must be do to extra spaces at the beginning or end of the lines of the input text file.  No mention was made to needing to "clean up" the input lines so I take those as is.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input and output filenames from command line
strInFile = WScript.Arguments(0)
strOutFile = WScript.Arguments(1)

' Read entire file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

' Concatenate all lines into one
'If Right(strData, 2) = vbCrLf Then strData = Left(strData, Len(strData) - 2)
strData = Trim(Replace(strData, vbCrLf, "; "))

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.WriteLine strData
objFile.Close

Open in new window

~bp
Paul TomasiCommented:
Hello Bill

I have just realised what the 'extra' [space] characters are in your output...

Looking more closely at ORIGINAL.TXT, lines 2, 3, 4 and 5 are indented by a single [space] character.

Hope that's shed some light on it for you.
:)
Bill PrewIT / Software Engineering ConsultantCommented:
Right, that's exactly what I said.

~bp
Paul TomasiCommented:
Simon336697

Please take a look at my code below if you haven't already done so.
It's essentially the same code as posted in my previous comment (40802872).

Thank you.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=2 delims=:" %%a in ('find /c /v "" original.txt') do set /a count=%%a

for /f "tokens=*" %%a in (original.txt) do (
  set /a count-=1
  if !count! gtr 0 (set /p =%%a; <nul) else (echo %%a;)
)>>processed.txt

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Simon336697Author Commented:
Guys so sorry about the delay.
Simon336697Author Commented:
Guys you have all done an amazing job thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.