Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 SCNLOCI 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.

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/kshwhile read v1 v2
do
echo "$v1"
echo "$v2"
done < data3.txt

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.

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

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

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

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

![]() |
![]() |
![]() |

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