Looking for optimization advice on a short script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for optimization advice on a short script
# 1  
Old 03-20-2010
Looking for optimization advice on a short script

I already have a solution to my problem, but I'm looking to see if it can be made more succinct and faster. The problem: given a list, as shown below, extract the pathname for any file in a directory named '[Ss]ample' and return it's index into the list. The index is also in the data itself. Note that pathnames can contain spaces. I'm also trying to avoid non-POSIX syntax and tools.

Code:
#!/bin/sh

list=$(cat <<'_EOF_'
Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV (28 files):
  #  Done Priority Get      Size  Name
  0: 100% Normal   Yes    8.4 MB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/Sample/sample-leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.avi
  1: 100% Normal   Yes    4.6 KB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.nfo
 26: 100% Normal   Yes    9.4 MB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.part25.rar
 27: 100% Normal   Yes    1.8 KB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.sfv
_EOF_
)

index=-1
for file in $(
    # The echo emulates the call to the external program.
    echo "$list" | grep '^ *[0-9]\+:' | cut -c 35-
)
do
    index=$((1 + $index))
    case "$file" in
        */[Ss]ample/*)
            # act upon this file
            echo "do something with $index"
            ;;
    esac
done


# 2  
Old 03-20-2010
Quote:
Originally Posted by prigo
I already have a solution to my problem, but I'm looking to see if it can be made more succinct and faster. The problem: given a list, as shown below, extract the pathname for any file in a directory named '[Ss]ample' and return it's index into the list. The index is also in the data itself. Note that pathnames can contain spaces. I'm also trying to avoid non-POSIX syntax and tools.

Code:
#!/bin/sh

list=$(cat <<'_EOF_'
Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV (28 files):
  #  Done Priority Get      Size  Name
  0: 100% Normal   Yes    8.4 MB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/Sample/sample-leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.avi
  1: 100% Normal   Yes    4.6 KB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.nfo
 26: 100% Normal   Yes    9.4 MB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.part25.rar
 27: 100% Normal   Yes    1.8 KB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.sfv
_EOF_
)


No need for cat:
Code:
list='Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV (28 files):
  #  Done Priority Get      Size  Name
  0: 100% Normal   Yes    8.4 MB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/Sample/sample-leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.avi
  1: 100% Normal   Yes    4.6 KB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.nfo
 26: 100% Normal   Yes    9.4 MB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.part25.rar
 27: 100% Normal   Yes    1.8 KB  Leigh.Harts.Mysterious.Planet.S01E06.PDTV.XviD-FiHTV/leigh.harts.mysterious.planet.s01e06.pdtv.xvid-fihtv.sfv'

Quote:
Code:
index=-1
for file in $(
    # The echo emulates the call to the external program.
    echo "$list" | grep '^ *[0-9]\+:' | cut -c 35-
)
do
    index=$((1 + $index))
    case "$file" in
        */[Ss]ample/*)
            # act upon this file
            echo "do something with $index"
            ;;
    esac
done


No need for grep or cut:
Code:
while read a b c d e file g
do
  index=$((1 + $index))
  case $file in
     */[Ss]ample/*)
         # act upon this file
         echo "do something with $index"
         ;;
  esac
done <<.
$list
.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Submitting awk script into cluster short.q

Hello, I want to submit my awk script into cluster queue as my job takes about forty minutes to finish so I can not run it on the main node. My awk script is like the following and I have three files. so, I write : qsub -q short.q Myscript.awk file1 file2 file3 It submits the work into... (1 Reply)
Discussion started by: Homa
1 Replies

2. Shell Programming and Scripting

Script Optimization - large delimited file, for loop with many greps

Since there are approximately 75K gsfiles and hundreds of stfiles per gsfile, this script can take hours. How can I rewrite this script, so that it's much faster? I'm not as familiar with perl but I'm open to all suggestions. ls file.list>$split for gsfile in `cat $split`; do csplit... (17 Replies)
Discussion started by: verge
17 Replies

3. Shell Programming and Scripting

translate a short csh script to bash

Hi, I have a csh: set NODES = `cat $HOST_FILE` set NODELIST = $TMPDIR/namd2.nodelist echo group main >! $NODELIST foreach node ( $NODES ) echo host $node >> $NODELIST end @ NUMPROCS = 2 * $#NODES I am very frustrated to translate it to bash: NODES = `cat... (3 Replies)
Discussion started by: rockytodd
3 Replies

4. Shell Programming and Scripting

Awk script gsub optimization

I have created Shell script with below awk code for replacing special characters from input file. Source file has 6 mn records. This script was able to handle 2 mn records in 1 hr. This is very slow speed and we need to optimise our processing. Can any Guru help me for optimization... (6 Replies)
Discussion started by: Akshay
6 Replies

5. UNIX Desktop Questions & Answers

Script that will display a short message

Can anyone point me to the right direction on how to write a simple script that will display a message on any terminal when implemented? Basically I need it so the script runs at a certain time, say April 30, 2010 and that the message will be displayed to me no matter which terminal I am logged... (2 Replies)
Discussion started by: jmack123
2 Replies

6. UNIX for Dummies Questions & Answers

unix script optimization

I have a file which contains 9,200,000. It contains 125 clolumns. I have to rearrange some columns and exclude some of them. I scripted the following script to do the same. It is working fine but it is taking more than 4hrs to do it. can it be optmized. Here is the script LOC="/sourcefile/"... (3 Replies)
Discussion started by: max_payne1234
3 Replies

7. Shell Programming and Scripting

Script Optimization required

Dear All, Sorry to bother you. But I tried the below problem but didn't come up a good solution. A have a file containing such info 2009-03-14 22:01:01,430 :: 2009-03-14 22:05:01,430 :: I need to show simply 22:01:01, 568, 181, 472 22:05:01, 903, 458, 572 that is time, TID,... (11 Replies)
Discussion started by: saifurshaon
11 Replies

8. Shell Programming and Scripting

script optimization

:o Hi, I am writing a script in which at some time, I need to get the process id of a special process and kill it... I am getting the PID as follows... ps -ef | grep $PKMS/scripts | grep -v grep | awk '{print $2 }'can we optimize it more further since my script already doing lot of other... (3 Replies)
Discussion started by: vivek.gkp
3 Replies

9. UNIX for Dummies Questions & Answers

Help on optimization of the script

Hi, I have prepared script which is taking more time to process. find below script and help me with fast optimized script:- cat name.txt | while read line do name=$(echo $line| awk '{print $8}') MatchRecord=$(grep $name abc.txt | grep -v grep ) echo "$line | $MatchRecord" | awk... (2 Replies)
Discussion started by: aju_kup
2 Replies

10. Shell Programming and Scripting

short script help

how do i find out the date of the last time the system was last booted from? (8 Replies)
Discussion started by: jodders
8 Replies
Login or Register to Ask a Question