Visit Our UNIX and Linux User Community


Double echo problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Double echo problem
# 1  
Old 11-29-2010
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
Double echo problem-script-troublejpg
Double echo problem-script-trouble-2jpg
# 2  
Old 11-29-2010
Try changing

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

to

Code:
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. Smilie
# 3  
Old 11-29-2010
See it this works. Instead of this:
Code:
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:
Code:
printf "%-20s%-20s%-50s%-20s\n" "$JUNOS_FILE" "${array0[$COUNTER]%% *}" "${array3[$COUNTER]}" "${array2[$COUNTER]%% *}"

# 4  
Old 11-29-2010
Found it!

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

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

Previous Thread | Next Thread
Test Your Knowledge in Computers #242
Difficulty: Easy
In September 1984 work was completed on restructuring the ARPANET giving U.S. military sites their own Military Network (MILNET) for unclassified defense department communications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing double quotes in echo command

Please help me to use echo or printf type of command to print some value from variable within double quotes - I want to print the double quote ( " ") also. I tried #!/bin/bash VALUE=some_value echo '{"value" : "$VALUE"}' I was expecting the above script would produce .. {"value" :... (3 Replies)
Discussion started by: atanubanerji
3 Replies

2. Shell Programming and Scripting

Double quotes and variable proble in echo

HI Guys, I want to echo below line in my file :- lpd | grep AL | nawk '{print "1dLA - " $0} How can i echo same Output (4 Replies)
Discussion started by: pareshkp
4 Replies

3. Shell Programming and Scripting

[Solved] echo not updating double quotes in output

Hi , I have a script to update firefox proxy. But the echo is not updating the double quotes. paste /home/names.txt /home/ip.txt | while read i j do mkdir $i echo "user_pref("network.proxy.http", "$j");" >> /home/$i/prefs.js echo "user_pref("network.proxy.http_port", 8080);" >>... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

4. Shell Programming and Scripting

echo problem

hi all i have little problem below is my shell script a=`sqlplus fss_cst/fss_cst@dolp1 << EOF SET PAGESIZE 0 FEEDBACK OFF TRIMOUT ON; select process from lfs$ta_process where valid_to_dat=to_date('9/16/2010','mm/dd/yyyy'); EOF` echo ${SQL} the script name is test2.sh when i execute... (5 Replies)
Discussion started by: aishsimplesweet
5 Replies

5. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 Replies

6. Shell Programming and Scripting

Perl echo with double quotes

I need to echo a string that has double quotes in a Perl script. #!/usr/bin/env perl `echo Rule123 -comment \"blah blah\" >> $filename` I'd like to get below appended to $filename: Rule 123 -comment "blah blah" But instead, the double quotes are lost: Rule 123 -comment blah bah ... (1 Reply)
Discussion started by: slchin
1 Replies

7. Shell Programming and Scripting

Problem with echo *

Hello all, Please help with the below. I have a requirement where in I have to read a pattern and print it as shown below. Patterns will be as below. Input Output Pattern Should be printed as below with spaces such that I can awk. -*--* - * - - * *--**... (2 Replies)
Discussion started by: tenderfoot
2 Replies

8. Shell Programming and Scripting

While read do, then echo, gives double output

Sorry about the title, but this should be fairly self-explanatory. My script seems to work, except that in the output, below, the first three lines are correct, but why does it print the additional lines 4, 5, & 6? Script: #!/bin/bash while read; do client="$(grep -m 1 Client | awk... (3 Replies)
Discussion started by: MrKen
3 Replies

9. UNIX for Dummies Questions & Answers

echo $ problem

Hi I am using tcsh. I want display in a file_1 like this. $VARIBALE I gave in a termianl > echo "\$VARIBALE" > file_1 Its not workning. It was giving VARIBALE: Undefined variable. I gave \ before $, but why it was giving undefined varible? Please help me. Thanks in advance (4 Replies)
Discussion started by: chaitubek
4 Replies

10. Shell Programming and Scripting

echo problem

Hi, I have given the following statement in a script to put the values of variables (VAR1, VAR2,...) in a file. echo " $VAR1 $VAR2 $VAR3 $VAR4 $VAR5" >> filename But the output is not coming properly. Variables VAR5, VAR4 are replacing the first (VAR1, VAR2,..). I can't... (5 Replies)
Discussion started by: abrd600
5 Replies

Featured Tech Videos