![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|