Optimizing bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimizing bash script
# 1  
Old 06-14-2016
Optimizing bash script

any way the following code can be optimized?

Code:
FIRSTIN=$(
        HKIPP=$(echo ${TMFR} | egrep -v "mo|MO|Mo" | egrep "m |M ")
        HRAMH=$(echo ${TMFR} | egrep "h|H")
        HRAMD=$(echo ${TMFR} | egrep "d|D")
        HRAMW=$(echo ${TMFR} | egrep "w|W")
        HKIPPO=$(echo ${TMFR} | egrep "mo|MO|Mo")
        if [ -z "${HRAMH}" ] && [ -z "${HRAMD}" ] && [ -z "${HRAMW}" ] && [ -z "${HKIPP}" ] && [ -z "${HKIPPO}" ] ; then
                echo $TMFR | sed 's~[hHmM]~~g' | gawk '{print $1 * 60}'
        elif [ ! -z "${HKIPP}" ] ; then
                echo $TMFR | sed 's~[hHmM]~~g' | gawk '{print $1 * 60}'
        elif [ ! -z "${HRAMH}" ] ; then
                echo $TMFR | sed 's~[hHmM]~~g' | gawk '{print $1 * 60 * 60}'
        elif [ ! -z "${HRAMD}" ] ; then
                echo $TMFR | sed 's~[dD]~~g' | gawk '{print $1 * 1440 * 60}'
        elif [ ! -z "${HRAMW}" ] ; then
                echo $TMFR | sed 's~[wW]~~g' | gawk '{print $1 * 10080 * 60}'
        elif [ ! -z "${HKIPPO}" ] ; then
                echo $TMFR | sed 's~[mo]~~g' | sed 's~[MO]~~g' | sed 's~[Mo]~~g' | gawk '{print $1 * 43200 * 60}'
        else
                echo $TMFR | gawk '{print $1 * 60}'
fi)

preferably in awk?
# 2  
Old 06-14-2016
Hi,

can you post sample input + output files? That will
make it easier.

stomp();

Here's some bash code, which should be a lot faster, because only shell builtins are used. Can be better if you bring samples(in/out) and explain them.

Code:
#!/bin/bash

shopt -s nocasematch 

NUMBER=${TMFR//[wdhmoWDHMO]/}

# default multiplier for MINUTES
MULTIPLIER=60

# order of the checks matters!
[[ "$TMFR" =~ mo ]] && MULTIPLIER=2592000 # MONTH
[[ "$TMFR" =~ w  ]] && MULTIPLIER=604800  # WEEK
[[ "$TMFR" =~ d  ]] && MULTIPLIER=86400   # DAY
[[ "$TMFR" =~ h  ]] && MULTIPLIER=3600    # HOUR

((FIRSTIN= $NUMBER * $MULTIPLIER))

echo $FIRSTIN $NUMBER $MULTIPLIER


Last edited by stomp; 06-14-2016 at 06:38 PM..
This User Gave Thanks to stomp For This Post:
# 3  
Old 06-14-2016
Quote:
Originally Posted by stomp
Hi,

can you post sample input + output files? That will
make it easier.

stomp();

Here's some bash code, which should be a lot faster, because only shell builtins are used. Can be better if you bring samples(in/out) and explain them.

Code:
#!/bin/bash

shopt -s nocasematch 

NUMBER=${TMFR//[wdhmoWDHMO]/}

# default multiplier for MINUTES
MULTIPLIER=60

# order of the checks matters!
[[ "$TMFR" =~ mo ]] && MULTIPLIER=2592000 # MONTH
[[ "$TMFR" =~ w  ]] && MULTIPLIER=604800  # WEEK
[[ "$TMFR" =~ d  ]] && MULTIPLIER=86400   # DAY
[[ "$TMFR" =~ h  ]] && MULTIPLIER=3600    # HOUR

((FIRSTIN= $NUMBER * $MULTIPLIER))

echo $FIRSTIN $NUMBER $MULTIPLIER

thank you so much for this.
one question, this uses the newer functions of bash, i dont think it would work on the old bash that uses sh. know how to make this more portable?
# 4  
Old 06-15-2016
Quote:
Originally Posted by SkySmart
thank you so much for this.
one question, this uses the newer functions of bash, i dont think it would work on the old bash that uses sh. know how to make this more portable?
Hello SkySmart,

Here comes the code re-usability by using function, as requested by Stomp you haven't shown us the Input_file so can't predict your exact requirement. Based on your shown Input_file in POST#1, could you please try following and let me know if this helps you.
You could basically change following commands(shown by you in POST#1):
Code:
echo $TMFR | sed 's~[hHmM]~~g' | gawk '{print $1 * 60}'
echo $TMFR | sed 's~[hHmM]~~g' | gawk '{print $1 * 60}'
echo $TMFR | sed 's~[hHmM]~~g' | gawk '{print $1 * 60 * 60}'
echo $TMFR | sed 's~[dD]~~g' | gawk '{print $1 * 1440 * 60}'
echo $TMFR | sed 's~[wW]~~g' | gawk '{print $1 * 10080 * 60}'
echo $TMFR | sed 's~[mo]~~g' | sed 's~[MO]~~g' | sed 's~[Mo]~~g' | gawk '{print $1 * 43200 * 60}'

To a single command:
Code:
echo $TMFR | awk 'function valuecal(Q,tmfr){gsub(/Q/,X,tmfr);val=$1 * 60;return val} {VAL=valuecal("[hHmM]",$0);print "Minutes to seconds= " OFS  VAL ORS "hours to seconds= " OFS VAL * 60;DATE_VAL=valuecal("[dD]",$0);print DATE_VAL * 1440;wW_VAL=valuecal("[wW]",$0);print wW_VAL * 10080;mo_VAL=valuecal("[mMoO]",$0;print mo_VAL * 43200}'

As I have mentioned earlier above code will do all the work but I couldn't test it as lack of Input_file.
So in case you want to take values of hHmM, dD, wW, mMoO differently then following you could try.
Code:
For hHmM:
echo $TMFR | awk 'function valuecal(tmfr){gsub(/[hHmM]/,X,tmfr);val=$1 * 60;return val} {VAL=valuecal($0);print "Minutes to seconds= " OFS  VAL ORS "hours to seconds= " OFS VAL * 60}'
For dD:
echo $TMFR | awk 'function valuecal(tmfr){gsub(/[dD]/,X,tmfr);val=$1 * 60;return val} {VAL=valuecal($0);print VAL * 1440}'
For wW:
echo $TMFR | awk 'function valuecal(tmfr){gsub(/[wW]/,X,tmfr);val=$1 * 60;return val} {VAL=valuecal($0);print VAL * 10080}'
For mMoO:
echo $TMFR | awk 'function valuecal(tmfr){gsub(/[mMoO]/,X,tmfr);val=$1 * 60;return val} {VAL=valuecal($0);print VAL * 43200}'

Please do try above codes as per your requirements and do let us know if you have any queries on same, hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 06-15-2016
Without digging deeper into the logics, wouldn't it make sense to deploy the case ... esac construct? Should be available in sh as well...
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-15-2016
Yes a case is ideal here. And portable to other shells.
For extracting numbers there is some Posix shell builtins that need more assumptions, like "the digits are always at the end of the string".
For old Bourne shells, and without such assumptions, one needs an external helper, here expr.
Code:
case $TMFR in
*[Mm][Oo]*) factor=2592000;;
*[Ww]*) factor=604800;;
*[Dd]*) factor=86400;;
*[Hh]*) factor=3600;;
*) factor=60;;
esac
num=`expr x"$TMFR" : x"[^0-9]*\([0-9]*\)"`
FIRSTIN=`expr 0$num \* $factor`

