create SQL-statements from textfile with awk

2010-03-08 1 min read Linux

<a href="http://feedproxy.google.com/~r/Command-line-fu/~3/lz9uEhVxEEk/create-sql-statements-from-textfile-with-awk">create SQL-statements from textfile with awk

  <td>
    <div class="text codecolorer">
      $ $ awk \'{printf "select * from table where id = %c%s%c;\\n",39,$1,39; }\' inputfile.txt
    </div>
  </td>
</tr>
1

inputfile.txt is a space-separated textfile, 1st column contains the items (id) I want to put into my SQL statement.

39 = charactercode for single tick \’

    <td>
      <div class="text codecolorer">
        1 = first column
      </div>
    </td>
  </tr>
</table>

If inputfile.txt is a CSV-file separated by \”,\” use FS= to define your own field-separator:

1
    <td>
      <div class="text codecolorer">
        awk \'BEGIN {FS=","; }{printf "select * from table where id = %c%s%c;\\n",39,$1,39; }\' inputfile.txt
      </div>
    </td>
  </tr>
</table>
  • <a href="http://www.commandlinefu.com/commands/view/3562/create-sql-statements-from-textfile-with-awk">View this command to comment, vote or add to favourites
  • <a href="http://feeds2.feedburner.com/commands/by/alvinx">View all commands by alvinx

<a href="http://www.commandlinefu.com"><img src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/header-logo.jpg" alt="commandlinefu.com" />

by David Winterbottom (<a href="http://codeinthehole.com">codeinthehole.com)

<a href="http://feedads.g.doubleclick.net/~a/1JwMtcXfp4EH4f6maLBHa0KsqhE/0/da"><img src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/~a/1JwMtcXfp4EH4f6maLBHa0KsqhE/0/di" border="0" alt="" />

<a href="http://feedads.g.doubleclick.net/~a/1JwMtcXfp4EH4f6maLBHa0KsqhE/1/da"><img src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/~a/1JwMtcXfp4EH4f6maLBHa0KsqhE/1/di" border="0" alt="" />

<img src="http://blog.amit-agarwal.com/wp-content/uploads/img.zemanta.com/~r/Command-line-fu/~4/lz9uEhVxEEk" alt="" width="1" height="1" />

URL: <a href="http://feedproxy.google.com/~r/Command-line-fu/~3/lz9uEhVxEEk/create-sql-statements-from-textfile-with-awk">http://feedproxy.google.com/~r/Command-line-fu/~3/lz9uEhVxEEk/create-sql-statements-from-textfile-with-awk

comments powered by Disqus
1