Sponsored Content
Top Forums Shell Programming and Scripting reading data from a file to an array Post 302702835 by kshji on Wednesday 19th of September 2012 01:11:03 AM
Old 09-19-2012
Here is some array using example.
=> for you is enough:
Code:
whle read line
do
     array=($line)
     ...
done

Array examples:
Code:
#!/some/shell     (=ksh, bash, ...)
#arrays
mytable=(a1 b2 c3)
echo ${#mytable[*]}
echo ${mytable[*]}

# save cmdline args
myargs=("$@")
echo "0:${myargs[0]}"
echo "1:${myargs[1]}"

# from file
cat <<EOF > $0.tmp
123 456 444
222 444 555
EOF

values=( $(<$0.tmp) )
echo "file in"
echo "0:${values[0]}"
echo "1:${values[1]}"
# from file, delimiter ;
cat <<EOF > $0.tmp
123;456;444
222;444;555
EOF

echo "csv"
oifs="$IFS"
IFS=";"
values=( $(<$0.tmp) )
IFS="$oifs"
echo "0:${values[0]}"
echo "1:${values[1]}"

# cmd output
echo "time"
values=( $(date '+%H %M %S') )
echo "0h:${values[0]}"
echo "1m:${values[1]}"
echo "2s:${values[2]}"

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading data into muti-dimentional array - in perl

Just want to learn how these are read into array but I don't seem to get it right what do I go wrong? Below is the sample Thanks input 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 #!/usr/bin/perl open (InFILE,"input"); while (<InFILE>) { @ar = split ; (5 Replies)
Discussion started by: zap
5 Replies

2. Shell Programming and Scripting

Using loop reading a file,retrieving data from data base.

Hi All, I am having trouble through, I am reading the input from tab delimited file containing several records, e.g. line1 field1 field2 field3 so on.. line2 field1 field2 field3 so on.. .. .. on the basis of certain fields for each record in input file, I have to retrieve... (1 Reply)
Discussion started by: Sonu4lov
1 Replies

3. UNIX for Dummies Questions & Answers

Reading a file into an array

I have a file that is a text file, how to get all the words into and array, i am able to get each line but not each word :(. Here is what i searched and already found...https://www.unix.com/shell-programming-scripting/99207-pipe-text-file-into-array.html. This one reads a whole line into... (6 Replies)
Discussion started by: SasankaBITS
6 Replies

4. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

5. Programming

Reading Scientific notation from file and storing in array

Hi, I am trying to read a set of numbers that are in scientific notation into a file so I can do some math on them, but when I display the array contents the numbers aren't the same as the numbers in the file. Could someone explain why? Thanks. int main() { double fArray; ... (3 Replies)
Discussion started by: Filter500
3 Replies

6. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

7. Shell Programming and Scripting

Reading columns from a text file and to make an array for each column

Hi, I am not so familiar with bash scripting and would appreciate your help here. I have a text file 'input.txt' like this: 2 3 4 5 6 7 8 9 10 I want to store each column in an array like this a ={2 5 8}, b={3 6 9}, c={4 7 10} so that i can access any element, e.g b=6 for the later use. (1 Reply)
Discussion started by: Asif Siddique
1 Replies

8. Shell Programming and Scripting

Reading a file into array

Hi, I need to read a file into array and print them in a loop:- 1st file :-cat a.txt RC1 RC2 RC3 RC4 My Program:- #!/bin/ksh index=0 while do read cnt<a.txt print "cnt value is ${cnt} index=`expr $index + 1` done Code tags for code, please. (5 Replies)
Discussion started by: satishmallidi
5 Replies

9. Shell Programming and Scripting

Reading a file into an array

Hi I have a file with contents as below : server | ABC Issue : File System Missing XYZ Issue : Wrong Syntax PQR Issue : Old File to be removed Now I am looking for an o/p similar to server <tab> ABC Issue : File System Missing <tab> XYZ Issue : Wrong Syntax <tab>... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

10. Shell Programming and Scripting

How to embed data instead of reading user input from an array?

Hello, I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works. Whenever I restart the system, I have to enter the same data from... (2 Replies)
Discussion started by: baris35
2 Replies
MYSQL_FETCH_OBJECT(3)							 1						     MYSQL_FETCH_OBJECT(3)

mysql_fetch_object - Fetch a result row as an object

SYNOPSIS
Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: omysqli_fetch_object(3) o PDOStatement::fetch object mysql_fetch_object (resource $result, [string $class_name], [array $params]) DESCRIPTION
Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead. o $ result -The result resource that is being evaluated. This result comes from a call to mysql_query(3). o $class_name - The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned. o $params - An optional array of parameters to pass to the constructor for $class_name objects. Returns an object with string properties that correspond to the fetched row, or FALSE if there are no more rows. Example #1 mysql_fetch_object(3) example <?php mysql_connect("hostname", "user", "password"); mysql_select_db("mydb"); $result = mysql_query("select * from mytable"); while ($row = mysql_fetch_object($result)) { echo $row->user_id; echo $row->fullname; } mysql_free_result($result); ?> Example #2 mysql_fetch_object(3) example <?php class foo { public $name; } mysql_connect("hostname", "user", "password"); mysql_select_db("mydb"); $result = mysql_query("select name from mytable limit 1"); $obj = mysql_fetch_object($result, 'foo'); var_dump($obj); ?> Note Performance Speed-wise, the function is identical to mysql_fetch_array(3), and almost as quick as mysql_fetch_row(3) (the difference is insignificant). Note mysql_fetch_object(3) is similar to mysql_fetch_array(3), with one difference - an object is returned, instead of an array. Indi- rectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names). Note Field names returned by this function are case-sensitive. Note This function sets NULL fields to the PHP NULL value. mysql_fetch_array(3), mysql_fetch_assoc(3), mysql_fetch_row(3), mysql_data_seek(3), mysql_query(3). PHP Documentation Group MYSQL_FETCH_OBJECT(3)
All times are GMT -4. The time now is 08:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy