15} Is it possible to echo without linefeed like the Unix echo -n?
At least with
gawk, yes. The following
example script demonstrates
@echo off
echo Testline 1|gawk '{printf"%%s",$0}'
echo Testline 2|gawk '{printf"%%s",$0}'
echo Testline 3|gawk '{printf"%%s\n",$0}'
The output will be
C:\_D\TEST>cmdfaq
Testline 1Testline 2Testline 3
@echo off
echo Testline 1|
unxgawk "{printf
\"%%s
\",$0}
"
echo Testline 2|unxgawk "{printf\"%%s\",$0}"
echo Testline 3|unxgawk "{printf\"%%s\n\",$0}"
Related subject: Re: Combining two single-line files into one
single-line file:
Q: The following question was posed on the Usenet news:
File1.txt contains: Asset #353
File2.txt contains: 3-25-02
I want File3.txt to contain: Asset #353 3-25-02
A: One, but not the only option is
@echo off
<File1.txt gawk '{printf"%%s",$0}'>File3.txt
<File2.txt gawk '{printf" %%s\n",$0}'>>File3.txt
@echo off
<File1.txt unxgawk "{printf\"%%s\",$0}">File3.txt
<File2.txt unxgawk "{printf\" %%s\n\",$0}">>File3.txt
The sister package for MS-DOS+Win..95/98/ME
tsbat.zip
Useful MS-DOS batch files and tricks, T.Salmi
contains ECHON.EXE "echo -n, Echo without a linefeed"
which works also on NT/2000/XP (or at least 32-bit XP, but
not 64-bit).
@echo off & setlocal enableextensions
if defined ProgramW6432 (
echo/
echo Exiting: %~f0 is incompatible with 64-bit Windows
goto :EOF)
C:\_F\BAT\ECHON Testline 1
C:\_F\BAT\ECHON Testline 2
C:\_F\BAT\ECHON Testline 3
endlocal & goto :EOF
The output:
D:\TEST>cmdfaq
Testline 1Testline 2Testline 3
Many of the tsbat.zip solutions not necessarily included in
tscmd.zip work for NT/2000/XP. However, the reverse is not true.
Most of the code in tscmd.zip is not valid for MS-DOS+Win..95/98/Me.
The
solutions in the comment demonstrate that this task can be
solved without external tools. The solution below is based on Phil's
posting. Note the ^ quoting of 1, 2, and 3 lest they be taken as
handles.
@echo off & setlocal
echo.|set /p=Testline
^1>tmp.txt
echo.|set /p=Testline ^2>>tmp.txt
echo.Testline ^3>>tmp.txt
for %%c in (type del) do %%c tmp.txt
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
Testline 1 Testline 2 Testline 3
Alternatively
@echo off & setlocal enableextensions
<nul (set /p z="Testline 1 ")>tmp.txt
<nul (set /p z="Testline 2 ")>>tmp.txt
<nul (set /p z="Testline 3")>>tmp.txt
echo.>>tmp.txt
for %%c in (type del) do %%c tmp.txt
endlocal & goto :EOF
The most concise method not producing the <CR><LF> pair is
@echo off & setlocal enableextensions disabledelayedexpansion
if not "%~1"=="" goto nolfcr
echo Usage: %~0 "Your text"
goto :EOF
::
:nolfcr
set /p="%~1"<nul
endlocal & goto :EOF
As an aside, if for some unfathomable reason you want to create a
file of 1 byte (one, not two) then
@echo off & setlocal enableextensions
echo.|set /p=>myfile
dir myfile
endlocal & goto :EOF
The
original problem can also be solved with a Visual Basic Script
(VBScript) aided command line script
@echo off & setlocal enableextensions
set vbs_=%temp%\tmp$$$.vbs
> "%vbs_%" echo WScript.StdOut.
Write "Prof. (emer.) "
>>"%vbs_%" echo WScript.StdOut.Write "Timo Salmi, "
>>"%vbs_%" echo WScript.StdOut.Write "University of Vaasa, "
>>"%vbs_%" echo WScript.StdOut.
WriteLine "Finland"
cscript //nologo "%vbs_%"
echo See, it works!
for %%f in ("%vbs_%") do if exist %%f del %%f
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
Prof. (emer.) Timo Salmi, University of Vaasa, Finland
See, it works!
Alternatively, the VBS-aided command line script could be formulated as
@echo off & setlocal enableextensions
set vbs_=%temp%\tmp$$$.vbs
(
echo WScript.StdOut.Write "Prof. (emer.) "
echo WScript.StdOut.Write "Timo Salmi, "
echo WScript.StdOut.Write "University of Vaasa, "
echo WScript.StdOut.WriteLine "Finland"
)>"%vbs_%"
cscript //nologo "%vbs_%"
echo See, it works!
for %%f in ("%vbs_%") do if exist %%f del %%f
endlocal & goto :EOF
Using VBS and the subroutine approach (slower) we can write
@echo off & setlocal enableextensions
call :EchoVbsN "Prof. (emer.) "
call :EchoVbsN "Timo Salmi, "
call :EchoVbsN "University of Vaasa, "
echo Finland
echo See, it works!
endlocal & goto :EOF
::
:: =====================================
:: Subroutine: Echo without the linefeed
: EchoVbsN
@echo off & setlocal enableextensions
set vbs_=%temp%\tmp$$$.vbs
>"%vbs_%" echo WScript.StdOut.Write "%~1"
cscript //nologo "%vbs_%"
for %%f in ("%vbs_%") do if exist %%f del %%f
endlocal & goto :EOF