Passing data from file to variables for script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing data from file to variables for script
# 1  
Old 02-14-2008
Passing data from file to variables for script

Hello all! After searching through numerous helpful posts on these forums I am still having an issue with a task I am trying to accomplish.

I am trying to take data from an input file, store the contents as variables, and use the variables in the script.

The input file (input.txt) is composed of 3 values: name, number, state. For example:

Quote:
John Doe, 555-123-4567, Texas
Jane Doe, 555-321-7654, Oklahoma
Jim Doe, 555-000-1111, California
Jeane Doe, 555-111-0000, New York
The main script requires the data to be in the same order but without the commas, i.e:

Quote:
name number state
After researching these forums I have come up with the following script that takes the data and puts it in an array:

Quote:
awk -F "," '{
array[$0] = array[$0] " " $1 " " $2 " " $3;
}
END {
for (i in array)
print array[i];
}' input.txt
Verifying with the print command, the array is properly formated how the main script needs it. Now the issue I am having is using the contents of the array in the variables for my main script. The script creates a single file using variables I want filled from the array.

Here is an abridged version of the main script using manually entered data in the variables:

Quote:
#!/bin/bash

cust=("John Doe");
tele=("555-123-1456");
state=("Texas");

echo "Please enter batch name: "
read batchname
echo "Creating "$batchname".txt..."

counter=${#cust[@]}
index=0

while [ "$index" -lt "$counter" ]
do

echo -e "\nCustomer Name:\t\t"${cust[$index]}"\n\tPhone:\t\t\t"${tele[$index]}"\n\tState:\t\t\t"${state[$index]}"\n" >> $batchname".cfg"

let "index = $index +1"
done

exit 0
I am trying to get the data from the array (taken from input.txt) to be used in the 3 variables in the main script (cust, tele, state), but with no luck.

The output file will be formatted as such:

Quote:
batchname.txt

Name: John Doe
Phone: 555-123-1234
State: Texas

Name: Jane Doe
Phone: 555-321-4321
State: Oklahoma

etc, etc...
Please let me know if you have any questions or need clarification on the issue I am having.

Thanks in advance!
# 2  
Old 02-14-2008
Code:
$ nawk -F"," '{printf("Name: %s\nPhone:%s\nState:%s\n\n", $1, $2, $3)}' batchnames.txt

Output:
Code:
Name: John Doe
Phone: 555-123-4567
State: Texas

Name: Jane Doe
Phone: 555-321-7654
State: Oklahoma

Name: Jim Doe
Phone: 555-000-1111
State: California

Name: Jeane Doe
Phone: 555-111-0000
State: New York

HTH
# 3  
Old 02-14-2008
This put me on the right track. Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variables from one script to another

Hi, this is the example i'm trying to do. script1.sh ABC="test.txt" ./script2.sh "$ABC" script2.sh INPUT="$HOMEDIR/$ABC" echo $INPUT when i run the 1st script it gives me ../home/ the test.txt is not passed into 2nd script. How can i resolve this. (2 Replies)
Discussion started by: gskris88
2 Replies

2. Shell Programming and Scripting

Passing gnuplot variables to shell script

Hi, I need to pass a gnuplot value to a shell script. I have a main shell script (Main.sh) that has a bunch of gnuplot commands. Main.sh calls another gnuplot script (Child.gnu). A part of the code in Child.gnu is as follows: sp '</data/src/scripts/results/plot_data.sh $col' u (A):2:3 w pm3d... (8 Replies)
Discussion started by: annazpereira
8 Replies

3. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

4. Web Development

passing variables to python script

Hello again, You guys might remember me from the "Apache problems" thread. And because this forum is the only one that actually helped me out after months of problems I was hoping you could help me with something related. Now if you remember the final code that we made for creating an account... (0 Replies)
Discussion started by: darkphaux
0 Replies

5. Shell Programming and Scripting

Need help passing variables in shell script to perl one-liner

I'm writing a script to automate some post-install tasks on RHEL4 servers. I need the following code to insert an 'A' in the middle of a string, then replace the string in a file. I know I can use sed to do this, but I'd like to use perl's in place edit so I don't have to write to a temp file,... (1 Reply)
Discussion started by: Xek
1 Replies

6. Shell Programming and Scripting

passing variables to sed function in a script ....

Hello , I have a script named testscript.sh wherein I have two variables $var and $final (both of which contain a number) I have a sed write function inside this script as follows: sed '1,2 w somefile.txt' fromfile.txt Now , in the above i want to pass $var and $final instead of... (2 Replies)
Discussion started by: shweta_d
2 Replies

7. Shell Programming and Scripting

passing two variables into a shell script?

Hello all, i have a infile.txt text file which contains such variables: aaa 123 asds 1323 asdsd 13434 lkjlkj 324324 23432 lkjlkj 24324 ljlkj 3j4lk 234kj3 and i want to pass them to my script such as: ./myscript $1 $2 where $1 is the first value in the first row and $2 is the second... (2 Replies)
Discussion started by: Bashar
2 Replies

8. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies

9. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies

10. Shell Programming and Scripting

Passing filenames/variables into a script

Hi! I'm somewhat new to Unix (I use it to fiddle, but not seriously), and at my work I have come to use it quite a bit. My question is this: I want to create a script to take the tedium out of repetitious tasks. However, I need to pass a file name into the script in order for it to work. ... (5 Replies)
Discussion started by: MrToast
5 Replies
Login or Register to Ask a Question