Since expr bails out if the first character in $TMFR is a dash, the usual work-around is to prepend a character (here an x) that is normally repeated on the right side (here for clarity, would be absorbed by the [^0-9]).
The 0 is prepended to $num, so in case it is empty the result is 0.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Optimizing JS and CSS

Yes. Got few suggestions. - How about minifying resources - mod_expires - Service workers setup https://www.unix.com/attachments/web-programming/7709d1550557731-sneak-preview-new-unix-com-usercp-vuejs-demo-screenshot-png (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

2. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

3. Shell Programming and Scripting

Optimizing bash loop

now, i have to search for a pattern within a particular time frame which the user will provide in the following format: 19/Jun/2018:07:04,21/Jun/2018:21:30 it is easy to get tempted to attempt this search with a variation of the following awk command: awk... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

Optimizing the Shell Script [Expert Advise Needed]

I have prepared a shell script to find the duplicates based on the part of filename and retain latest. #!/bin/bash if ; then mkdir -p dup fi NOW=$(date +"%F-%H:%M:%S") LOGFILE="purge_duplicate_log-$NOW.log" LOGTIME=`date "+%Y-%m-%d %H:%M:%S"` echo... (6 Replies)
Discussion started by: gold2k8
6 Replies

5. Shell Programming and Scripting

Optimizing script to reduce execution time

AFILENAME=glow.sh FILENAME="/${AFILENAME}" WIDTHA=$(echo ${FILENAME} | wc -c) NTIME=0 RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1) do WIDTHTIMES=$(awk "BEGIN{printf... (5 Replies)
Discussion started by: SkySmart
5 Replies

6. Shell Programming and Scripting

Optimizing awk script

Can this awk statement be optimized? i ask because log.txt is a giant file with several hundred thousands of lines of records. myscript.sh: while read line do searchterm="${1}" datecurr=$(date +%s) file=$(awk 'BEGIN{split(ARGV,var,",");print var}' $line) ... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Shell Programming and Scripting

Optimizing the code

Hi, I have two files in the format listed below. I need to find out all values from field 12 to field 20 present in file 2 and list them in file3(format as file2) File1 : FEIN,CHRISTA... (2 Replies)
Discussion started by: nua7
2 Replies

8. Shell Programming and Scripting

Need help optimizing this piece of code (Shell script Busybox)

I am looking for suggestions on how I could possibly optimized that piece of code where most of the time is spend on this script. In a nutshell this is a script that creates an xml file(s) based on certain criteria that will be used by a movie jukebox. Example of data: $SORTEDTMP= it is a... (16 Replies)
Discussion started by: snappy46
16 Replies

9. OS X (Apple)

Optimizing OSX

Hi forum, I'm administrating a workstation/server for my lab and I was wondering how to optimize OSX. I was wondering what unnecessary background tasks I could kick off the system so I free up as much memory and cpu power. Other optimization tips are also welcome (HD parameters, memory... (2 Replies)
Discussion started by: deiphon
2 Replies

10. UNIX and Linux Applications

Optimizing query

Hi All, My first thread to this sub-forum and first thread of this sub-forum :) Here it is, Am trying to delete duplicates from a table retaining just 1 duplicate value out of the duplicate records for example : from n records of a table out of which x are duplicates, I want to remove x... (15 Replies)
Discussion started by: matrixmadhan
15 Replies
Login or Register to Ask a Question