create SQL-statements from textfile with awk

2010-03-08 154 words 1 min read

<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


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it
1