Jump to content

Inserting carriage returns in text file


Recommended Posts

Guest GKW in GA
Posted

I have a text file that 8 numbers per row separated by a space with a

carriage return after the last number in the row. How can I insert a carriage

return after each number in the text file so I only have 1 number per row.

 

In other words I want to change:

123456 123456 123456 123456 123456 123456 123456123456

 

To

 

123456

123456

123456

123456

 

If there is an other way to do this besides replacing the blank with a

carriage return, I am open to it

  • Replies 3
  • Created
  • Last Reply
Guest Pegasus \(MVP\)
Posted

Re: Inserting carriage returns in text file

 

 

"GKW in GA" <GKWinGA@discussions.microsoft.com> wrote in message

news:7C6C1489-C4E4-4BDD-8654-8354E724B02B@microsoft.com...

>I have a text file that 8 numbers per row separated by a space with a

> carriage return after the last number in the row. How can I insert a

> carriage

> return after each number in the text file so I only have 1 number per row.

>

> In other words I want to change:

> 123456 123456 123456 123456 123456 123456 123456123456

>

> To

>

> 123456

> 123456

> 123456

> 123456

>

> If there is an other way to do this besides replacing the blank with a

> carriage return, I am open to it

>

 

You could do it manually in MS Word by replacing each space with

a ^p (the symbol for a paragraph marker).

 

You could do it automatically with this batch file:

@echo off

set SourceFile=d:\temp\test.txt

set TargetFile=c:\out.txt

 

if exist "%TargetFile%" del "%TargetFile%"

for /F "delims=" %%a in ('type "%SourceFile%"') do call :Sub %%a

notepad "%TargetFile%"

goto :eof

 

:Sub

echo %1 >> "%TargetFile%"

if "%2"=="" goto :eof

shift

goto sub

Guest GKW in GA
Posted

Re: Inserting carriage returns in text file

 

I used the BAT file technique. It work great ...........thanks

 

Would you mind explaining what each line of code does starting with 'if

exist'. It's all kind of Greek to me

 

"Pegasus (MVP)" wrote:

>

> "GKW in GA" <GKWinGA@discussions.microsoft.com> wrote in message

> news:7C6C1489-C4E4-4BDD-8654-8354E724B02B@microsoft.com...

> >I have a text file that 8 numbers per row separated by a space with a

> > carriage return after the last number in the row. How can I insert a

> > carriage

> > return after each number in the text file so I only have 1 number per row.

> >

> > In other words I want to change:

> > 123456 123456 123456 123456 123456 123456 123456123456

> >

> > To

> >

> > 123456

> > 123456

> > 123456

> > 123456

> >

> > If there is an other way to do this besides replacing the blank with a

> > carriage return, I am open to it

> >

>

> You could do it manually in MS Word by replacing each space with

> a ^p (the symbol for a paragraph marker).

>

> You could do it automatically with this batch file:

> @echo off

> set SourceFile=d:\temp\test.txt

> set TargetFile=c:\out.txt

>

> if exist "%TargetFile%" del "%TargetFile%"

> for /F "delims=" %%a in ('type "%SourceFile%"') do call :Sub %%a

> notepad "%TargetFile%"

> goto :eof

>

> :Sub

> echo %1 >> "%TargetFile%"

> if "%2"=="" goto :eof

> shift

> goto sub

>

>

>

Guest Pegasus \(MVP\)
Posted

Re: Inserting carriage returns in text file

 

Glad you asked - few people bother to find out how their

utility program functions.

 

The overall concept is simple: Grab each element in your line

of text and add it as a single line to your results file. Since

we're "adding" it to the results file, we must ensure that we

start with a clean slate. This line of code will do it:

if exist "%TargetFile%" del "%TargetFile%"

Without this line we would add elements to whatever the

file contained before.

 

for /F "delims=" %%a in ('type "%SourceFile%"') do call :Sub %%a

This line uses advanced batch file techniques. It says: Grab one line

of text at a time from "SourceFile" and pass it as a parameter to the

subroutine that I chose to call "Sub".

 

:Sub

This is the label that marks the beginning of the subroutine called "Sub".

 

echo %1 >> "%TargetFile%"

This line will grab the first element of the received parameter and

append it to the target file. A single > would "write" the parameter

to the target file. A double >> will "append" it to that file. If your

line was

A123456 B123456 C123456 D123456 123456 123456 123456123456

then the first parameter would be A123456, because "parameters"

are text elements separated by one or more spaces.

 

if "%2"=="" goto :eof

Here we check if there is a further parameter to be dealt with. If

there isn't then we drop out of the subroutine.

 

shift

The shift command will move all parameters by one element to the

left. Hence

A123456 B123456 C123456 D123456 123456 123456 123456123456

becomes

B123456 C123456 D123456 123456 123456 123456123456

then

C123456 D123456 123456 123456 123456123456

and so on. This means that it is sufficient for the program to write

the first parameter (%1) do the output file, until there is nothing left.

 

goto sub

This instruction jumps back to the ":sub" label so that we start

all over again.

 

Enjoy!

 

"GKW in GA" <GKWinGA@discussions.microsoft.com> wrote in message

news:A40E5C01-8E08-4C23-8B36-E825273D3891@microsoft.com...

>I used the BAT file technique. It work great ...........thanks

>

> Would you mind explaining what each line of code does starting with 'if

> exist'. It's all kind of Greek to me

>

> "Pegasus (MVP)" wrote:

>

>>

>> "GKW in GA" <GKWinGA@discussions.microsoft.com> wrote in message

>> news:7C6C1489-C4E4-4BDD-8654-8354E724B02B@microsoft.com...

>> >I have a text file that 8 numbers per row separated by a space with a

>> > carriage return after the last number in the row. How can I insert a

>> > carriage

>> > return after each number in the text file so I only have 1 number per

>> > row.

>> >

>> > In other words I want to change:

>> > 123456 123456 123456 123456 123456 123456 123456123456

>> >

>> > To

>> >

>> > 123456

>> > 123456

>> > 123456

>> > 123456

>> >

>> > If there is an other way to do this besides replacing the blank with a

>> > carriage return, I am open to it

>> >

>>

>> You could do it manually in MS Word by replacing each space with

>> a ^p (the symbol for a paragraph marker).

>>

>> You could do it automatically with this batch file:

>> @echo off

>> set SourceFile=d:\temp\test.txt

>> set TargetFile=c:\out.txt

>>

>> if exist "%TargetFile%" del "%TargetFile%"

>> for /F "delims=" %%a in ('type "%SourceFile%"') do call :Sub %%a

>> notepad "%TargetFile%"

>> goto :eof

>>

>> :Sub

>> echo %1 >> "%TargetFile%"

>> if "%2"=="" goto :eof

>> shift

>> goto sub

>>

>>

>>


×
×
  • Create New...