The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Basic bash 'for loop' usage Orange Stripes Shell Programming and Scripting 2 12-18-2007 05:58 PM
Basic OS question catwomen HP-UX 4 09-08-2006 05:39 PM
basic question karthikosu SUN Solaris 2 07-12-2006 12:07 PM
basic question urwannabefriend UNIX for Dummies Questions & Answers 1 04-10-2004 02:21 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-28-2007
Registered User
 

Join Date: Apr 2007
Posts: 44
A basic question of FOR loop

Hi,
have a basic query.
Please see the below code:

list="one two three"
for var in $list ; do
echo $var
list="nolist"
Done

Wht if I want to print only first/ last line in the list

Eg one & three

Regards
er_ashu
Reply With Quote
Forum Sponsor
  #2  
Old 09-28-2007
Registered User
 

Join Date: Aug 2007
Location: Binfield, Berkshire. UK
Posts: 91
awk

don't know if it is the best way but awk will work....

echo $variable | awk '{print $1,$NF}'

note $NF represents the no of fields
Reply With Quote
  #3  
Old 09-28-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
with zsh and array:

Code:
zsh 4.3.4%zsh 4.3.4% list=(one two three)
zsh 4.3.4% print $list[1]
one
zsh 4.3.4% print $list[-1]
three
otherwise:

Code:
$ list="one two three"
$ printf "${list%% *}\n"
one
$ printf "${list##* }\n"
three
Reply With Quote
  #4  
Old 09-28-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
with bash
Code:
# var=(one two three)
# echo ${var[0]}
one
# echo ${var[2]}
three
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0