File Field Replacement, Assigning Fields to Variables, Lists/Arrays?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File Field Replacement, Assigning Fields to Variables, Lists/Arrays?
# 1  
Old 02-19-2011
File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more.

I just want this one question answered: How can I assign fields from a file(comma separated) to variables?

My goal is to check whether or not a user is driving a car. If he is, he will not be capable of driving others. If he is not, he will be capable of driving one.

So first off, here's a sample cars.csv file I made:
The fields separated by commas represent ID number of driver, car model, brand, make, and a boolean 0/1 to see whether or not the car is being driven, respectively.
Code:
#cars.csv
000000,Protege,Mazda,2003,0
000000,Pathfinder,Nissan,1997,0
123456,Corvette,Chevrolet,1976,1
000000,Jimmy,GMC,1994,0
535712,Carrera,Porsche,1

Next, here is a sample drivers.csv listing the drivers:
The fields in this file are ID number of driver, name of driver, and a boolean 0/1 to check if the user is driving a car, respectively.
Code:
#drivers.csv
123456,John,1
125639,Jack,0
967381,Bob,0
535712,Ben,1
649264,Brian,0

This file, garage.sh allows the user to be selected. I used case for the first part as I don't know how to do what I am asking for..yet.
Code:
#garage.sh
echo "Who are you? (select 1,2,3,etc)"
echo 1. John
echo 2. Jack
echo 3. Bob
echo 4. Ben
echo 5. Brian
read name
case $name in 
	1)
		IDnum="123456";;
	2)
		IDnum="125639";;
	3)
		IDnum="967381";;
	4)
		IDnum="535712";;
	5)
		IDnum="649264";;
	*)
		echo Invalid command;;
esac
echo " "
echo " "
echo Enter one of the commands below:
echo 1. Drive a car
echo 2. Park a car
echo 3. Check user status
echo 4. Quit
while :
do
        read command
        case $command in
                1)
                        `sh drive.sh "$IDnum"`;;
                2)
                        `sh park.sh "$IDnum"`;;
                3)
                        `sh userstatus.sh "$IDnum"`;;
                4)
                        break;;
                *)
                        echo Invalid command;;
        esac
done


Okay, so what I want to do is try select a driver and select a command. I don't want to ask how to do a lot as I want to learn to do the others from what you tell me.

So, for example, I could compare the IDnum in cars.csv with the IDnum in drivers.csv and output it with something like this:
Code:
join -t, -11 -21 -o2.2,1.2,1.3,1.4,1.1 <(sort cars.csv) <(sort drivers.csv) > output.csv

Question 1:
How would I do the above comparison but assign all fields to different variables? (ie. $model=1.2, $year=1.4, etc)

Quote:
Question 2:
How would I make it output a list to the screen like the one below and allow users to select which car to drive(preferably using case so the code flows)?
Code:
#only cars that are not being driven
1. 2003 Mazda Protege
2. 1997 Nissan Pathfinder
3. 1994 GMC Jimmy

Figured this one out on my own using the following:
Code:
count=0
awk -F, '{ if ($1 == "000000") print $4, $3, $2}' cars.csv | while read line
do
        count=$(($count + 1))
        echo $count. $line
done
echo " "
echo "Which car would you like to drive?"
read car

Still need to know how to set things to values and write them back in

Quote:
Question 3:
Conversely to question 1, how would I write/replace fields in the .csv's with variables in the script? (I think I could do it using sed?)

EDIT: I think I can just use -tr...but we'll see

Last edited by chickeneaterguy; 02-20-2011 at 05:21 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a file - and assigning responses to variables (or something)

So first: Sorry if the title is confusing... I have a script I'm writing with a file with several names in it (some other info - but it's not really pertinent...) - I want to be allow the user to delete certain records, but I ran into a problem I'm not sure how to go about fixing. If I were... (6 Replies)
Discussion started by: sabster
6 Replies

2. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

3. Shell Programming and Scripting

Assigning fields values to different variables

Hi, I have this code: cat file.txt | awk -F, 'NR==1{print $6","$8","$10","$20","$21","$19}' > file.tmp VAR1=`cat file.tmp | cut -d "," -f1` VAR2=`cat file.tmp | cut -d "," -f2` VAR3=`cat file.tmp | cut -d "," -f3`; VAR4=`cat file.tmp | cut -d "," -f4`; VAR5=`cat... (1 Reply)
Discussion started by: Tr0cken
1 Replies

4. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

5. UNIX for Dummies Questions & Answers

Comparing lists.. Arrays maybe?

Problem Part 1. Gather data from linux server and output to a file named data_DDMMYY Output file must contain the file name and size Part 2. Compare todays data_DDMMYY to yesterdays data_DDMMYY and output results to a file named difference_DDMMYY Output file must show the difference in... (3 Replies)
Discussion started by: Aussiemick
3 Replies

6. Shell Programming and Scripting

Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier). My text document for example would be something as simple as 1312... (6 Replies)
Discussion started by: samh785
6 Replies

7. Shell Programming and Scripting

assigning fields variables value to shell variable

suppose I have a file named abc.txt.The contents of the file is sited below abc.txt maitree,test,test3 Using awk command can I store these 3 values in 3 different variable and in one single line command of awk. suppose variable a b c is there. I don't want like this a=`awk -F"," '{print... (2 Replies)
Discussion started by: maitree
2 Replies

8. UNIX for Dummies Questions & Answers

Assigning String to Arrays

Hello everybody, this is my first post here, I think IŽll be here for a long time :D I wanted to know how I can assign each single character from a string to an array... For example, "unix" I have to set, array=u array=n array=i array=x However, that string may change in each... (5 Replies)
Discussion started by: Gartlar
5 Replies

9. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

10. Shell Programming and Scripting

Assigning file attributes to variables

Hi, I'm trying to assign the permissions, owner and group of a file to seperate variables, but using ls -l filename | awk '{print $1 "\t" $3 "\t" $4}' gives the owner as tom.ja instead of tom.james Is there any way to expand it so i get the full name, or is there an easier way to get them... (5 Replies)
Discussion started by: olimiles
5 Replies
Login or Register to Ask a Question