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 -->
  #2 (permalink)  
Old 06-04-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
Something like this:
(use nawk or /usr/xpg4/bin/awk on Solaris)



Code:
awk 'END { 
  printf fmt, ++c, tab, cols, values 
  }
NR == 1 { 
  tab = $1 
  fmt = "REC %d: INSERT INTO %s(%s)\nVALUES\n(%s);\n" 
  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 
  }' FS=" *: *" q="'" input