comparing BASH vs. Python scriptORIGINAL SCRIPT: CAT-GREP scriptBASH 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 ########################################################################### |
The Art of Linux System Administration published by O'Reilly Media Study Guide for the LPIC-2 Certification Exams |
Wagoneers FULL SIZE JEEPS JeepMeister "Jeep is America's -Enzo Ferrari MeisterTech Diesels + |
One Page Overview of Linux Commands click for an image of the 5 essential Linux commands An Intro to Linux |
at Midway Auto on SR9 in Snohomish, or at Northland Diesel in Bellingham, WA |