The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
query on how to search for a line and read 4th word from that line jaggesh UNIX for Dummies Questions & Answers 4 07-01-2008 10:21 PM
Alternative for a while read line loop kabs UNIX for Dummies Questions & Answers 2 04-01-2008 12:25 PM
ls while read loop - internal read picking up wrong input dkieran Shell Programming and Scripting 2 05-14-2007 03:02 PM
While read loop and rsh 104234 UNIX for Advanced & Expert Users 1 01-15-2006 11:53 AM
Nested while read line loop Rakker Shell Programming and Scripting 7 06-24-2005 07:42 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-28-2008
balajim balajim is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 3
read line in a for loop

Hi All,
How can we use read line using the index value of a FOR loop?
eg:
pt_mstr,pt_status,8
pt_mstr,pt_buyer,8
pt_mstr,pt_sfty_stk,8
pt_mstr,pt_ord_pol,3
pt_mstr,pt_capacity,8
pt_mstr,pt_plan_ord,3
pt_mstr,pt_ord_mult,8

From this file i want to read the line2, 3 and 4 only using a FOR Loop.

for (( i=2; $i <= 4; i++ ))
do
read line
echo $line
done

This will read 3 lines from the top, but what i want is the lines from the index of the FOR Loop. Here the index of the for loop is 2, 3 and 4 so i want the line2 , 3 and 4

Thanks,
Balaji
  #2 (permalink)  
Old 10-28-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Reason for using a for loop?

I would probably approach with head and tail command.
Code:
> tail +2 file14 | head -3
pt_mstr,pt_buyer,8
pt_mstr,pt_sfty_stk,8
pt_mstr,pt_ord_pol,3
  #3 (permalink)  
Old 10-28-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
You cannot easily do what you want using a for loop since there is no connection between your i variable and read. read does not magically advance to the i th line!

Here is a simple way to achieve what you want use a while loop
Code:
#/bin/bash

i=0
while read line
do
   (( ++i ))
   case $i in
    [2-4]) echo $line
   esac
done < file
  #4 (permalink)  
Old 10-29-2008
lifegeek lifegeek is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 28
another method

file.txt
--------------------------------
pt_mstr,pt_status,8
pt_mstr,pt_buyer,8
pt_mstr,pt_sfty_stk,8
pt_mstr,pt_ord_pol,3
pt_mstr,pt_capacity,8
pt_mstr,pt_plan_ord,3
pt_mstr,pt_ord_mult,8
---------------------------------
Code:
#! /bin/bash 

set $(cat file.txt)
for (( i=2; $i <= 4; i++ ))
do
eval echo \$$i
done
or

Code:
#! /bin/bash

for i in `cat`
do
((j++))
if [ $j -eq 2 -o $j -eq 3 -o $j -eq 4 ]
then
echo $i
fi
done
or

Code:
cat >> temp && sed -n '2,4p' temp

Last edited by lifegeek; 10-29-2008 at 10:24 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0