Using for in one-liner

P

Paul Watson

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.readlines()]),"
now is
the time
now is
the time

$ python -c "import sys;for line in sys.stdin.readlines(): print line,"
File "<string>", line 1
import sys;for line in sys.stdin.readlines(): 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
 
W

wittempj

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

BranoZ

Paul said:
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

This was tricky..

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

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

BranoZ
 
P

Paul Watson

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

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

Paul Watson

BranoZ said:
This was tricky..

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

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

BranoZ

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.
 
B

BranoZ

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
 
P

Paul Watson

BranoZ said:
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

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
 
B

BranoZ

Paul said:
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

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
 
R

Reinhold Birkenfeld

BranoZ said:
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)

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

Reinhold
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top