Converting Several Lines to One Line with Punctuation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Converting Several Lines to One Line with Punctuation

#1 Post by aisha » 07 Nov 2016 08:48

Hello DOS Tips
I hope someone from the board knows how to do this, I must not be Googling it right.

We have many text files with about 50 words lines
Each line has a word or short phrase

We are trying to convert the file to have all the lines on a single line, with Boolean operators between them.

Example file name:
c:\sandbox\original.txt

Contents of original.txt are:
Jack and Jill
Went up the Hill
To Fetch
A Pail of Water
Jack fell down And broke his crown
And Jill came tumbling after

We hope to convert this file so we get the contents of each line in QUOTATION MARKS
Also, each of these should be separated by a QUOTE and then a SPACE and then the boolean OR and another SPACE
But the last line should only end in the QUOTE

so it will look like this:
"Jack and Jill" OR "Went up the Hill" OR "To Fetch" OR "A Pail of Water" OR "Jack fell down And broke his crown" OR "And Jill came tumbling after"

I hope I explained that well enough, I just cannot seem to figuce it out, but this will be a huge help

Aisha

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Converting Several Lines to One Line with Punctuation

#2 Post by Squashman » 07 Nov 2016 09:03

Do you want this output to a new file or do you want it assigned to a variable?

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Converting Several Lines to One Line with Punctuation

#3 Post by aisha » 07 Nov 2016 09:06

sorry - c:\sandbox\newfile.txt

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Converting Several Lines to One Line with Punctuation

#4 Post by aGerman » 07 Nov 2016 12:35

If you don't have too many too long lines this should work

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "src=c:\sandbox\original.txt"
set "dst=c:\sandbox\newfile.txt"

setlocal EnableDelayedExpansion
<"!src!" (
  for /f %%i in ('type "!src!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "line=" &set /p "line="
    if %%j==1 (
      set "txt="!line!""
    ) else (
      set "txt=!txt! OR "!line!""
    )
  )
)
>"!dst!" echo(!txt!

Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Converting Several Lines to One Line with Punctuation

#5 Post by dbenham » 07 Nov 2016 12:48

You could use JREPL.BAT - a hybrid JScript/batch regular expression text processinig utility.

Code: Select all

call jrepl "[^\r\n]+|(\r?\n)+(?=[^\r\n])" "\q$&\q| OR " /x /m /t "|" /f "c:\sandbox\original.txt" /o "c:\sandbox\newfile.txt"


Dave Benham

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Converting Several Lines to One Line with Punctuation

#6 Post by aisha » 07 Nov 2016 13:34

Thank you so much to both of you...it works perfectly and now looks so much more obvious, but I really needing that extra bit of help.

Aisha

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Converting Several Lines to One Line with Punctuation

#7 Post by Aacini » 07 Nov 2016 13:41

Another one! :)

Code: Select all

@echo off
setlocal EnableDelayedExpansion

call :sub  < "c:\sandbox\original.txt"  > "c:\sandbox\newfile.txt"
goto :EOF

:sub
set /P line="""
set /P "line=!line!" "
:next
set /P "line=OR "!line!" "
if not errorlevel 1 goto next
echo/
exit /B

Antonio

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Converting Several Lines to One Line with Punctuation

#8 Post by Squashman » 07 Nov 2016 13:58

This works but could not figure out why I could not get the first two SET /P commands to work with NUL redirection.

Code: Select all

@echo off

set "src=original.txt"
set "dest=newfile.txt"

(for /f "delims=" %%G in (%src%) do (
   IF NOT DEFINED FLAG (
      echo.|set /p .="""
      echo.|set /p ".=%%G" "
      set flag=1
   ) else (
      set /p ".=OR "%%G" "<nul
   )
 )
)>"%dest%"

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Converting Several Lines to One Line with Punctuation

#9 Post by jfl » 10 Nov 2016 05:35

Squashman wrote:I could not get the first two SET /P commands to work with NUL redirection.

They do work if the NUL redirection is placed ahead of the line, instead of at the end as you did. (But I don't know why either!)
Then the code can be slightly simplified by factoring out all the <nul redirections like this:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set "FLAG="
for /f "delims=" %%G in (%1) do <nul (
  IF NOT DEFINED FLAG (
    set /p .="""
    set /p ".=%%G" "
    set FLAG=1
  ) else (
    set /p ".=OR "%%G" "
  )
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Converting Several Lines to One Line with Punctuation

#10 Post by dbenham » 10 Nov 2016 06:47

jfl wrote:
Squashman wrote:I could not get the first two SET /P commands to work with NUL redirection.

They do work if the NUL redirection is placed ahead of the line, instead of at the end as you did. (But I don't know why either!)

There are an odd number of quotes, so everything after the last quote has quote semantics, and the redirection is treated as a literal. That being said, the characters after the last quote are not printed because all characters after the last quote are ignored by SET.

You can successfully put the redirection at the end of the line if you escape the first or last quote.

Code: Select all

set /p .=^""" <nul
or

Code: Select all

set /p .=""^" <nul


Dave Benham

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Converting Several Lines to One Line with Punctuation

#11 Post by Squashman » 10 Nov 2016 07:22

dbenham wrote:You can successfully put the redirection at the end of the line if you escape the first or last quote.

Swear I tried that!

Post Reply