Converting Several Lines to One Line with Punctuation
Moderator: DosItHelp
Converting Several Lines to One Line with Punctuation
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
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
Re: Converting Several Lines to One Line with Punctuation
Do you want this output to a new file or do you want it assigned to a variable?
Re: Converting Several Lines to One Line with Punctuation
sorry - c:\sandbox\newfile.txt
Re: Converting Several Lines to One Line with Punctuation
If you don't have too many too long lines this should work
Steffen
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
Re: Converting Several Lines to One Line with Punctuation
You could use JREPL.BAT - a hybrid JScript/batch regular expression text processinig utility.
Dave Benham
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
Re: Converting Several Lines to One Line with Punctuation
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
Aisha
Re: Converting Several Lines to One Line with Punctuation
Another one!
Antonio
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
Re: Converting Several Lines to One Line with Punctuation
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%"
Re: Converting Several Lines to One Line with Punctuation
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" "
)
)
Re: Converting Several Lines to One Line with Punctuation
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
Code: Select all
set /p .=""^" <nul
Dave Benham
Re: Converting Several Lines to One Line with Punctuation
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!