how to read a value from a file and pass it to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to read a value from a file and pass it to a variable
# 1  
Old 06-25-2008
how to read a value from a file and pass it to a variable

Hi,
Some one please help me with this script.

I have a file "sequence.txt" and contents of it look like below.

1 5
3 7
4 7
5 74

from my script i should be able to read every single and save the first columnbs to variable x1, x2, x3 and second column as y1, y2, y3 and so on. Then i can use those x and y values to my rest of the script.

Thank you in advance.
# 2  
Old 06-25-2008
ksh

#!/usr/bin/ksh

count=0;
while read each_line
do
x[count]=`echo "$each_line" | cut -f 1 -d ' '`
y[count]=`echo "$each_line" | cut -f 2 -d ' '`
count=`expr $count + 1`
done < unix_forum_file.txt

i=0;
while [ $i -lt $count ]
do
print ${x[$i]};
print ${y[$i]};
(( i=i+1 ))
done


Filename : cat unix_forum_file.txt

2 3
4 5
6 7
12 32
44 22
32 11


$ ./unix_forum.ksh

2
3
4
5
6
7
12
32
44
22
32
11


I hope this helps .....
# 3  
Old 06-25-2008
MySQL Exellent

THanx for the great help.
# 4  
Old 06-25-2008
Rather than calling cut to break up the read line
Quote:
count=0
while read each_line
do
x[count]=`echo "$each_line" | cut -f 1 -d ' '`
y[count]=`echo "$each_line" | cut -f 2 -d ' '`
count=`expr $count + 1`
done < unix_forum_file.txt
try the following
Code:
count=0;
while read num1 num2
do
  x[count]=$num1
  y[count]=$num2
  ((count++))
done < file

or more tersely
Code:
count=0;
while read x[count] y[count]
do
  ((count++))
done < file

Both work for ksh93. Have not tested in any other version of ksh or bash.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file lines and pass line values as arguments.

Dears, Need help to implement below requirement A file (detail.txt)contain : 1st column: Stream 2nd column: PathAddress 3rd column: Counterlimit 4th column: TransactionDateColumn 5th column: DateType 6th column: SleepValue 7th column: Status Need to write a... (1 Reply)
Discussion started by: sadique.manzar
1 Replies

2. Shell Programming and Scripting

How to read the output of a command line by line and pass it as a variable?

Hi, I have some 2000 names in a table like below. Java Oracle/SQL ANSI SQL SQL,DWH,DB DB&Java And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line. I have to execute the below queries in... (10 Replies)
Discussion started by: Samah
10 Replies

3. Shell Programming and Scripting

Read files into variable using then pass to delete call

I am trying read all the files from list into a variable line using bash. After there are read into the variable they are passed to a delete call. The files appear to be read line (as I can see them with the echo) by line into the variable, but the delete call is not removing them and I do not... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Read input from file and pass it to sub shell

i have a scenario where in i have to monitor jobs which run in different servers, The job details(job name, host server, etc) are present in a dat file. I have written a script a script which reads the details from the dat file and calls a local method where the job monitoring logic is present.... (2 Replies)
Discussion started by: abubucker0
2 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Shell Programming and Scripting

Read Oracle Username password SID from single file and pass it to shell

Dear All I am trying to write one shell which will be running through Cron which contain one SQL query. But I want to draw/fetch the Username password and Instance name (required to loging to the database) from one single file to run that SQL query . Also this file contain details of multiple... (2 Replies)
Discussion started by: jhon
2 Replies

7. Shell Programming and Scripting

how do I pass or read values from file ?

Hi one & All , My Need is to Create 64 Partition and create File System in Linux. I have the Script ... for((a=0;a<=63;a++)) do fdisk /dev/cciss/c0d$a done for((a=0;a<=63;a++)) do mkfs.ext2 /dec/cciss/'c0d'$a'p1' done the moment I run the Script I get the Prompt ... Command... (1 Reply)
Discussion started by: nix-kid
1 Replies

8. Shell Programming and Scripting

getting the file name and pass as variable

Can any one suggest me how to check the file extension and pass the name based out of the filename within the folder. There would be always one latest file in the folder, but extension may vary... ie .csv, .CSV,.rpt,.xls etc what is best way to get the latest file name and pass as variable.... (1 Reply)
Discussion started by: u263066
1 Replies

9. Shell Programming and Scripting

Read through a file and Pass system commands

Hi, I have a file xyz.txt, which contains several "tar.gz" package names Eg :- Now i need to execute an rpm - ivh against all those packages in this file through a script one by one. I need a script to read through the file "xyz.txt", pick up each package name and execute rpm -ivh... (7 Replies)
Discussion started by: systemali
7 Replies

10. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies
Login or Register to Ask a Question