<p>I have to read data from some files and insert the data into different tables in a database. Is Unix shell script powerful enough to do the job?</p>

<p>Is it easy to do the job in shell script or should I go about doing this in Java?</p>

[{'answer_id': 71789, 'author': 'Josti', 'author_id': 11231, 'author_profile': 'https://Stackoverflow.com/users/11231', 'pm_score': 0, 'selected': False, 'text': '<p>Can\'t test it right now, but something like:</p>\n\n<pre class="lang-sh prettyprint-override"><code>echo "INSERT INTO foo (b,a,r) VALUES (1,2,3);" | \n    mysql -u user -psecret -h host database\n</code></pre>\n\n<p>in a shell script should work. Don\'t know about getting Data out of it though</p>\n'}, {'answer_id': 71797, 'author': 'Jean', 'author_id': 7898, 'author_profile': 'https://Stackoverflow.com/users/7898', 'pm_score': 2, 'selected': False, 'text': "<p>it is possible :\nUsing your unix shell script, generate an sql script and use the cli to the database to execute the sql script. </p>\n\n<p>if the amount of information is small enough you could build the SQL in memory, I advise against it though since you never know what the future holds (and it could be very large amount of data). Using one call per request doesn't allow you to benefit from bulk operations which are sometimes available.</p>\n"}, {'answer_id': 71799, 'author': 'Charles Ma', 'author_id': 11708, 'author_profile': 'https://Stackoverflow.com/users/11708', 'pm_score': 2, 'selected': False, 'text': "<p>You can, but it might be a bit ugly, for example if you're using mysql and suppose you have an SQL string stored in $sql</p>\n\n<pre><code>echo $sql | mysql -u[user] -p[password] -h[host]\n</code></pre>\n\n<p>p.s. it might be a good idea to tell us what database you're using so we can offer more specific help :p</p>\n\n<p>edit: changed the example line so it actually works</p>\n"}, {'answer_id': 71804, 'author': 'lbz', 'author_id': 11530, 'author_profile': 'https://Stackoverflow.com/users/11530', 'pm_score': -1, 'selected': False, 'text': '<p>Shell scripting (Bash or similar) primary intention is not to deal with databases. Go for Java or even better, ride this opportunity to learn the basics of a scripting language like Python or Ruby.</p>\n'}, {'answer_id': 71834, 'author': 'Yining', 'author_id': 6506, 'author_profile': 'https://Stackoverflow.com/users/6506', 'pm_score': 1, 'selected': False, 'text': "<p>Pipe is your friend.</p>\n\n<p>For example, in MySQL:\n</p>\n\n<pre><code>echo 'load data infile /path/to/the/file into table table_name ...' | \n    mysql -u mysql_user_id -p \n</code></pre>\n\n<p>should do the work.</p>\n\n<p>Provided your file is somehow structured e.g. comma/tab separated etc.</p>\n\n<p>For details, check the manual for your database.</p>\n"}, {'answer_id': 71855, 'author': 'Mike Desjardins', 'author_id': 10466, 'author_profile': 'https://Stackoverflow.com/users/10466', 'pm_score': 2, 'selected': False, 'text': "<p>Of course you can, assuming that you've got a command-line SQL client handy!  I've done it w/ Sybase and the isql command-line client.  You can even get clever and send stuff through awk and send scripts to generate commands on the fly.  It might not be the most efficient way to do everything, but there's plenty of opportunity to flex your Unix hacker mojo.</p>\n"}, {'answer_id': 72018, 'author': 'Michael Cramer', 'author_id': 1496728, 'author_profile': 'https://Stackoverflow.com/users/1496728', 'pm_score': 3, 'selected': True, 'text': '<p>If the data you are trying to import is in a reasonable format -- comma-delimited, for example -- and your database server has reasonable command line utilities, this should be no problem. MySQL has the "mysqlimport" command-line tool that will accept various arguments describing the format of the file:</p>\n\n<pre class="lang-sh prettyprint-override"><code>mysqlimport \\\n    --fields-terminated-by=, \\\n    --ignore-lines=1 \\\n    --fields-optionally-enclosed-by=\'"\' &lt; datafile.txt\n</code></pre>\n\n<p>Passing the data through perl/sed/awk one-liners can help with getting it in the proper format, and the shell script can easily handle prompting for filenames, handling arguments, etc.</p>\n\n<p>Using the various command-line tools provided by Unix is the entire point of bash scripting. Perl, mysql, etc. are all part of that toolkit.</p>\n'}, {'answer_id': 75812, 'author': 'eckes', 'author_id': 13189, 'author_profile': 'https://Stackoverflow.com/users/13189', 'pm_score': 0, 'selected': False, 'text': "<p>It depends on your Database Management System. Most of them have powerfull shell tools for importing data, doing even some ETL functions. Those tools could be even very performant if they support bulk loading - usually Java JDBC can't do that so easily.</p>\n"}]
['https://Stackoverflow.com/questions/71775', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']