PC Review


Reply
Thread Tools Rate Thread

echo, newline escaping, and more

 
 
Csaba Gabor
Guest
Posts: n/a
 
      4th Dec 2007
My question is how to pipe newlines directly
from echo into more.

Detailed explanation:
It is possible to escape newlines in cmd.exe
by ending a line with the caret (^) and
then pressing the enter key twice. Thus,
the following 3 lines will display foo, and
on the next line it will display bar:

echo foo^

bar


Very nifty. What I would now like, however,
is for the result of an echo, piped into
more, to display foo on one line, followed
by bar on the next. The following 3 lines
do not work:

echo foo^

bar | more


more will only shows the first line, foo.
What is going on is that when echo is piped
into more, for reasons I have not fathomed,
the command line parser runs over the
command line twice (for example, there are
two levels of environment variable expansion
as will be seen shortly). Therefore, it
would seem that I have to escape the above 3
lines once to the following 5 lines (using
double caret below (^^) to escape a single
caret (^) above and ^ followed by two newlines
below to escape a single newline above):

echo foo^^^

^

bar | more



Unfortunately, this does not work, and as
before, I only get the first line showing:

foo

Furthermore, if the last line is only bar
instead of bar | more, then the 3 line
output from echo's single pass is as
expected:

foo^

bar



Now you will be thinking, Csaba, really,
you were overly optimistic thinking that
the command parser would be willing to
accommodate that screwy newline escaping.
You should be lucky that it worked at all
with echo, never mind the pipe.

Well, perhaps so, but then I would expect
the following 5 lines to similarly fail,
but it does quite nicely, thank you very
much.

set nl=^


set nlm=^^^^%^nl%%^nl%
echo foo%nlm%bar | more


The 'echo ... | more' demonstrates
the double substitution mentioned earlier.
The last two lines would also work as:

set nlm=^^^%nl%%nl%^%nl%%nl%
echo foo%^nlm%bar | more

which 2nd variant causes a single expansion
on the 2nd pass.


But I wonder if it is possible to avoid
the expansion on the second pass, which
happens in both examples. In other words,
could I have an environment variable
just expand on the first pass? And this
should be the same as my original question:
How can I pipe newlines directly from
echo into more?


Csaba Gabor from Vienna


PS. The above may be tested by copying
the specified number of lines and then
at the cmd.exe prompt doing (Alt+space)ep
(enter may need to be pressed depending
on whether the copied section has a final
newline in it)

PPS. Environment variable expansion
happens first, and then escaped characters
are unescaped. Thus, an isolated ^ in an
environment variable will seemingly
disappear.

PPPS. VBScript group copied as a possilble
interest topic when using Run/Exec
 
Reply With Quote
 
 
 
 
foxidrive
Guest
Posts: n/a
 
      4th Dec 2007
On Tue, 04 Dec 2007 15:38:36 +0100, Csaba Gabor <(E-Mail Removed)> wrote:

>My question is how to pipe newlines directly
>from echo into more.
>
>Detailed explanation:
>It is possible to escape newlines in cmd.exe
>by ending a line with the caret (^) and
>then pressing the enter key twice. Thus,
>the following 3 lines will display foo, and
>on the next line it will display bar:
>
>echo foo^
>
>bar
>
>
>Very nifty. What I would now like, however,
>is for the result of an echo, piped into
>more, to display foo on one line, followed
>by bar on the next. The following 3 lines
>do not work:
>
>echo foo^
>
>bar | more


Is this useful?

@echo off
(
echo foo
echo.
echo bar
)|more


 
Reply With Quote
 
 
 
 
Csaba Gabor
Guest
Posts: n/a
 
      5th Dec 2007
foxidrive wrote:
> On Tue, 04 Dec 2007 15:38:36 +0100, Csaba Gabor <(E-Mail Removed)> wrote:
>> My question is how to pipe newlines directly
>>from echo into more.

....
>> echo foo^
>>
>> bar
>>
>>
>> Very nifty. What I would now like, however,
>> is for the result of an echo, piped into
>> more, to display foo on one line, followed
>> by bar on the next. The following 3 lines
>> do not work:
>>
>> echo foo^
>>
>> bar | more

>
> Is this useful?
>
> @echo off
> (
> echo foo
> echo.
> echo bar
> )|more


Fabulous!
From the command line I can now do:

(echo foo && echo bar) | more
(echo.foo&&echo.bar)|more

Not only that, but it also gives the same
output without the piping:

(echo foo && echo bar)

Furthermore, I'd been wanting a way to
make a totally blank prompt/page under cmd.exe
and this is an easy way:

(@echo off && cls)


Thanks very much for your suggestion Foxi!
Csaba Gabor from Vienna

PS. The results are meant for use with a
single invocation of the command line.
 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a
 
      7th Dec 2007

"Csaba Gabor" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> foxidrive wrote:
>> On Tue, 04 Dec 2007 15:38:36 +0100, Csaba Gabor <(E-Mail Removed)> wrote:
>>> My question is how to pipe newlines directly
>>>from echo into more.

> ...
>>> echo foo^
>>>
>>> bar
>>>
>>>
>>> Very nifty. What I would now like, however,
>>> is for the result of an echo, piped into
>>> more, to display foo on one line, followed
>>> by bar on the next. The following 3 lines
>>> do not work:
>>>
>>> echo foo^
>>>
>>> bar | more

>>
>> Is this useful?
>>
>> @echo off
>> (
>> echo foo
>> echo.
>> echo bar
>> )|more


I often use compound commands like that when I want a batch script to
generate a log file of its proceedings, as I think that this:

(
echo/starting at:
time /t
callrocess
echo/done at
time /t
) >logfile.log

is more efficient than multiple instances of the ">>" redirection operator,
in addition to being simpler to get right.

/Al


> Fabulous!
> From the command line I can now do:
>
> (echo foo && echo bar) | more
> (echo.foo&&echo.bar)|more
>
> Not only that, but it also gives the same
> output without the piping:
>
> (echo foo && echo bar)
>
> Furthermore, I'd been wanting a way to
> make a totally blank prompt/page under cmd.exe
> and this is an easy way:
>
> (@echo off && cls)
>
>
> Thanks very much for your suggestion Foxi!
> Csaba Gabor from Vienna
>
> PS. The results are meant for use with a
> single invocation of the command line.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HttpWebRequest and headers with only newline Mikael Svenson Microsoft Dot NET Framework 2 5th Mar 2005 11:33 AM
string.split and Environment.NewLine question. =?Utf-8?B?QXNoYQ==?= Microsoft ASP .NET 1 30th Dec 2004 10:33 AM
System::String, std::string and const char* and newline Vasco Lohrenscheit Microsoft VC .NET 0 13th Jan 2004 04:50 PM
Re: how to add newline and align GVaught Microsoft Access Form Coding 1 5th Sep 2003 03:47 PM
Set NewLine styles -- CRLF and LF? Quan Nguyen Microsoft C# .NET 0 21st Aug 2003 04:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:14 PM.