-1

I'm used to writing a lot of AWK commands, but, always wanted to use Python to do the same stuff that i do with Awk without writing a .py file. I have basics in the Python, but not in the command line.

For example I have the following AWK command. What is the equivalent for python?

awk '/Classic.java/ {print $0}' somefile.log;
Zeus
  • 6,386
  • 6
  • 54
  • 89
  • 1
    is `python -c "print(expression)"` the sort of thing you want? – tew Aug 05 '15 at 19:57
  • @FoxWilson I'm trying to understand how to do basic search and word processing on log files using python. See my update, that is how the awk will search and print the matching line from the file. – Zeus Aug 05 '15 at 19:59
  • I'm voting to close this question as off-topic because SO is not a code translation service or a tutorial service. – TigerhawkT3 Aug 05 '15 at 20:00
  • 1
    ....I'm trying to come up with good way to do what you want, but `somefile.log> python3 -c "import re; import sys; [print(i) for i in sys.stdin if re.search('Classic.java', i)]"` requires piping, and looks real silly :P. Just use awk, python's not really meant for stuff like this. – NightShadeQueen Aug 05 '15 at 20:10
  • @NightShadeQueen thank you much for the reply, that is what i've expected, i've written a python something like that, was looking for ways to use python in these type of stuff as i hear it is versatile, and it was lengthy compared to awk or sed. – Zeus Aug 05 '15 at 20:18
  • 1
    Think of the command line as a specialized IDE, and the question as a request to find a convenient, unix-like way to use python via that IDE. The pyline / pyp answer is exactly that - letting programmers easily use their knowledge of python to filter or mapreduce stuff via the command line. – nealmcb Oct 05 '15 at 18:58

1 Answers1

5

Python is not very suitable for shell one-liners. You could play with The Pyed Piper:

$ ls | pyp "p[0] | pp.sort() | p + ' first letter, sorted!'"
  # it gives sorted list of first letters of every line

It uses standard Python string and list methods as well as custom functions. There is also pyline:

$ ls | pyline -m os 'line and os.path.abspath(line.strip())'
$ ls | pyline -r '\(.*\)' 'rgx and (rgx.group(0), rgx.group(1)) or line'
$ ls | pyline -p 'p and p.abspath() or ("# ".format(line))'

Another alternative is to use ipython as a shell or a browser-based notebook (recommended). Or if you want more BASHwards-looking syntax and tab completion for subprocess commands; try xonsh as your shell:

xonsh$ [i*i for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
xonsh$ date -u
Tue Oct  6 04:25:27 UTC 2015

subprocess and its alternatives (plumbum, pexpect, sarge, sh (module), fabric) allow you to create arbitrary complex commands by exploiting the best of python and bash.

There are also several nice python -m one-liners e.g.:

$ python3 -m http.server # serve current directory over http
$ python -m zipfile # work with zipfiles
$ python -m calendar # show calendar
$ python -m telnetlib towel.blinkenlights.nl # Star Wars
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • You made the question a helpful, worthy question via this epic answer (truly, given the ability to watch Star Wars via python on the console)! – nealmcb Oct 05 '15 at 19:02