Computing.Net > Forums > Unix > Parsing file to store array variabl

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Parsing file to store array variabl

Reply to Message Icon

Name: pnbalaji
Date: November 19, 2008 at 09:32:11 Pacific
OS: Windows XP SP2
CPU/Ram: 2 GB RAM
Product: Dell Optiplex
Comment:

Hi,

I have a file ./scanner_users.txt which contains the data as given below. There may be multiple spaces between the two fields, but the file will contain only two fields.

TRIM SCNLOG
PACK1 SCNLOG
INQ SCNINQ
LOC1 SCNLOC

I want to read this file, parse each line and store them into array variables like the ones given below.

_scannerUser[1]="TRIM"
_scannerProg[1]="SCNLOG"
_scannerUser[2]="PACK1"
_scannerProg[2]="SCNLOG"
etc..


I don't want to use awk.

Can some one help me?

Thanks,
Balaji.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 19, 2008 at 10:08:40 Pacific
Reply:

Hi:

First, I'd like to know what unix shell you are using before I get too much into arrays.

This little korn shell script reads and parses a file with only 2 columns per line:


#!/bin/ksh

while read v1 v2
do
echo "$v1"
echo "$v2"
done < data3.txt



0

Response Number 2
Name: pnbalaji
Date: November 19, 2008 at 11:24:29 Pacific
Reply:

Hi,

Thanks for your response. I am using Korn shell only.

I tried your approach and it works fine. Thanks for your help.

Thanks,
Balaji.


0

Response Number 3
Name: rstemphowski
Date: February 17, 2009 at 09:19:58 Pacific
Reply:

Hi,

How would you do the above with a file that has a variable number of columns? Actually, the rows on the file are keyed, that's why I thought to use an array. Any better suggestions? The first column is the key, that is followed by 4 columns that exist for every row. The sixth column is the number of columns I need to read for the remainder of that record. I know I need to do an inner loop but I don't know how to do the variable names with a subscript ($v1,...,$v7,$v8,...,$vn. I'd appreciate any help you can give me.

Thanks,
Bob


0

Response Number 4
Name: nails
Date: February 17, 2009 at 13:49:09 Pacific
Reply:

I'm assuming you are not talking about arrays. The shell does not support multi-dimensional arrays. But you can create shell variables using the eval command.

Suppose you have a line in your datafile that looks like this:

key f2 f3 f4 c5 4 c1 c2 c3 c4

If I've read your spec correctly, there are 4 variables after the 6th argument where the 6th arg says there is 4. The following script will create, display,and destroy variables a1, a2, a3, a4.

It should work for a multiple number of variables, but they all start with "a". (Haven't tested it for variable number beyond 9).

This script works because the set command parses the variable xremainder using spaces. The parsing process sets the parsed segments equal to the command line parameters $1, $2, $3, etc. $# is the total number of args parsed.

#!/bin/ksh

while read xkey x2 x3 x4 x5 cnt xremainder
do
   set - $(IFS=" "; echo ${xremainder})
   echo "$xremainder"

   # create the variables a1, ...an
   i=1
   while (($i <= $#))
   do
      eval a$i=\$$i
      ((i+=1))
   done

   # display variables
   h=1
   while (($h <= $#))
   do
      eval echo \"\$a$h\"
      ((h+=1))
   done

   # destroy variables
   h=1
   while (($h <= $#))
   do
      unset eval a$h
      ((h+=1))
   done

done < datafile.txt


0

Response Number 5
Name: rstemphowski
Date: February 18, 2009 at 10:25:37 Pacific
Reply:

Hi Nails,

Thank you.

Things have taken a turn with where the input is coming from. Instead of a csv file, I now need to read from a db2 table. My table has 6 columns. The first is a non-unique key. I need to pass a $variable to the sql so that I can select * from table where key=$variable. I need to then take the result of 0 or more rows, and using a loop that will be executed once for each row returned, parse the rows' columns into individual script variables. I've not executed sql commands from within a script before. Can you help with this one? I've searched through this site but it's difficult to find exactly what I am looking for.

Thanks,
Bob


0

Related Posts

See More



Response Number 6
Name: nails
Date: February 18, 2009 at 12:37:27 Pacific
Reply:

Bob:

Yes, I can help, but only up to a point since I'm not a DB2 guy.

If you plan on interacting with a database in a unix shell script, you require an interface to the database. Oracle has plsql, Informix has isql & dbacess, Sybase has isql (different from an informix) Probably DB2 does also????

I'm an informix guy, this is an example unix script that I've posted in this forum:

http://www.computing.net/answers/un...

As I've said, Since I've never worked with DB2, my help might be limited. Post your questions in this forum or send me a personal note thru computing.net and I'll give you an email address.

Regards,

Nails


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Parsing file to store array variabl

Parsing file, storing as variables www.computing.net/answers/unix/parsing-file-storing-as-variables/5228.html

Script: Read file to set variables www.computing.net/answers/unix/script-read-file-to-set-variables/3849.html

variables from one file to another www.computing.net/answers/unix/variables-from-one-file-to-another/7378.html