Operating System - HP-UX
1748254 Members
4109 Online
108760 Solutions
New Discussion юеВ

Re: print field seperators

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

print field seperators

Hi all,

I have setup an array in a config file and what to use it in two ways - here is the array:

ECM_PORT[1]=443
ECM_PORT[2]=7943
ECM_PORT[3]=7843
ECM_PORT[4]=7080
ECM_PORT[5]=7090

the first is to print the array on one line field seperator being a "+"

# echo ${ECM_PORT[*]}
443 7943 7843 7080 7090

so I want to replace the white space with:

443+7943+7843+7080+7090

the string is to be executed in a command and the array will contain a different number of elements depending on the environment being configured.

The second function is pretty straight forward and can workout myself :-)

Thanks

Chris.
hello
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: print field seperators

Hi Chris:

You could use 'tr' as:

#!/usr/bin/sh
set -A EMC_PORT
ECM_PORT[1]=443
ECM_PORT[2]=7943
ECM_PORT[3]=7843
ECM_PORT[4]=7080
ECM_PORT[5]=7090
echo "${ECM_PORT[*]}"|tr -s " " "+"

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: print field seperators

quite simple if I just took a step back :-)

I was playing around with on something like:

echo ${ECM_PORT[*]} |awk 'BEGIN{RS="\n";ORS="+"};{ for ( i = NF; i > 0; i = i - 1 ) print $i}'

which prints:

7090+7080+7843+7943+443+

this obviously prints backwards and leaves a "+" at the end with no carriage return.

my way needs a bit more work to print in order.

Thanks anyway james - your help is always appreciated.

Chris
hello
lawrenzo_1
Super Advisor

Re: print field seperators

I thought I'd have a play and came up with:

echo ${ECM_PORT[*]} |awk '{ for ( i = 1; i <= NF; i = i + 1 )
if ( i == 1 ) out = $i
else out = out"+"$i
print out}'
443+7943+7843+7080+7090

Thanks again

Chris
hello
James R. Ferguson
Acclaimed Contributor

Re: print field seperators

Hi (again) Chris:

If you prefer your 'awk', then:

# echo "${ECM_PORT[*]}"|awk '{for (i=1;i
...or:

# echo "${ECM_PORT[*]}"|awk '{for (i=1;i
Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: print field seperators

>the first is to print the array on one line field separator being a "+"

Why is everyone working so hard? :-)
IFS_SAVE=$IFS
IFS="+"
echo ${ECM_PORT[*]}
IFS=$IFS_SAVE

lawrenzo_1
Super Advisor

Re: print field seperators

I get your point Dennis :-)

thanks both and I'll be using the first solution as this is probably the most effecient and understandable but wanted to play around !

Chris.
hello