Double echo problem

I'm parsing a router configuration file and printing out some of the fields. Given the following output,
I'd like to add the filename to the first column. I'm definately a neophyte in scripting.

COUNTER=${#array1
[*]}

while [ $COUNTER -ne -1 ]; do
######## echo -n $JUNOS_FILE | awk '{ printf "%-20s" , $0 }'
echo ${array0[$COUNTER]} | awk '{ printf "%-20s" , $1 }'
echo ${array3[$COUNTER]} | awk '{ printf "%-50s" , $0 }'
echo ${array2[$COUNTER]} | awk '{ printf "%-20s \n" , $1 }'
let COUNTER=COUNTER-1
done

------------------------- OUTPUT ---------------------------------------------------------------

lo0 LB rtr-management 127.0.0.1/32
t1-1/0/1 TZ Unused
t1-1/0/0 S3/0/16:0 ATT 69DHZA382XXX 10.255.74.62/30
ge-0/0/3 GE Reserved for WAN
ge-0/0/2 GE ZZZ-Test 10.213.0.109/30
ge-0/0/1 GE Unused-ge-0/0/1
ge-0/0/0 GE ECD_28307_00 Juniper J2320 10.213.101.19/27

-----
-----

When I try to add the filename the I get an extra row on top that I don't want. Appears to be
the filename echos twice somehow.

Any ideas??

COUNTER=${#array1
[*]}

while [ $COUNTER -ne -1 ]; do
echo -n $JUNOS_FILE | awk '{ printf "%-20s" , $0 }'
echo ${array0[$COUNTER]} | awk '{ printf "%-20s" , $1 }'
echo ${array3[$COUNTER]} | awk '{ printf "%-50s" , $0 }'
echo ${array2[$COUNTER]} | awk '{ printf "%-20s \n" , $1 }'
let COUNTER=COUNTER-1
done

------------------------- OUTPUT ---------------------------------------------------------------

router-config-1
router-config-1 lo0 LB rtr-management 127.0.0.1/32
router-config-1 t1-1/0/1 TZ Unused
router-config-1 t1-1/0/0 S3/0/16:0 ATT 69DHZA382XXX 10.255.74.62/30
router-config-1 ge-0/0/3 GE Reserved for WAN
router-config-1 ge-0/0/2 GE ZZZ-Test 10.213.0.109/30
router-config-1 ge-0/0/1 GE Unused-ge-0/0/1
router-config-1 ge-0/0/0 GE ECD_xxxxx_00 Juniper J2320 10.213.101.19/27

Try changing

echo -n $JUNOS_FILE |  awk '{ printf "%-20s" , $0 }'

to

echo -n $JUNOS_FILE |  awk '{ printf "%-20s" , $1 }'

HTH

ps. awk is a bear of a scripting language to learn. but once you've written a few scripts it becomes a very powerful language to use. :smiley:

See it this works. Instead of this:

echo -n $JUNOS_FILE |  awk '{ printf "%-20s" , $0 }'
echo  ${array0[$COUNTER]} | awk '{ printf "%-20s" , $1 }'
echo  ${array3[$COUNTER]} | awk '{ printf "%-50s" , $0 }'
echo  ${array2[$COUNTER]} | awk '{ printf "%-20s \n" , $1 }'

Try this:

printf "%-20s%-20s%-50s%-20s\n" "$JUNOS_FILE" "${array0[$COUNTER]%% *}" "${array3[$COUNTER]}" "${array2[$COUNTER]%% *}"

Thanks for the replies bluescreen and Scrutinizer. Scrutinizer your code is prettier than mine, I'm going to use it :slight_smile:

OK .. so I got the same results with your two code snippets.

I started to wonder if maybe I had a line-feed in my file name variable so I used sed to strip out the last character. NOPE .. that was not it!

I found the problem when I referenced the numerical array element instead of using the $COUNTER variable. What I finally figured out was that $COUNTER did indeed count all the elements of my arrays .... BUT the array elements begin with 0 ... ie array1[0]. So all this time I was trying to print out an array element that did not exist the first time thru the loop.

Solution: decrement the #COUNTER before beginning the loop

COUNTER=${#array1
[*]}
COUNTER=COUNTER-1
while [ $COUNTER -ne -1 ]; do
echo -n $JUNOS_FILE | awk '{ printf "%-20s" , $0 }'
echo ${array0[$COUNTER]} | awk '{ printf "%-20s" , $1 }'
echo ${array3[$COUNTER]} | awk '{ printf "%-50s" , $0 }'
echo ${array2[$COUNTER]} | awk '{ printf "%-20s \n" , $1 }'
let COUNTER=COUNTER-1
done