0

In a shell script, I am trying to convert an CVS file to Excel 2003 XML as described in this answer : https://stackoverflow.com/a/150368

The section below deals with creating the Row elements within the Table element

ROWS=`wc -l < $IN_FILE | sed 's/ //g' `
COLS=`awk -F',' '{print NF; exit}' $IN_FILE`

#for each row in turn, create the elements
for r in {1 to ${ROWS}}
    do
    echo "<Row>\n" >> $OUT_FILE
    for c in {1 to ${COLS}}
    do
        echo "<Cell><Data>`sed -n '${r}p' $IN_FILE | cut -d "," -f ${c} `</Data></Cell> \n"
    done
    echo "</Row>\n" >> $OUT_FILE
done
echo "</Table>" >> $OUT_FILE

I am having problems because sed won't accept the line number as a variable.

I need to print just that row, pipe to a cut to extract just that col.

Also, not sure about using command substituion in the middle of an echo...

Any advise?

Community
  • 1
  • 1
Ben Hamilton
  • 949
  • 3
  • 10
  • 21

2 Answers2

2

The single quotes are making the variable literal. You could try breaking it up this way:

FOO=`sed -n "${r}p" $IN_FILE | cut -d "," -f ${c}`
echo "<Cell><Data>$FOO</Data></Cell> \n"
lurker
  • 56,987
  • 9
  • 69
  • 103
0

replace

echo "<Cell><Data>`sed -n '${r}p' $IN_FILE | cut -d "," -f ${c} `</Data></Cell> \n"

by

sed -n "${r} {s/^/,,/;s/ //g;;s/\(,[^,]*\)\{${c}\}, *\([^,]*\).*/<Cell><Data>\2<\/Data><\/Cell>/p;q;}" ${IN_FILE}
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43