473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.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
Aug 15 '05 #1
8 2428
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.

Aug 15 '05 #2
Paul Watson wrote:
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

Aug 15 '05 #3
wi******@hotmail.com wrote:
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.
Aug 15 '05 #4
BranoZ wrote:
Paul Watson wrote:
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


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.
Aug 15 '05 #5
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

Aug 15 '05 #6
BranoZ wrote:
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
Aug 15 '05 #7
Paul Watson wrote:
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

Aug 15 '05 #8
BranoZ wrote:
Paul Watson wrote:
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)


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

Reinhold
Aug 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
12
by: bj7lewis | last post by:
I am working on a project I want to add a few files as resource to access(copy them to FS and use) at runtime. So far in VS.NET IDE, I Add Files to the project and set its Build Action to...
6
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.