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
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 02:25 PM
array ccp Shell Programming and Scripting 3 02-26-2008 03:19 AM
I need help with an array! djsal Shell Programming and Scripting 1 11-28-2007 11:26 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 08:42 AM
Do I need an array here? TheCrunge Shell Programming and Scripting 5 04-19-2005 03:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-06-2007
shary shary is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 61
using array

hello experts

can u tell me please how i have a text file .
In third field of a file there are names like

abb
asa
asas
asasas
i just want to store the third filed of a file in an array.
and to display the result in a same order using for loop through array
can any one tell me how to do this in the shell script

take care bye

shary
  #2 (permalink)  
Old 02-06-2007
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
What is the field delimiter? The following assumes a comma:
Code:
integer i=0
awk 'BEGIN {FS=OFS=","} {print $3}' file.txt | while read item; do
  myarray[$i]=$item
  i=$i+1
done
If the fields are delimited by spaces, just eliminate the BEGIN block:
Code:
integer i=0
awk '{print $3}' file.txt | while read item; do
  myarray[$i]=$item
  i=$i+1
done
  #3 (permalink)  
Old 02-06-2007
Glenn Arndt's Avatar
Glenn Arndt Glenn Arndt is offline Forum Advisor  
Anomalous Lurker
  
 

Join Date: Feb 2006
Location: Indianapolis, IN
Posts: 255
You can loop through the array you created with this:
Code:
for x in ${myarray[*]}; do
  print $x
done
  #4 (permalink)  
Old 02-06-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,793
With bash:
Code:
$ cat file
filed1 filed2 field3_1 field4
filed1 filed2 field3_2 field4
filed1 filed2 field3_3 field4
filed1 filed2 field3_4 field4
filed1 filed2 field3_5 field4
filed1 filed2 field3_6 field4
filed1 filed2 field3_7 field4
$ a=($(cut -d" " -f3 file))
$ i=0;until((i>=(${#a}-1)));do echo ${a[i]};((i+=1));done
field3_1
field3_2
field3_3
field3_4
field3_5
field3_6
field3_7
With awk:
Code:
awk '{x[NR]=$3}
END{for(i=1;i<=NR;i++)print x[i]}' file
  #5 (permalink)  
Old 02-07-2007
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Quote:
Originally Posted by radoulov
$ a=($(cut -d" " -f3 file))
$ i=0;until((i>=(${#a}-1)));do echo ${a[i]};((i+=1));done
This will work with ksh93 also.
  #6 (permalink)  
Old 02-07-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
eval ` awk ' { printf("arr[%d]=%s\n",NR,$3) } END { printf("tot=%d",NR) }' file `
i=1
while [[ $i -le $tot ]]
do
	echo ${arr[$i]}
	(( i=i+1 ))
done
  #7 (permalink)  
Old 02-07-2007
shary shary is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 61
using array

hi experts

thank you so much for your cooperation i really appreciate your quick response.

Regards,
Shary
Sponsored Links
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 09:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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