Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

All what i want is to specify a certain number of line like this lineNumberIs=3 and to tell while read to start out of this third line or what ever line number and fetching lines after wards to execute some commands later on the fetched lines Something like that

 while read line from $lineNumberIs
    do
    **some commands not just echo nor printing on the screen** 
    done < $dataFile
share|improve this question
1  
And a counter would not suffice? –  Rinzwind May 1 '13 at 17:15
    
u mean something like for loop and counter , if that so , no sir it wont be sufficient –  Ahmed Zain El Dein May 1 '13 at 17:35

2 Answers 2

up vote 3 down vote accepted
while read line; do
    # ...
done < <(tail -n "+$lineNumberIs" $dataFile)

tail -n +K (with the plus sign) tells tail to start at the specified line number (see the man page).

The <(...) bit is a process substitution. It lets you specify a command sequence and let bash read from it like a file. It's very handy when you want to avoid the effect of the subshell created in a pipeline.

share|improve this answer
    
thank u :) u mean that it is handy if i want to avoid the waste resources on reading those N unnecessary lines and start directly reading from this specific line number , did i understand u right ? –  Ahmed Zain El Dein May 2 '13 at 11:39
    
Consider this: x=foo; echo bar | read x; echo $x -- Would you be surprised that it outputs "foo"? If you wanted to see "bar" you would write x=foo; read x < <(echo bar); echo $x –  glenn jackman May 2 '13 at 13:23
    
yes i understand what u say about x= foo etc .... buut actaully i dont get what is te relation between this and my question :) :) –  Ahmed Zain El Dein May 2 '13 at 13:27
1  
The tail command lets you start reading from a specific number. The process substitution is what I called "handy" -- to relate to your question, it's a technique where the output of one command can easily be redirected into another without side effects: the while loop gets the output of tail, so the first read command gets the first line you want. Also, other people will read your question and hopefully learn something from my answer. –  glenn jackman May 2 '13 at 13:38
    
ok but what i want to make sure f it is that using this code it will never start reading files from the beginning and skip passing them along to while or whatever and will directly start reading from the specified line number , as if he made index to lines and start from the specified one , am i right or i misunderstand ? –  Ahmed Zain El Dein May 2 '13 at 13:49
#!/bin/bash
if [ $# -eq 0 ]; then
        echo "Please execute $0 with linestoskip parameter"
        exit 0
fi
linestoskip=$1
Counter=0
dataFile='/etc/fstab'
while read line
do
        if [ $Counter -ge $linestoskip ]; then
                echo $line
        fi
        Counter=`expr $Counter + 1`
done < $dataFile

This script expects number of lines to skip as a parameter. You can do whatever you like to in the inner if condition.

share|improve this answer
    
is that the only want there is no syntax for the psuedo code that i put above in my question like while read from .... ? at all –  Ahmed Zain El Dein May 1 '13 at 17:53
1  
In bash, you can use arithmetic expressions: ((Counter++)) so you don't have to call out to expr. –  glenn jackman May 1 '13 at 19:35

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.