Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to replace "new line" with comma and space in command prompt 1

Status
Not open for further replies.

grofaty

IS-IT--Management
Jan 16, 2003
370
SI
Hi,
using Windows XP, I would like to replace "new line" character with comma and space.

My command outputs the following result:
AAA
BBB
CCC
DDD
etc

I would like to write a script to get:
AAA, BBB, CCC, DDD, etc

How to write such a script in command prompt?
Regards,
Grofaty
 
@Diancecht, my command is very complex script that gets data from relation database. The output from relation database returns data in one column - separated by carriage return and new line characters. I would like to output data from column to one row delimited by comma.

@linney, thanks, but I don't want to use Visual Basic script. I would just like to use ordinary command prompt commands if available. I also need to write the same script for Linux, so I don't want to go in deep into some scripting language.
 
Hi, Grofaty

Under linux you will likely use grep.

Under windows I don't know an easy way with built-in commands to do what you want. However, there is a freeware program called FART (I kid you not) which looks like it can do what you want:

PS: Their animated logo is a hoot (or should I say toot).

Jock
 
I think the following code does what you want. It must be run from a command prompt that has delayed environment variable expansion enabled with the /V:ON switch, e.g. Start, Run..., cmd /V:ON

The code example is reading text file d:\test.txt. I used notepad to create this file and each line contains 3 letters of data:
AAA
BBB
CCC
...
ZZZ

The last set command eliminates the final trailing comma inserted by the FOR command.

Code:
@ECHO OFF

d:
cd \

set out=

FOR /F %%i in (test.txt) do set out=!out!%%i,
echo %out%

set out=%out:~0,-1%
echo %out%
 
oops, I missed the comma and space requirement. There is a space following the comma at the end of the FOR command

Code:
@ECHO OFF

d:
cd \

set out=

FOR /F %%i in (test.txt) do set out=!out!%%i, 
echo %out%

set out=%out:~0,-2%
echo %out%

 
@Freestone,
I have little bit changed the code and it works fine.
Code:
@ECHO OFF
set out=
FOR /F %%i in ('type myfile.txt') do set out=!out!%%i,
echo %out%

The only think I dislike is turning on "cmd /v:eek:n" - is there any way to turn of this settings on the fly and not when starting the cmd. I would like to send this code to multiple users and I can't influence how they start the cmd.

The same problem. If I already have cmd opened and execute "cmd /v:eek:n" and I lose all history of commands (if using up key to get previous commands that were executed).
Regards
 
The only think I dislike is turning on "cmd /v:eek:n" - is there any way to turn of this settings on the fly and not when starting the cmd
I haven't tried this, but the follow text is from the cmd help (cmd /?):
Code:
Delayed environment variable expansion is NOT enabled by default.  You can enable or disable delayed environment variable expansion for a particular invocation of CMD.EXE with the /V:ON or /V:OFF switch.  You can enable or disable completion for all invocations of CMD.EXE on a machine and/or user logon session by setting either or both of the
following REG_DWORD values in the registry using REGEDT32.EXE:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion

and/or

HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion

to either 0x1 or 0x0.  The user specific setting takes precedence over the machine setting.  The command line switches take precedence over the registry settings.
An alternative may be to write another batch file that invokes your first one. This batch file invokes a secondary command processor with the V:ON specified:

Code:
cmd /v:ON /c d:\target.cmd

target.cmd would be your code from your above post. Note though that upon exit of target.cmd your environmnet variable out will not be available to the invoking cmd processor. There may be a work around for this but I am not familiar with one at this moment.

If I already have cmd opened and execute "cmd /v:eek:n" and I lose all history of commands (if using up key to get previous commands that were executed).
If your invoke a new command processor from another, typing exit at the command prompt will return you to the parent command processor and you'll regain its command history. There may be a way for a child command processor to inherit the parent's command history, but again, I am not aware of any.
 
Hi,
thank you very much for your help! I have also found out the following web page "Windows 2000 delayed environment variable expansion".

The "cmd /v:eek:n" and "cmd /v:eek:ff" can also be written for just part of batch file - exactly what I need.

I just merged all your great! ideas with article tips and got the following code that solves ALL problems I have written above.
Code:
@ECHO OFF
REM The same as cmd /v:on - valid only for current batch file from this after the following command
setlocal ENABLEDELAYEDEXPANSION

set out=
FOR /F %%i in ('type myfile.txt') do set out=!out!%%i,
set out=%out:~0,-1%
echo %out%

REM The same as cmd /v:off. From this part of file all commands use just like "cmd /v:off settings".
endlocal

Thanks a lot for help.

Problem solved.
 
Hi,
one more problem. If file myfile.txt if empty then echo command returns:
~0,-1

How to check if %out% is empty?
Regards
 
Hi,
I have some kind solve the problem by using:
echo %out% | find /v "~0,-1%"
instead off
echo %out%
but using above find command, still empty line is displayed.

Any other idea how to solve the problem?
Regards
 
Hi,
one more problem. If myfile.txt has a lot of data then error is returned: "The input line is too long."
It looks like of some kind of limit. Any idea how to overcome this limit?
Regards
 
You are reaching the command line length limit of 8196 bytes. Probably time to think about abandoning the command line approach and use VB or VB SCRIPT or some other programming language to solve this issue. Even then, you may have to consider string length limitations. How many lines of 3 characters each are you expecting to concatenate?

As far as checking for an empty environment variable, something like: if .%out%==. goto Wherever will work. I've implemented it below as an example.

Code:
@ECHO Off
REM The same as cmd /v:on - valid only for current batch file from this after the following command
setlocal ENABLEDELAYEDEXPANSION

set out=
FOR /F %%i in ('type myfile.txt') do set out=!out!%%i,

if "%out%" == "" goto End

set out=%out:~0,-1%
echo %out%

:End

REM The same as cmd /v:off. From this part of file all commands use just like "cmd /v:off settings".
endlocal
Why did you use 'type myfile.txt' in the FOR command rather than just the filename?
 
@Freestone, thanks a lot for help. Thanks for if command tip. I really appreciate you help.

I have drastically reduced the size of string in each column, so no more error of max length of string.

3 char data fields where just simple sample not to bother you. Data fields are longer.

I know 'type myfile.txt' is not needed in this sample. But instead of this command I have a command to get data from database, so I just try if commands can be executed instead of just reading the file.
 
Thank you for your explanations, grofaty. And I am glad I was able to help. I learned much from this too!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top