LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-14-2006, 06:34 AM   #1
chobin
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: Fedora Core 4 - Suse 10.0
Posts: 24

Rep: Reputation: 15
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
 
Old 06-14-2006, 07:02 AM   #2
exe
LQ Newbie
 
Registered: Jan 2004
Distribution: fedora,rhel
Posts: 5

Rep: Reputation: 0
head -n <file> | tail -1 :-(
 
Old 06-14-2006, 08:01 AM   #3
chobin
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: Fedora Core 4 - Suse 10.0
Posts: 24

Original Poster
Rep: Reputation: 15
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
 
Old 06-14-2006, 08:09 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,087

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 06-14-2006, 08:22 AM   #5
chobin
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: Fedora Core 4 - Suse 10.0
Posts: 24

Original Poster
Rep: Reputation: 15
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....
 
Old 06-14-2006, 09:11 AM   #6
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,008

Rep: Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624Reputation: 624
this should get you started:
Code:
sed -n 5p file.txt
 
Old 06-14-2006, 09:14 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Code:
perl -ne 'print if grep {$. == $_ } (3, 5, 9)' text.txt
 
Old 06-14-2006, 10:41 AM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,087

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
 
Old 06-14-2006, 11:14 AM   #9
chobin
LQ Newbie
 
Registered: May 2006
Location: Italy
Distribution: Fedora Core 4 - Suse 10.0
Posts: 24

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM


All times are GMT -5. The time now is 06:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration