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
Help required with a Csh script to read data from a file fizzme Shell Programming and Scripting 1 05-29-2008 07:30 PM
Need to read data from a file (cut/awk) rejirajraghav Shell Programming and Scripting 1 04-29-2008 03:13 PM
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 10:28 PM
read/writte/input data in file. dorek Shell Programming and Scripting 0 06-22-2006 07:31 AM
Read data from a file into a variable yorkdg Shell Programming and Scripting 2 12-09-2004 05:29 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 02-26-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Read from data file

Hi,
I have a data file formatted like this:
[Name] [Number]

Ex:
Mike 3434
Jack 481
Peter 12
Alan 926

I want to get this data into 2 variables: "Names" and "Numbers" that I can using one "for" loop to get the value as Names[i] and Numbers[i]
Like this:
Code:
for i in 0 1 2 3 
do
   echo $Names[$i]
   echo $Numbers[$i]
done
Can anyone help ?
  #2 (permalink)  
Old 02-26-2007
sb008 sb008 is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 384
Code:
#!/usr/bin/ksh

# reading content of file into arrays
INDEX=0
while read NAMES[${INDEX}] NUMBERS[${INDEX}]
do
  ((INDEX=$INDEX+1))
done < <inputfile>

#displaying values in each array
for NAME in ${NAMES[@]}
do
  echo ${NAME}
done

for NUMBER in ${NUMBERS[@]}
do
  echo ${NUMBER}
done
  #3 (permalink)  
Old 02-27-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Hi,
How about read the content into arrays, I can use awk to read it content by column
But the challenge is how to declare array NAMES and NUMBERS,
Can you show me the way?
  #4 (permalink)  
Old 02-27-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Give a try on this...

#!/bin/bash
for i in $(awk -F" " '{print NR}' new)
do
nam_arr[$i]=$(awk -F" " -v cnter=$i '{ if (cnter == NR) {print $1;} }' new)
num_arr[$i]=$(awk -F" " -v cnter=$i '{ if (cnter == NR) {print $2;} }' new)
done
  #5 (permalink)  
Old 02-27-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
In my perv post, nam_arr[<index>] and num_arr[<index>] are Arrays!!!Hope this is what you need...
And 'new' is the file which contain the data
  #6 (permalink)  
Old 02-27-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
It works!
Thank jacoden and sb008!
  #7 (permalink)  
Old 02-27-2007
fongthai fongthai is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 54
Hi jacoden,

I have one questions:
Data file may contain empty lines or un-used lines (start with "#" character). Can we exclude them from the arrays.

Last edited by fongthai; 02-27-2007 at 03:09 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 10:08 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