Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

korn shell arrays

Status
Not open for further replies.

cmeyers

Programmer
Jul 6, 2001
24
US
Feel up for a awk/ksh question?

I'm trying to get data into a ksh while loop using ksh arrays. This I've found is more efficient than for-item-in-list construct.

The problem is my array elements are descriptions which have spaces between the words. My descriptions are in the 2nd field of a CSV file:

CSV file:
[tt]c1,Tank Level Controller
f1,Flow Path #1
f2,Flow Path #2
h1,Header #1
h2,Header #2
m1,Motor #1
m2,Motor #2
p1,Pump #1
p2,Pump #2
t1,Tank #1
v1,Valve #1
v2,Valve #2[/tt]

The script:
Code:
#!/usr/bin/ksh

# set IFS to NEWLINE only (ignore SPACES and TABS)
IFS"
"

set -A array1 $(gawk 'BEGIN {FS=","} {print $2}' file.csv)

i=0
while (( $i<${#array1[*]} ))
 do

  print &quot;<OBJ_DESC>${array1[$i]}</OBJ_DESC>&quot;

  i=$((${i}+1))

 done

The result is:
[tt]</OBJ_DESC>ank Level Controller
</OBJ_DESC>low Path #1
</OBJ_DESC>low Path #2
</OBJ_DESC>eader #1
</OBJ_DESC>eader #2
</OBJ_DESC>otor #1
</OBJ_DESC>otor #2
</OBJ_DESC>ump #1
</OBJ_DESC>ump #2
</OBJ_DESC>ank #1
</OBJ_DESC>alve #1
</OBJ_DESC>alve #2[/tt]

I get the similar result if I use XXXXX for <OBJ_DESC> and YYYYY for </OBJ_DESC>.

For example:
[tt]YYYYYank Level Controller[/tt]

Any ideas?

CraigMan >:):O>
 
It's a typo in my post, but the script is OK.

CraigMan
 
that's the result of running of the script. Anything wrong with that? [i donn't have gawk - I'm using nawk]:

<OBJ_DESC>Tank Level Controller</OBJ_DESC>
<OBJ_DESC>Flow Path #1</OBJ_DESC>
<OBJ_DESC>Flow Path #2</OBJ_DESC>
<OBJ_DESC>Header #1</OBJ_DESC>
<OBJ_DESC>Header #2</OBJ_DESC>
<OBJ_DESC>Motor #1</OBJ_DESC>
<OBJ_DESC>Motor #2</OBJ_DESC>
<OBJ_DESC>Pump #1</OBJ_DESC>
<OBJ_DESC>Pump #2</OBJ_DESC>
<OBJ_DESC>Tank #1</OBJ_DESC>
<OBJ_DESC>Valve #1</OBJ_DESC>
<OBJ_DESC>Valve #2</OBJ_DESC>
 
I've just determined that I (CraigMan) am officially an idiot! My problem was with the CSV file. It was a DOS file. Once I ran it through dos2unix, my problem was solved.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top