我目前通过一堆不好记的AWK、sed、Bash和一小部分Perl来做我的文本文件操作。
我已经看到提到的一些地方,python很适合做这种事情。我如何使用Python来代替shell脚本,AWK, sed和朋友?
我目前通过一堆不好记的AWK、sed、Bash和一小部分Perl来做我的文本文件操作。
我已经看到提到的一些地方,python很适合做这种事情。我如何使用Python来代替shell脚本,AWK, sed和朋友?
当前回答
我喜欢Python的一个原因是它比POSIX工具更加标准化。我必须反复检查每个比特是否与其他操作系统兼容。在Linux系统上编写的程序在OSX的BSD系统上可能无法正常工作。对于Python,我只需要检查目标系统是否有足够现代的Python版本。
更棒的是,用标准Python编写的程序甚至可以在Windows上运行!
其他回答
我建议你看一本很棒的在线书籍Dive Into Python。这就是我最初学习这门语言的方式。
除了教你语言的基本结构和大量有用的数据结构之外,它还有一个关于文件处理的很好的章节,以及关于正则表达式等的后续章节。
如果您想使用Python作为shell,为什么不看看IPython呢?用互动的方式学习语言也很好。 如果需要进行大量的文本操作,并且使用Vim作为文本编辑器,那么还可以直接用python为Vim编写插件。只需在Vim中输入“:help python”,然后按照说明操作,或者看看这个演示文稿。它是如此简单和强大的编写函数,你将直接在你的编辑器中使用!
是的,当然。
看看这些库,它们可以帮助你不再编写shell脚本(Plumbum的座右铭)。
铅 军士 上海
另外,如果你想用基于Python的东西替换awk, sed和grep,那么我推荐pyp -
“The Pyed Piper”,或pyp,是一个linux命令行文本操作 工具类似于awk或sed,但使用标准的python字符串和 列表方法以及自定义函数进化为快速生成 在紧张的生产环境中产生的结果。
添加到前面的答案:检查pexpect模块处理交互式命令(adduser, passwd等)
任何shell都有几组特性。
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. 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. 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. 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. 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. 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”模块。不要一开始就编写一个替换“grep”的Python模块。
最好的事情是你可以一步一步来做。
用Python替换AWK和PERL。不要管其他的事情。 看看用Python替换GREP。这可能有点复杂,但是您的GREP版本可以根据您的处理需求进行定制。 看看用使用os.walk的Python循环替换FIND。这是一个巨大的胜利,因为您不需要生成那么多的进程。 看看用Python脚本替换常见的shell逻辑(循环、决策等)。