Using for in one-liner

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Watson

    #1

    Using for in one-liner

    Can a for loop be used in a one-liner? What am I missing?

    $ python -c "import sys;print ''.join([line for line in
    sys.stdin.readl ines()]),"
    now is
    the time
    now is
    the time

    $ python -c "import sys;for line in sys.stdin.readl ines(): print line,"
    File "<string>", line 1
    import sys;for line in sys.stdin.readl ines(): print line,
    ^
    SyntaxError: invalid syntax

    $ python -c "import sys;for i in range(5): print i,"
    File "<string>", line 1
    import sys;for i in range(5): print i,
    ^
    SyntaxError: invalid syntax
  • wittempj@hotmail.com

    #2
    Re: Using for in one-liner

    to me it seems the ',' is superfluous, this works: python -c "import
    sys;print ''.join([l for l in sys.stdin.readl ines()])" in 2.4.1 - with
    the comma it works as well but it looks weird, as if you want to
    un-pack a tuple.

    Comment

    • BranoZ

      #3
      Re: Using for in one-liner

      Paul Watson wrote:[color=blue]
      > Can a for loop be used in a one-liner? What am I missing?
      >
      > $ python -c "import sys;for i in range(5): print i,"
      > File "<string>", line 1
      > import sys;for i in range(5): print i,
      > ^
      > SyntaxError: invalid syntax[/color]

      This was tricky..

      python -c $'import sys;\nfor i in range(5): print i,'

      Separate statements with <newline> and enclose it in $'string'.

      BranoZ

      Comment

      • Paul Watson

        #4
        Re: Using for in one-liner

        wittempj@hotmai l.com wrote:[color=blue]
        > to me it seems the ',' is superfluous, this works: python -c "import
        > sys;print ''.join([l for l in sys.stdin.readl ines()])" in 2.4.1 - with
        > the comma it works as well but it looks weird, as if you want to
        > un-pack a tuple.[/color]

        Without the comma, an additional newline is written at the end.

        Comment

        • Paul Watson

          #5
          Re: Using for in one-liner

          BranoZ wrote:[color=blue]
          > Paul Watson wrote:
          >[color=green]
          >>Can a for loop be used in a one-liner? What am I missing?
          >>
          >>$ python -c "import sys;for i in range(5): print i,"
          >> File "<string>", line 1
          >> import sys;for i in range(5): print i,
          >> ^
          >>SyntaxError : invalid syntax[/color]
          >
          >
          > This was tricky..
          >
          > python -c $'import sys;\nfor i in range(5): print i,'
          >
          > Separate statements with <newline> and enclose it in $'string'.
          >
          > BranoZ[/color]

          I did try '\n' a few ways. But, I never thought to add a '$' before the
          string? What made you think of that? I cannot find any shell
          documentation about it either. Can you provide a reference? Thanks.

          Comment

          • BranoZ

            #6
            Re: Using for in one-liner

            In "man python"
            "Here command may contain multiple statements separated by
            newlines. Leading whitespace is significant in Python statements!"

            In "man bash" search for \n (/\\n)
            Frankly, I know bash for 10 years, but this has surprised me, too.

            BranoZ

            Comment

            • Paul Watson

              #7
              Re: Using for in one-liner

              BranoZ wrote:[color=blue]
              > In "man python"
              > "Here command may contain multiple statements separated by
              > newlines. Leading whitespace is significant in Python statements!"
              >
              > In "man bash" search for \n (/\\n)
              > Frankly, I know bash for 10 years, but this has surprised me, too.
              >
              > BranoZ[/color]

              Using a '$' before the string works in the ksh that is part of FC4.
              However, it does not work on the pdksh that is in FC3 and Cygwin. It
              also does not work on AIX ksh.

              $ print $'now'
              $now

              Comment

              • BranoZ

                #8
                Re: Using for in one-liner

                Paul Watson wrote:[color=blue]
                > Using a '$' before the string works in the ksh that is part of FC4.
                > However, it does not work on the pdksh that is in FC3 and Cygwin. It
                > also does not work on AIX ksh.
                >
                > $ print $'now'
                > $now[/color]

                In bash you can also use Ctrl-v followed by special character.
                (I used to reset terminal by echo "<Ctrl-v><Esc>c"<Enter >)

                Ctrl-v, Enter -> generate 0x0d to command-line
                Crtl-v, Ctrl-m -> the same as above
                Ctrl-v, Ctrl-j -> generate 0x0a (UNIX \n)

                So type:
                python -c 'import sys;
                then press Ctrl-v followed by Ctrl-j
                and type the rest..

                In vi it looks like ^@. At CLI it realy does a newline.
                I guess, you can no longer call it an one-liner ;-)

                I'm not sure whether Ctrl-v is a bash feature. More probably
                the tty driver. So, it may be worth tring it on bash-less UNIXes
                (that deserve to extinct)

                BranoZ

                Comment

                • Reinhold Birkenfeld

                  #9
                  Re: Using for in one-liner

                  BranoZ wrote:[color=blue]
                  > Paul Watson wrote:[color=green]
                  >> Using a '$' before the string works in the ksh that is part of FC4.
                  >> However, it does not work on the pdksh that is in FC3 and Cygwin. It
                  >> also does not work on AIX ksh.
                  >>
                  >> $ print $'now'
                  >> $now[/color]
                  >
                  > In bash you can also use Ctrl-v followed by special character.
                  > (I used to reset terminal by echo "<Ctrl-v><Esc>c"<Enter >)
                  >
                  > Ctrl-v, Enter -> generate 0x0d to command-line
                  > Crtl-v, Ctrl-m -> the same as above
                  > Ctrl-v, Ctrl-j -> generate 0x0a (UNIX \n)
                  >
                  > So type:
                  > python -c 'import sys;
                  > then press Ctrl-v followed by Ctrl-j
                  > and type the rest..
                  >
                  > In vi it looks like ^@. At CLI it realy does a newline.
                  > I guess, you can no longer call it an one-liner ;-)
                  >
                  > I'm not sure whether Ctrl-v is a bash feature. More probably
                  > the tty driver. So, it may be worth tring it on bash-less UNIXes
                  > (that deserve to extinct)[/color]

                  You can as well use Enter directly on the command line, together with
                  quotes.

                  Reinhold

                  Comment

                  Working...