Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


LinkBack Search this Thread
Old 03-22-2005, 05:23 PM   #1
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.


Old 03-22-2005, 06:16 PM   #2
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

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

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Here's one way:
word=$( cat filename | sed ${line_number}\!d )
The simpler, more straightforward way would be to use the read command. Something like:
while read line_word
  # do stuff
done < filename
Old 03-22-2005, 06:18 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
There's probably several ways, two would be to
use awk or sed
awk '{if( NR==7)print}' file
This would print line 7 of file file ...

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



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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 03:10 AM.

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