Computing.Net > Forums > Programming > Insert carriage return w/ batch

Insert carriage return w/ batch

Reply to Message Icon

Original Message
Name: ebrian
Date: November 2, 2005 at 05:28:46 Pacific
Subject: Insert carriage return w/ batch
OS: XP
CPU/Ram: 1GB
Comment:

Is there a way to insert a carriage return (new line character) into a file? I would like to be able to insert a carriage return whenever I find (using finstr) a # character in a file.

For example, if the file contained the string:

"This is line 1 # this should be line 2."

I would like to break it apart and save it into another file as:

"This is line 1 #
this should be line 2."

Any suggestions?


Report Offensive Message For Removal


Response Number 1
Name: wizard-fred
Date: November 2, 2005 at 07:39:37 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

There are unix style utilities like awk and sed that can do this easily. Otherwise a good text editor (work processor ) can replace a character with another.


Report Offensive Follow Up For Removal

Response Number 2
Name: ebrian
Date: November 2, 2005 at 07:50:31 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

I realize there are other tools that can facilitate this, but I was hoping to be able to do it with DOS.


Report Offensive Follow Up For Removal

Response Number 3
Name: djas
Date: November 2, 2005 at 09:01:23 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Well "echo." gives you a blacnk line if thats any use to you but I'm afraid I don't know of any carriage return method.

djas


Report Offensive Follow Up For Removal

Response Number 4
Name: IVO
Date: November 2, 2005 at 12:36:36 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

It is possible to do what you posted with the NT batch language of Windows XP (that has no DOS at all as it is a NT kernel system), but the key point is the following:

Is your file a true text file, i.e. a collection of records each delimited by CR/LF, the "rows", or just a byte stream ended by an EOF symbol?

If each row holds a variable number of strings delimited by #, then the job can be done by batch, otherwise the stream can not be handled by a script languag aimed to process text files.

So please explain and than we can try to help... if possible.


Report Offensive Follow Up For Removal

Response Number 5
Name: ebrian
Date: November 2, 2005 at 15:43:00 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

The text file is a true text file, it contains multiple records all of which are delimited by CR/LF and some of which contain # symbols.


Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: November 3, 2005 at 02:24:13 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Well, here is your script that works under Win NT/2K/XP only. Pay attention the :SPLIT internal routine is a recursive one, i.e. it calls itself, so if the row holds too many # substrings a stack overflow may occur. Last point, the space lines marked by a CR/LF only are lost. So stated I hope you get what you wish.

@Echo OFF

For /f "tokens=1,* delims=#" %%a in (%*) Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF

:SPLIT
For /f "tokens=1,* delims=#" %%a in (" %*") Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF

The label :EOF does not need to be declared as it is the standard return point for NT sequences.

To play the script type Split File_Name, assuming the batch's name is Split.bat.




Report Offensive Follow Up For Removal

Response Number 7
Name: ebrian
Date: November 3, 2005 at 03:42:16 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Thanks for your help IVO. The script does split the lines, however it removes the # symbol.


Report Offensive Follow Up For Removal

Response Number 8
Name: IVO
Date: November 3, 2005 at 07:55:38 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

OK, this sligthly modified version saves the separator; I used the character @, but you can choose any other under the condition that is not a symbol typed in the text.

To save the output to a new file type

Split File_Name > File_Out

@Echo Off

For /f "tokens=* delims=" %%a in (%*) Do (
Call :SPLIT %%a)
GoTo :EOF

:SPLIT
Set Line=%*
Set Line=%Line:#=#@%
For /f "tokens=1,* delims=@" %%a in ('Echo %Line%') Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF


Report Offensive Follow Up For Removal

Response Number 9
Name: ebrian
Date: November 3, 2005 at 09:50:33 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Thanks IVO, your the master at this stuff. I could follow your first iteration of this, but I don't understand:

Set Line=%*
Set Line=%Line:#=#@%

in your final version.


Report Offensive Follow Up For Removal

Response Number 10
Name: Mechanix2Go
Date: November 3, 2005 at 09:54:48 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Yep, IVO's the man.

I don't understand any of it but I'm muddling along.


If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 11
Name: IVO
Date: November 3, 2005 at 12:07:37 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

That's the trick to retain the # separator, i.e. to suffix it with a new exot(er)ic character to use as marker in place of the native one. So each occurence of # is replaced by #@ (@ as the new symbol) and then the split is performed by looking for @, as the separator itself is lost in parsing.

Set Line=%*

assigns to the environment variable Line the string to be parsed and

Set Line=%Line:#=#@%

replaces each occurence of # with #@

Obviously to work the appended character needs to have a special meaning in the text context, i.e. it must not be used elsewhere in the body of the file. That's the reason why I choose a character like @, but other good choices may be % $ £ § ç according to your country.

I hope my explanation helps.


Report Offensive Follow Up For Removal

Response Number 12
Name: ebrian
Date: November 3, 2005 at 14:18:03 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Now it makes sense.

Thanks for taking the time to explain the code. You're definitely an asset to the forum !!


Report Offensive Follow Up For Removal

Response Number 13
Name: FishMonger
Date: November 4, 2005 at 14:08:40 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Just for comparison, here's a perl command that accomplishes the same thing more efficiently.

perl -pi.bak -e "s/#/#\n/g;" file.txt


Report Offensive Follow Up For Removal

Response Number 14
Name: Jiriki
Date: January 6, 2006 at 07:09:04 Pacific
Subject: Insert carriage return w/ batch
Reply: (edit)

Here's a neat trick. Right-click on My Computer | Properties | Advanced tab | Environment Variables button. Under System Variables, click New.

In the Variable name type something like CR. Make sure NumLock is on, in the Variable value field, press and hold the ALT key, now press and release the 1 key on the number pad and then the 0 key on the number pad, now release the ALT key. You should get a Square. Click OK.

Now in your batch file whenever you want a carriage return, use %CR%. If you write to a file, notepad can't interperet this ASCII, but if you open in Write, Word or other word processor, it will be a carriage return. Standard out to console will interpert it as well as Net Send command.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Insert carriage return w/ batch

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge