The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #5 (permalink)  
Old 06-04-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
Change the script like this:


Code:
#! /usr/xpg4/bin/awk -f

END {
  printf fmt, ++c, tab, cols, values
  }
NR == 1 {
  tab = $1
  fmt = "REC %d: INSERT INTO %s(%s)\nVALUES\n(%s);\n"
  FS = " *: *"
  q = "'"
  next
  }
/^REC/ && cols {
  printf fmt, ++c, tab, cols, values
  cols = ""
  values = ""
  next
  }
{
  $2 = $2 ~ /^[0-9]/ ? $2 : q $2 q
  cols = cols ? cols "," $1 : $1
  values = values ? values "," $2 : $2
  }

And invoke it like this:


Code:
./script datafile