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 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.
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 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.
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.
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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()',...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |