Trying to "accumulate" data - looking for help

lvirden

New Member
Joined
Oct 6, 2017
Messages
7
Reaction score
0
Credits
0
I have a data file of the basic form:
aaa1
-> b
-> c
bbb2
-> b
ccc3
-> d

and what I want as a result is something like
b,aaa1,bbb2
c,aaa1
d,ccc3

So I am trying this code:

typeset -A CI

if [ "$#" -eq 0 ] ; then
echo "USAGE: $0 input_file" >&2
exit 1
fi
prefix="--*> "


exec < "$1"


read line
bussvc=$line
line2=""

while read line2 ; do
case "$line2" in
-* )
plain="${line2#$prefix}"

echo $plain,$bussvc
if [ "CI[$plain]" ] ; then
CI[$plain]="${CI[$plain]},$bussvc"
else
CI[$plain]=$bussvc
fi
;;
* )
bussvc=$line2
line2=""
;;
esac

done

for index in "${!CI[@]}" ; do
print "CI[$index],${CI[$index]}"
done

The first issue I am having is setting $plain. The idea is that I want to skip the first portion of the string when it starts with a -. It could be -> or --> or --->, etc. followed by a space.
It looks to me like bash does not want a regular expression in the pattern to be matched.

Since the most frequent case is "-> ", to try and make progress, I replace the pattern with the static "-> ".

The next issue is this:

(Line 25) read line2


(Line 26) case "$line2" in

(Line 28) plain=ABC-ROVER-HEAD-XYZ

(Line 30) echo ABC-ROVER-HEAD-XYZ,AP_ABC Networking

ABC-ROVER-HEAD-XYZ,AP_ABC Networking

(Line 31) '[' 'CI[ABC-ROVER-HEAD-XYZ]' ']'

./genbussvc.ksh: line 32: CI: bad array subscript

(Line 32) CI[$plain]=',AP_ABC Networking'

./genbussvc.ksh: line 32: CI[$plain]: bad array subscript


Why is the string called a bad array subscript?

Thank you for your help.
 


Thanks to the lack of [ code ] [ /code ] tags in your post, it took me a little while to make sense of your post.

But after copy/pasting the code for your script into vim and auto-indenting it, I think I can see what's going on now.

The problem was the call to "print" in the "for... in" loop.

Print is a utility which is part of run-mailcap from the mailutils package - which contains some GNU email related programs. The script was complaining because it expects a path to a file to run. Did you mean to use printf?

Anyway - to fix it, I used echo instead of print....Which gets rid of the error message and outputs something similar to what you want:
Code:
#!/bin/bash

typeset -A CI

if [ "$#" -eq 0 ] ; then
   echo "USAGE: $0 input_file" >&2
   exit 1
fi
prefix="--*> "


exec < "$1"


read line
bussvc=$line
line2=""

while read line2 ; do
   case "$line2" in
       -* )
           plain="${line2#$prefix}"
          # i assume the line below was for debug purposes, so I have temporarily commented it!
           #echo $plain,$bussvc
           if [ "CI[$plain]" ] ; then
               CI[$plain]="${CI[$plain]},$bussvc"
           else
               CI[$plain]=$bussvc
           fi
           ;;
       * )
           bussvc=$line2
           line2=""
           ;;
   esac

done

for index in "${!CI[@]}" ; do
   echo $CI[$index]${CI[$index]}
done

I'm not sure about the second half of your problem - I haven't quite de-cyphered that part of your post yet.
Hope this is of some help!
 

Members online


Latest posts

Top