473,671 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

where is the awk to python translator program

An old dog can't learn new tricks, so where's the a2py awk to python
translator? Perl has a2p. E.g. today I wonder how to do '{print
$1}', well with a2p I know how to do it in perl, but with python I am
supposed to hunker down with the manuals.
Jul 18 '05 #1
2 8415

Dan> An old dog can't learn new tricks, so where's the a2py awk to python
Dan> translator? Perl has a2p.

Sorry, Python doesn't have such a beast. There's never been enough demand.

Dan> E.g. today I wonder how to do '{print $1}', well with a2p I know
Dan> how to do it in perl, but with python I am supposed to hunker down
Dan> with the manuals.

Python doesn't have the implicit looping and splitting into fields that awk
has, so you need to loop over the input and split the fields yourself:

import sys
for line in sys.stdin:
print line.split()[0] # == '{print $1}'

There's also no global field separator variable. Suppose I wanted to print
the first field in each row of my passwd file. The split() method, when
called as above with no arguments, will split the string on any runs of
whitespace. If given a string parameter it will split the string on that
string. Here's some input and output pasted from an interactive session:
for line in file("/etc/passwd"):

... print line.split(":")[0]
...
root
bin
daemon
adm
lp
... and so on ...

Obviously, Python is not as concise as awk for the sorts of things awk is
good at, but then Python is good for a lot more things than awk is. ;-)

Skip

Jul 18 '05 #2
Why translate? PyAWK may do what you need:

http://pyawk.sourceforge.net/

I don't have any direct experiences with it, but it does look
interesting.
Dan Jacobson wrote:
An old dog can't learn new tricks, so where's the a2py awk to python
translator? Perl has a2p. E.g. today I wonder how to do '{print
$1}', well with a2p I know how to do it in perl, but with python I am
supposed to hunker down with the manuals.


Jul 18 '05 #3

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

Similar topics

7
17628
by: Bruce Dickey | last post by:
Hi, I have some PHP scripts I'm moving from my web server to the local PC. They just do file manipulation. Anybody know of a tool for translating PHP to Python? A partial translation would be hwlpful. Also, any tips or pritfalls to look out for?
0
1506
by: Bruce Dickey | last post by:
Hi, I'm moving some functionality from the server to the local PC and need to translate some PHP scripts to Python. They just do file manipulation. Anybody know of a PHP to Python translator? Even an alpha or beta program would help. Also, any tips or pitfalls to look out for? I'm a Python/C/C++ programmer who is only aquainted with PHP.
42
4076
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time. I'm not interested on which language is better in *general*, just for my purpose. My area of research is in CAD algorithms, and I'm sensing the need to resort to something more expedient than C++, bash scripting, or sed scripting.
53
3664
by: Stelios Xanthakis | last post by:
Hi. pyvm is a program which can run python 2.4 bytecode (the .pyc files). A demo pre-release is available at: http://students.ceid.upatras.gr/~sxanth/pyvm/ Facts about pyvm: - It's FAST. According to the "cooked-bench" benchmark suite it finishes in 55% of the time python takes;)
7
3728
by: Eric van Riet Paap | last post by:
Wouldn't it be nice if we could program our browser in Python instead of Javascript? - Eric
23
2487
by: napi | last post by:
Does anybody know if there's any C99 to C89 translator out there, either free or commercial. I searched on Google but couldn't see it so far. Thanks for any tips. Napi
9
1745
by: nephish | last post by:
lo there all ! i have a huge delima, i have to be able to connect to a data server and recieve info from it. The servers software guys release some visual C++ modules that one can incorporate into a visual C++ project. Which is great, but i am developing in linux, and only am very familliar with python. i have tried to use swig to make a python module out of it, and it wouldn't work. Tried to just compile them with gcc and that would...
1
1506
by: Patrick Mullen | last post by:
(sorry michael, didn't mean to personal post On Wed, Mar 19, 2008 at 9:24 AM, Michael Wieher wrote: No, py2exe basically bundles the main script and the interpreter together so it's easy to run and requires no python installation. Look into pyrex and pypy. A mature translator doesn't exist. Also there is ctypes which goes in reverse letting you use more c from python easily.
5
1776
by: Bob Nelson | last post by:
My interest in this new topic was piqued by santosh's recent posting concerning the subject of ``diagnostic message'' here in clc. After reading the C99 standard (``n1256.pdf''), I am unclear if the requirements of the ``translator'' are fulfilled if a translator does nothing more than emit appropriate diagnostics -- without taking any additional steps that could possibly lead to an executable program. In short, does a tool such a...
0
8474
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8912
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.