Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

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

I've seen mentioned a few places that python is good for this kind of thing. How can I use Python to replace shell scripting, AWK, sed and friends?

1 Answer

0 votes
by (106k points)

You can use any shell that has several sets of features to replace shell scripting, AWK, sed:-

  • All the essential Linux/Unix commands are available through the subprocess library. You can also use shutil for some commands that are separate Linux commands, but you could probably implement directly in your Python scripts. 

  • Some of the specific programs (awk, sed, grep, etc.) can often be rewritten as Python modules. The best thing you can do is as follows.

  1. You can replace AWK and PERL with Python and  Leave everything else alone.

  2. Also, look at replacing GREP with Python. This replacement can be a bit more complex, but in your version of GREP, it can be tailored to your processing needs.

  3. Try to 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. Also, look at replacing common shell logic (loops, decisions, etc.) with Python scripts.

Related questions

Browse Categories