LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 03-22-2005, 05:23 PM   #1
Louie55
Member
 
Registered: Oct 2003
Location: Nebraska
Distribution: SuSE 9.0, Redhat 9.0
Posts: 41

Rep: Reputation: 15
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.
 
Old 03-22-2005, 06:16 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,614

Rep: Reputation: 136Reputation: 136
You could loop as this :
Code:
i=1
for word in `cat file`; do
    echo "$i: $word"
    let "i++"
done
 
Old 03-22-2005, 06:17 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
Old 03-22-2005, 06:18 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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
 
  


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
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
Inserting lines into a file through shell scripting false-hopes Linux - General 1 10-22-2005 11:39 AM
number of command line arguments to shell u4u Linux - General 1 03-04-2005 06:09 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM
Extracting the line number in Unix Shell? Aziz Programming 2 12-01-2004 11:03 AM


All times are GMT -5. The time now is 09:39 AM.

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