[BASH] mapping of values from file line into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [BASH] mapping of values from file line into variables
# 1  
Old 04-20-2009
[BASH] mapping of values from file line into variables

Hello,

I've been struggling with this for some time but can't find a way to do it and I haven't found any other similar thread.

I'd like to get the 'fields' in a line from a file into variables in just one command.

The file contains data with the next structure:
value1;value2;value3;value4;(...);value19

And I need to get these values into variables in order to create a MySql INSERT statement that I will later 'echo' into a file.

I have been able to get the values one by one by means of awk i.e.
for file in $dir/lcc*; do
echo "Processing file: $file"
while read line; do
value1=$(echo $line| awk -F\; '{print $1}')
value2=$(echo $line| awk -F\; '{print $2}')
...

But calling awk 19 times for every line in the file just doesn't seem logical to me and it takes too much time (and CPU).

There must be a simple way to map all value fields into variables, mustn't it?

Thank you very much!!
# 2  
Old 04-20-2009
Assuming the fieldseparator is a semicolon and your fields don't have spaces, you can do something like:

Code:
awk -F\; '{print}' OFS=" " |
while read value1 value2 value3........
do
  # Actions
done

Regards
# 3  
Old 04-20-2009
This variation works with spaces:

Code:
IFS=$'\n'
tr \; \\n |
while read value1 value2 value3 ..............
do
    doSomethingWith $value1
    doSomethingWith $value2
done

# 4  
Old 04-20-2009
Thank you very much for your kind contributions. They helped me a lot, although I couldn't get the 'multiple variable' while to work.

However I found out that by using IFS and 'read -a' the mapping could be done into an array (without the need to use awk).

This is now my code:

Code:
for file in $dir/lcc*; do
  echo "Processing file: $file"
  while read line
  do
    IFS=$'\;'
    read -a val
    if [ "${val[1]}" == "2" ]; then
      echo "ticket 2"
      echo "INSERT INTO trigger (VALUE1, VALUE3, VALUE4, VALUE5, VALUE6,\
VALUE7, VALUE8, VALUE9, VALUE10, VALUE11, VALUE12, VALUE13,\
VALUE14, VALUE15) VALUES ('${val[0]}','${val[2]}','${val[3]}','${val[4]}',\
'${val[5]}','${val[6]}','${val[7]}','${val[8]}','${val[9]}','${val[10]}',\
'${val[11]}','${val[12]}','${val[13]}','${val[14]}');" >> mysql.txt
    fi
  done < $file
done

# 5  
Old 04-21-2009
There was a bug in my code Smilie The correct one is:

Code:
for file in $dir/lcc*; do
  echo "Processing file: $file"
  IFS=$'\;'
  while read -a val
  do
    if [ "${val[1]}" == "2" ]; then
      echo "ticket 2"
      echo "INSERT INTO trigger (VALUE1, VALUE3, VALUE4, VALUE5, VALUE6,\
VALUE7, VALUE8, VALUE9, VALUE10, VALUE11, VALUE12, VALUE13,\
VALUE14, VALUE15) VALUES ('${val[0]}','${val[2]}','${val[3]}','${val[4]}',\
'${val[5]}','${val[6]}','${val[7]}','${val[8]}','${val[9]}','${val[10]}',\
'${val[11]}','${val[12]}','${val[13]}','${val[14]}');" >> mysql.txt
    fi
  done < $file
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Setting default values for variables

I have a variable I want to use in bash script. The user will pass an argument to the script and I will store it in `arg_fql`. If the user does not pass the variable, I still never set arg_fql, but I set another variable to a default. However, if the user passes a value, `arg_fql` will be set to... (2 Replies)
Discussion started by: kristinu
2 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

How to fetch values from a line in a file to variables in UNIX?

Hi, I need to assign values from a lines in a file into variables in unix, i am using Korn shell. I tried the below script from posts but i am unable to fetch every value in a variable. #! /usr/bin/ksh #for file in test.txt; do IFS=$'\|' I=1 while read -a val do echo... (15 Replies)
Discussion started by: karthikram
15 Replies

4. Shell Programming and Scripting

Exporting Values of Variables to Another File

Hello, I'm very new to Linux, and I have a question, I'm hoping you could help me with. :) I have created a file called subject, contains this code: #!/bin/bash read -p "Student Name: " NAME read -p "Student ID: " ID read -p "Address: " ADDRESS I'm to create another file called... (7 Replies)
Discussion started by: iwant2learn
7 Replies

5. Shell Programming and Scripting

mapping of values in shell scripting

sample content of file1: SSTY1 2145228348 652011011715140100000002419005432092074 008801726143662 VDZX01 MIO2 008801726143662 SSRTY 2145228349 ... (3 Replies)
Discussion started by: vsachan
3 Replies

6. 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

7. Shell Programming and Scripting

bash -- concatenating values from variables

Hi This is a simple one but I got a lost in translation when doing. What I want to do, given both variables in the example below, to get one value at the time from both variables, for example: 1:a 2:b etc... I need to get this in bash scripting code: varas="1 2 3 4" varbs="a b c d"... (4 Replies)
Discussion started by: ranmanh
4 Replies

8. Shell Programming and Scripting

[bash] command line substitution with environmental variables

Hi, I'm using an array that contains compiler FLAGS that need to be executed either before ./configure or after the main 'make' command. example of array containing compiler flags. ------------------------------------------------- FLAGS="CFLAGS=\"-arch x86_64 -g -Os -pipe... (7 Replies)
Discussion started by: ASGR
7 Replies

9. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

10. Programming

how do u assign the values to different variables when it is presneted in one line??

hey people.. i have a configuration file that looks like 7080 7988 net04.xxxxx.edu 20 where 20 is the number of threads in the thread pool initially. net04.xxxxx.edu is the hostname. and 7080 7988 are two ports. first one for client requests and second one for dns communication. now my... (2 Replies)
Discussion started by: SwetaShah
2 Replies
Login or Register to Ask a Question