LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-11-2016, 12:27 PM   #1
dmchess
Member
 
Registered: Jan 2005
Posts: 189

Rep: Reputation: 29
Mysql and awk


I don't know if anyone else does this:

Changes in mysql/mariadb have made LoadData Infile harder to use. It is now just easier to use awk instead.

This is a standard script to import a csv into a mysql table MyTable and using the database MyDataBase:

cat MyInputFile.csv | awk -F, '{printf("\nInsert Into MyTable (MyVarChar, MyNum) Values(%c%s%c, %d);",34, $1, 34, $2)}' | mysql --user=MyUser --Password=MyPassword MyDataBase

The decimal 34's translate to the double quote character, allowing you to encase your string fields. No mucking around with sed required.
 
Old 06-11-2016, 09:13 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,390

Rep: Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426Reputation: 3426
Being a simple soul, I prefer not to have to remember what code is what - you can embed the double-quote directly if escaped. This should also work
Code:
awk -F, '{printf("\nInsert Into MyTable (MyVarChar, MyNum) Values(\"%s\", %d);", $1, $2)}' MyInputFile.csv
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK: extract mysql query + info pix9 Programming 5 11-23-2015 04:41 AM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 10:06 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. SpellChainz Linux - Newbie 1 06-23-2007 04:35 PM
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 09:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration