comparing BASH vs. Python script

ORIGINAL SCRIPT: CAT-GREP script

BASH SCRIPT TO remove blank and commented lines and allow to save with date time stamp:

    1  !/bin/bash
     2  ################################################################################
     3  # jm - 24 jun 2016 - cat, grep out blank lines and comments, option to save
     4  ################################################################################
     5  USAGE="catgrep filename # removed commented and blank lines, option to save"
     6  echo "$1"
     7  cat $1 | grep -E -v '(^#|^$)'
     8  echo "save info? Y,y"
     9  read answer
    10  if [ $answer = "y" ]
    11          then
    12          echo "$1"  | tee $1-content-`date +%Y%b%d-%H%M`.txt
    13          cat $1 | grep -E -v '(^#|^$)' | tee -a $1-content-`date +%Y%b%d-%H%M`.txt
    14          else
    15          echo 'thanx for using catgrep! next'
    16  fi
    17  ########################################

BASH script converted to python:
       
    18  #!/usr/bin/env python
    19  ###########################################################################
    20  # USAGE: catgrep filename
    21  # jm - 24 jun 2016 - cat, grep out blank lines and comments, option to save
    22  # mc - 09 dec 2016 - Translated from bash to python. Works on Windows, too.
    23  ###########################################################################
    24  import datetime, os, re, sys
    25  discard = re.compile('^$|^#')
       
    26  f = sys.argv[1]
       
    27  def filter(garbage_in, gospel_out = '/dev/stdout'):
    28    garbage = open(garbage_in, 'r')
    29    gospel = open(gospel_out, 'w')
    30    print >>gospel, garbage_in
    31    for line in garbage:
    32      line = line.rstrip(os.linesep)
    33      if not discard.match(line): print >>gospel, line
    34    gospel.close()
    35    garbage.close()
       
    36  filter(f)
    37  if raw_input('save info? Y,y: ') == 'y':
    38    filter(f, f + '-content-' + datetime.datetime.now().strftime('%Y%b%d-%H%M') + '.txt')      #  JUST LOOK AT THIS SYNTAX!!!!  yikes
    39  else:
    40    print 'thanx for using catgrep! next'
    41  ###########################################################################

stripping comments and blank lines and usage out to compare actual code differences: --> more nl-nc-nl-compare-BASH-python.txt 1 1 !/bin/bash 2 6 echo "$1" 3 7 cat $1 | grep -E -v '(^#|^$)' 4 8 echo "save info? Y,y" 5 9 read answer 6 10 if [ $answer = "y" ] 7 11 then 8 12 echo "$1" | tee $1-content-`date +%Y%b%d-%H%M`.txt 9 13 cat $1 | grep -E -v '(^#|^$)' | tee -a $1-content-`date +%Y%b%d-%H%M`.txt 10 14 else 11 15 echo 'thanx for using catgrep! next' 12 16 fi 13 # ------------------------------------------------------------------------------- 14 18 #!/usr/bin/env python 15 24 import datetime, os, re, sys 16 25 discard = re.compile('^$|^#') 17 26 f = sys.argv[1] 18 27 def filter(garbage_in, gospel_out = '/dev/stdout'): 19 28 garbage = open(garbage_in, 'r') 20 29 gospel = open(gospel_out, 'w') 21 30 print >>gospel, garbage_in 22 31 for line in garbage: 23 32 line = line.rstrip(os.linesep) 24 33 if not discard.match(line): print >>gospel, line 25 34 gospel.close() 26 35 garbage.close() 27 36 filter(f) 28 37 if raw_input('save info? Y,y: ') == 'y': 29 38 filter(f, f + '-content-' + datetime.datetime.now().strftime('%Y%b%d-%H%M') + '.txt') 30 39 else: 31 40 print 'thanx for using catgrep! next'

ebook:
Full Size
Jeep Buyer's
Guide
ebook:
Practical Suggestions
for Microsoft
Windows
Linux Tackles Microsoft
12 hour Video Course by john:
The Art of Linux System Administration
published by O'Reilly Media
Study Guide for the LPIC-2 Certification Exams
search for:
on the internet, or:
JohnMeister.com-fotos
LinuxMeister-Linux
BibleTech-
Bible overview


An overview of Biblical history:
"Promises and Prophets"

Wagoneers

FULL SIZE JEEPS

JeepMeister
"Jeep is America's
only real sports car."
-Enzo Ferrari


MeisterTech
Diesels +

One Page Overview of Linux Commands

click for an image of the 5 essential Linux commands

An Intro to Linux
AMSOIL product guide,
or, AMSOIL web, or 1-800-956-5695,
use customer #283461

Amsoil dealer since 1983
purchase AMSOIL
at Midway Auto on SR9 in Snohomish,
or at Northland Diesel in Bellingham, WA


SJ - 1962-1991

XJ - 1984-2001

WJ - 1999-2004

KJ - 2002-2007

WK - 2005-2010

Find the recommended
AMSOIL synthetics
for your Jeep

CJ-10A - 1984-1986

Jeepsters

MJ - 1984-1992

Willys - 1946-1965

Other Jeeps (FC)