echo, newline escaping, and more

Discussion in 'Microsoft Windows 2000 CMD Promt' started by Csaba Gabor, Dec 4, 2007.

  1. Csaba Gabor

    Csaba Gabor Guest

    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
     
    Csaba Gabor, Dec 4, 2007
    #1
    1. Advertisements

  2. Csaba Gabor

    foxidrive Guest

    On Tue, 04 Dec 2007 15:38:36 +0100, Csaba Gabor <> 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
     
    foxidrive, Dec 4, 2007
    #2
    1. Advertisements

  3. Csaba Gabor

    Csaba Gabor Guest

    foxidrive wrote:
    > On Tue, 04 Dec 2007 15:38:36 +0100, Csaba Gabor <> 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.
     
    Csaba Gabor, Dec 5, 2007
    #3
  4. Csaba Gabor

    Al Dunbar Guest

    "Csaba Gabor" <> wrote in message
    news:...
    > foxidrive wrote:
    >> On Tue, 04 Dec 2007 15:38:36 +0100, Csaba Gabor <> 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
    call:process
    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.
     
    Al Dunbar, Dec 7, 2007
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wensi Peng

    NewLine in batch file

    Wensi Peng, Jan 14, 2005, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    2
    Views:
    1,137
    Wensi Peng
    Jan 15, 2005
  2. Jonathan

    differences between : 'echo toto' and 'echo/toto'

    Jonathan, Jan 17, 2005, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    3
    Views:
    277
    Al Dunbar [MS-MVP]
    Jan 18, 2005
  3. cliff
    Replies:
    6
    Views:
    846
    Timo Salmi
    Sep 12, 2005
  4. Igor Solodovnikov

    JT.exe and escaping " marks

    Igor Solodovnikov, Jan 31, 2006, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    3
    Views:
    363
    Bill Stewart
    Feb 7, 2006
  5. Jonathan de Boyne Pollard

    Re: Echo., Echo., Echo/, Etc.

    Jonathan de Boyne Pollard, Feb 4, 2010, in forum: Microsoft Windows 2000 CMD Promt
    Replies:
    8
    Views:
    840
    Les Matthew
    Feb 19, 2010
Loading...

Share This Page