我可以使用Python作为替代猛砸? [英] Can I use Python as a Bash replacement?

查看:140
本文介绍了我可以使用Python作为替代猛砸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过一束想起严重的AWK,sed的做我的文本文件操作,Bash和Perl中的一点点。

I currently do my textfile manipulation through a bunch of badly remembered AWK, sed, Bash and a tiny bit of Perl.

我见过提到了几个地方Python是很好的这种事情,我知道一点,我想知道更多。 Python的是这一个不错的选择,有一本好书或指导,学习如何使用Python来代替shell脚本,AWK,sed和朋友吗?

I've seen mentioned a few places that python is good for this kind of thing, I know a little and I would like to know more. Is Python a good choice for this, and is there a good book or guide to learning how to use Python to replace shell scripting, AWK, sed and friends?

推荐答案

任何外壳拥有多套功能。

Any shell has several sets of features.


  • 的基本的Linux / Unix命令。所有这些都可以通过库。这并不总是这样做的所有的外部命令的最佳首选。还看 shutil 了解一些命令是独立的Linux命令,但你很可能在你的Python直接实现脚本。另一个巨大的批处理命令的Linux都在 OS 库;你可以做这些更简单的Python。

  • The Essential Linux/Unix commands. All of these are available through the subprocess library. This isn't always the best first choice for doing all external commands. Look also at shutil for some commands that are separate Linux commands, but you could probably implement directly in your Python scripts. Another huge batch of Linux commands are in the os library; you can do these more simply in Python.

和 - 红利! - 更快速。在外壳(除了少数例外)每个单独的Linux命令派生一个子进程。通过使用Python shutil 操作系统模块,你不叉一子。

And -- bonus! -- more quickly. Each separate Linux command in the shell (with a few exceptions) forks a subprocess. By using Python shutil and os modules, you don't fork a subprocess.

shell环境的功能。这包括东西,设置命令的环境(当前目录和环境变量,什么而不是)。您可以轻松地从Python的直接管理这一点。

The shell environment features. This includes stuff that sets a command's environment (current directory and environment variables and what-not). You can easily manage this from Python directly.

在shell编程功能。这是所有的进程状态code检查,各种逻辑命令(如,同时,对等),测试命令和它的所有亲戚。函数定义的东西。这是所有的Python得多,容易得多。这是摆脱庆典,并在Python做了巨大的胜利之一。

The shell programming features. This is all the process status code checking, the various logic commands (if, while, for, etc.) the test command and all of it's relatives. The function definition stuff. This is all much, much easier in Python. This is one of the huge victories in getting rid of bash and doing it in Python.

互动功能。这包括命令历史,什么,而不是。你不需要这个编写shell脚本。这仅仅是人际交往,而不是剧本写作。

Interaction features. This includes command history and what-not. You don't need this for writing shell scripts. This is only for human interaction, and not for script-writing.

外壳文件管理功能。这包括重定向和管道。这是棘手。这在很大程度上可以与子来完成。但有些事情,在外壳很容易在Python中的不愉快。具体的东西,如(A | B; C)|什么>结果。此并行运行两个进程(与的输出作为输入 B )中,随后的第三过程。从该序列的输出与的东西并行运行,输出被收集到一个名为结果文件。这只是复杂,任何其他语言的前preSS。

The shell file management features. This includes redirection and pipelines. This is trickier. Much of this can be done with subprocess. But some things that are easy in the shell are unpleasant in Python. Specifically stuff like (a | b; c ) | something >result. This runs two processes in parallel (with output of a as input to b), followed by a third process. The output from that sequence is run in parallel with something and the output is collected into a file named result. That's just complex to express in any other language.

具体方案(AWK,sed的中,grep等),通常可以改写为Python模块。不要走极端。更换你的需要和发展你的grep模块。不要开始就写一个Python模块,取代grep的。

Specific programs (awk, sed, grep, etc.) can often be rewritten as Python modules. Don't go overboard. Replace what you need and evolve your "grep" module. Don't start out writing a Python module that replaces "grep".

最好的事情是,你可以在步骤做到这一点。

The best thing is that you can do this in steps.


  1. 使用Python替换AWK和Perl。别的先不谈一切。

  2. 看看与Python更换GREP。这可能是更复杂一点,但你的牛瘟的版本可以根据您的加工需求。

  3. 看看与Python更换FIND循环使用的 os.walk 。这是一个巨大的胜利,因为你不产卵多个进程。

  4. 看一下使用Python脚本取代普通外壳逻辑(循环,决定等)。

  1. Replace AWK and PERL with Python. Leave everything else alone.
  2. Look at replacing GREP with Python. This can be a bit more complex, but your version of GREP can be tailored to your processing needs.
  3. Look at replacing FIND with Python loops that use os.walk. This is a big win because you don't spawn as many processes.
  4. Look at replacing common shell logic (loops, decisions, etc.) with Python scripts.

这篇关于我可以使用Python作为替代猛砸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