我目前通过一堆不好记的AWK、sed、Bash和一小部分Perl来做我的文本文件操作。
我已经看到提到的一些地方,python很适合做这种事情。我如何使用Python来代替shell脚本,AWK, sed和朋友?
我目前通过一堆不好记的AWK、sed、Bash和一小部分Perl来做我的文本文件操作。
我已经看到提到的一些地方,python很适合做这种事情。我如何使用Python来代替shell脚本,AWK, sed和朋友?
当前回答
Pythonpy是一个工具,可以方便地访问awk和sed的许多特性,但使用python语法:
$ echo me2 | py -x 're.sub("me", "you", x)'
you2
其他回答
我在PyPI: ez上发布了一个包。 使用pip install ez进行安装。
它在shell中打包了通用命令,我的库使用了与shell基本相同的语法。例如,cp(源,目标)可以同时处理文件和文件夹!(书纸的包装。shutil副本。Copytree,它决定什么时候使用哪个)。更妙的是,它可以支持像R!
另一个例子:没有os。Walk,使用fls(path, regex)递归地查找文件并使用正则表达式进行过滤,它将返回带有或没有全路径的文件列表
最后一个例子:你可以结合它们来编写非常简单的脚本: Files = fls('.','py$');文件,myDir (cp)
一定要去看看!我花了几百个小时来编写/改进它!
In the beginning there was sh, sed, and awk (and find, and grep, and...). It was good. But awk can be an odd little beast and hard to remember if you don't use it often. Then the great camel created Perl. Perl was a system administrator's dream. It was like shell scripting on steroids. Text processing, including regular expressions were just part of the language. Then it got ugly... People tried to make big applications with Perl. Now, don't get me wrong, Perl can be an application, but it can (can!) look like a mess if you're not really careful. Then there is all this flat data business. It's enough to drive a programmer nuts.
进入Python、Ruby等。这些都是非常好的通用语言。它们支持文本处理,并且做得很好(尽管可能与语言的基本核心没有那么紧密地交织在一起)。但它们也可以很好地扩展,并且在一天结束时仍然有漂亮的代码。他们还发展了相当庞大的社区,有大量的图书馆,几乎可以提供任何东西。
Now, much of the negativeness towards Perl is a matter of opinion, and certainly some people can write very clean Perl, but with this many people complaining about it being too easy to create obfuscated code, you know some grain of truth is there. The question really becomes then, are you ever going to use this language for more than simple bash script replacements. If not, learn some more Perl.. it is absolutely fantastic for that. If, on the other hand, you want a language that will grow with you as you want to do more, may I suggest Python or Ruby.
不管怎样,祝你好运!
最好的选择是专门针对您的问题的工具。如果它正在处理文本文件,那么Sed、Awk和Perl是最佳竞争者。Python是一种通用动态语言。与任何通用语言一样,它支持文件操作,但这不是它的核心目的。如果我特别需要一种动态语言,我会考虑Python或Ruby。
简而言之,非常好地学习Sed和Awk,以及所有其他随*nix风格而来的好东西(所有Bash内置,grep, tr等等)。如果您感兴趣的是文本文件处理,那么您已经使用了正确的方法。
我喜欢Python的一个原因是它比POSIX工具更加标准化。我必须反复检查每个比特是否与其他操作系统兼容。在Linux系统上编写的程序在OSX的BSD系统上可能无法正常工作。对于Python,我只需要检查目标系统是否有足够现代的Python版本。
更棒的是,用标准Python编写的程序甚至可以在Windows上运行!
我已经构建了半长的shell脚本(300-500行)和Python代码,它们具有类似的功能。当执行许多外部命令时,我发现shell更容易使用。当有大量文本操作时,Perl也是一个很好的选择。