LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Scripting: How to pick lines out of files by line number. (https://www.linuxquestions.org/questions/programming-9/shell-scripting-how-to-pick-lines-out-of-files-by-line-number-304851/)

Louie55 03-22-2005 05:23 PM

Shell Scripting: How to pick lines out of files by line number.
 
In a shell script (Linux), how would you pick out certain lines from a text file using line numbers. What I want to do is loop through each line of the file seperately. First, I want to read line 1, and perform an operation according to line 1's content, then I want to go to line 2, etc....

All I can find is how to pull lines from a file based on a pattern match (like grep). I want to get every line one at a time no matter what the pattern.

Each line only contains one word.

Thanks.

Louie.

keefaz 03-22-2005 06:16 PM

You could loop as this :
Code:

i=1
for word in `cat file`; do
    echo "$i: $word"
    let "i++"
done


Dark_Helmet 03-22-2005 06:17 PM

Here's one way:
Code:

line_number=1
word=$( cat filename | sed ${line_number}\!d )

The simpler, more straightforward way would be to use the read command. Something like:
Code:

while read line_word
do
  # do stuff
done < filename


Tinkster 03-22-2005 06:18 PM

There's probably several ways, two would be to
use awk or sed
Code:

awk '{if( NR==7)print}' file
This would print line 7 of file file ...

Code:

sed -n '7p' file
Same thing using sed


Cheers,
Tink


All times are GMT -5. The time now is 01:48 PM.