Tab Delimited file in loop

Hi,

I have requirement to create tab delimited file with values coming from variables.

  1. File will contain only two columns separated by tab.
  2. Header will be added once.
  3. Values will be keep adding upon the script run.
  4. If values already exists then values will be replaced.

I have done so far below work:

#!/bin/bash

NDIR="/opt/nodes/"
DIRS=`ls -l $NDIR | egrep '^d' | awk '{print $9}'`

HEADER="Environment\tNode"

printf "1\ni\n$HEADER \n.\n\nw\nq\n"|ed -s /usr/node.txt

for DIR in $DIRS
do
echo  ${DIR}
NFILE="/opt/nodes/$DIR/config"

ENV=$(sed -n '/<user-props>/,\|</user-props>| {
   s/ *<kv-pair name="environment.name" value="\([^"]*\)"\/>/\1/p
   }' $NFILE/node.xml)
   
NODE=$(sed -n '/<sys-props>/,\|</sys-props>| {
   s/ *<kv-pair name="node.name" value="\([^"]*\)"\/>/\1/p
   }' $NFILE/node.xml)

echo -e "$ENV\t$NODE" >> /usr/node.txt
done

echo "###### END OF SCRIPT ######"

Above piece of code:
Will read node.xml file. Look for props. assign variables as below:
e.g.,

ENV = SystemEnv
NODE = SystemNode

There are multiple Nodes in nodes folder so for loop to take care.

Currently I'm able to write values into output file node.txt

Problems:
First time script will run ok. File will be perfect.
But Next time on run Header will be added again and if the folder contains same node names those will be added.
If I use only ">" method with echo then only last node will be added, ignoring the others.
Header method I used to add always print

?
? 

upon run. I couldn't figure out why.

  1. Is there any other method to add header in tab delimited style.
  2. Shall use sed or awk instead of echo to make output file?
  3. Replace the existing entries.

Help needed to make it working as per needs.
Thanks,

As sed 's applicability is somewhat limited, awk would lend itself to solve your problem. It could even loop through all the input files, eliminating the header problem. Using arrays to hold the values, duplicates could be handled.
To become more concrete, data samples would be necessary.