Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

a.sh

cat in|while read s
do call "$s"
done

This needs to be modified so it runs only for arg+n lines. e.g.: a.sh 5

    cat in|while read s
n=10
for lines arg0 through arg0+n
    do call "$s" 
    done
share|improve this question

2 Answers 2

up vote 0 down vote accepted

It'll be faster & cleaner to filter the input down to just the relevant lines before feeding it to the read loop:

n=10
start=$1
end=$((start+n))
cat in | sed "${start},${end}!d" | while read s; do
    call "$s"
done

Note: printing lines $1 through $1+n lines will actually print n+1 lines (e.g. printing lines 5 through 15 is actually 11 lines). If you want to print n lines starting at $1, use end=$((start+n-1)).

share|improve this answer
    
Would cat in | sed "${start},${end}!d" be faster than sed -n $1,+$np in? –  user93200 Sep 28 '11 at 19:22
    
@user93200: I did some quick tests, and it seems to be the same speed. What would help is replacing the useless use of cat: sed "${start},${end}!d" <in | ... (unless, of course, cat in is just a placeholder for something less trivial). –  Gordon Davisson Sep 29 '11 at 0:15
lines=10
current=0
while read line; do
   current=current+1
   if [ "$current" -gt "$lines" ] then exit 0; fi
   if [ "$current" -gt "$1" ] then call "$line"; fi
done < in
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.