LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Read specific lines from a text file (http://www.linuxquestions.org/questions/programming-9/read-specific-lines-from-a-text-file-454639/)

chobin 06-14-2006 06:34 AM

Read specific lines from a text file
 
Hi,
I wrote this script to read each line of a text file and than do something with the line content:
Code:

for i in 'cat text.txt'
do
......
done

Is there a way to read only specific lines of text files?
For example I have 10 lines and I want to read only
line 3
line 5
line 9
Thanks in advance for help :)

exe 06-14-2006 07:02 AM

head -n <file> | tail -1 :-(

chobin 06-14-2006 08:01 AM

Quote:

Originally Posted by exe
head -n <file> | tail -1 :-(

ok, but i have to integrate it in a for loop.
I don't know the exact number of lines in the text file, and I have to read lines jumping some of them. To be clear I need to do something like this:
start to read the third line of the file
then jump one line and read the next one
do this to the end of the lines

jlinkels 06-14-2006 08:09 AM

You'll find information about reading text lines in the advanced bash scripting guide

It is something like this:

Code:

while read line
do
  echo $line
done <mytextfile

It reads lines from mytextfile and you can do fancy things checking or processing the $line variable.

jlinkels

chobin 06-14-2006 08:22 AM

Quote:

Originally Posted by jlinkels
You'll find information about reading text lines in the advanced bash scripting guide

It is something like this:

Code:

while read line
do
  echo $line
done <mytextfile

It reads lines from mytextfile and you can do fancy things checking or processing the $line variable.

jlinkels

yes I know how to read all lines of i file text, what i need to do is reading lines jumping a certain number of them.... :)

schneidz 06-14-2006 09:11 AM

this should get you started:
Code:

sed -n 5p file.txt

bigearsbilly 06-14-2006 09:14 AM

Code:

perl -ne 'print if grep {$. == $_ } (3, 5, 9)' text.txt

jlinkels 06-14-2006 10:41 AM

Yes, I understood. There are a zillion ways to do that. One of them could be:

Code:

lines_to_skip=0
while read line
do
  echo $line
  if [ $lines_to_skip -gt 0 ]
  then
    <do nothing>
    let "lines_to_skip--"
  else
    <process $line>
    <set lines_to_skip if necessary>
  fi
done <mytextfile

Note that <this code> is not syntactically correct.

If you want to skip line without actually reading them, that is not possible in the loop AFAIK. You could use an external program for that, but is that really worth doing?

jlinkels

chobin 06-14-2006 11:14 AM

Quote:

Originally Posted by schneidz
this should get you started:
Code:

sed -n 5p file.txt

ok, Thanks to schneidz suggestion I solved in this way:
I set a variable to the number of line I want to start from
Then in the loop I increase this variable to skip one line in sed command
Code:

I=3
for j in 1 2 3 ....
do
sed -n "$I"p file.txt
I='expr $I + 2'
done

it works !!! :)
Thanks everybody for help


All times are GMT -5. The time now is 08:35 AM.