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?
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.
I realize there are other tools that can facilitate this, but I was hoping to be able to do it with DOS.
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
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.
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.
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 :EOFThe 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.
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
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.
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
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.
Now it makes sense.
Thanks for taking the time to explain the code. You're definitely an asset to the forum !!
Just for comparison, here's a perl command that accomplishes the same thing more efficiently.
perl -pi.bak -e "s/#/#\n/g;" file.txt
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.
« Spaces in ANSI string cal... | FTP Server Programming » |