我目前通过一堆不好记的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上运行!
其他回答
Pythonpy是一个工具,可以方便地访问awk和sed的许多特性,但使用python语法:
$ echo me2 | py -x 're.sub("me", "you", x)'
you2
在研究这个主题时,我发现了这个概念验证代码(通过http://jlebar.com/2010/2/1/Replacing_Bash.html上的评论),它让你“使用简洁的语法在Python中编写类似shell的管道,并在有意义的地方利用现有的系统工具”:
for line in sh("cat /tmp/junk2") | cut(d=',',f=1) | 'sort' | uniq:
sys.stdout.write(line)
是的,当然。
看看这些库,它们可以帮助你不再编写shell脚本(Plumbum的座右铭)。
铅 军士 上海
另外,如果你想用基于Python的东西替换awk, sed和grep,那么我推荐pyp -
“The Pyed Piper”,或pyp,是一个linux命令行文本操作 工具类似于awk或sed,但使用标准的python字符串和 列表方法以及自定义函数进化为快速生成 在紧张的生产环境中产生的结果。
截至2015年和Python 3.4的发布,现在有一个相当完整的用户交互shell: http://xon.sh/或https://github.com/scopatz/xonsh
演示视频没有显示正在使用的管道,但是在默认shell模式下支持管道。
Xonsh(“conch”)非常努力地模仿bash,因此您已经获得了肌肉记忆,例如
env | uniq | sort -r | grep PATH
or
my-web-server 2>&1 | my-log-sorter
仍然可以正常工作。
本教程相当冗长,似乎涵盖了人们通常在ash或bash提示符时所期望的大量功能:
Compiles, Evaluates, & Executes! Command History and Tab Completion Help & Superhelp with ? & ?? Aliases & Customized Prompts Executes Commands and/or *.xsh Scripts which can also be imported Environment Variables including Lookup with ${} Input/Output Redirection and Combining Background Jobs & Job Control Nesting Subprocesses, Pipes, and Coprocesses Subprocess-mode when a command exists, Python-mode otherwise Captured Subprocess with $(), Uncaptured Subprocess with $[], Python Evaluation with @() Filename Globbing with * or Regular Expression Filename Globbing with Backticks
我刚刚发现了如何结合bash和ipython的最佳部分。到目前为止,这似乎比使用subprocess等更舒服。你可以很容易地复制现有bash脚本的大部分内容,例如以python的方式添加错误处理:) 这是我的结果:
#!/usr/bin/env ipython3
# *** How to have the most comfort scripting experience of your life ***
# ######################################################################
#
# … by using ipython for scripting combined with subcommands from bash!
#
# 1. echo "#!/usr/bin/env ipython3" > scriptname.ipy # creates new ipy-file
#
# 2. chmod +x scriptname.ipy # make in executable
#
# 3. starting with line 2, write normal python or do some of
# the ! magic of ipython, so that you can use unix commands
# within python and even assign their output to a variable via
# var = !cmd1 | cmd2 | cmd3 # enjoy ;)
#
# 4. run via ./scriptname.ipy - if it fails with recognizing % and !
# but parses raw python fine, please check again for the .ipy suffix
# ugly example, please go and find more in the wild
files = !ls *.* | grep "y"
for file in files:
!echo $file | grep "p"
# sorry for this nonsense example ;)
请参阅IPython文档,了解系统shell命令并将其用作系统shell。