Solved

Bash script to insert text into MYSQL

Posted on 2011-02-24
3
764 Views
Last Modified: 2012-05-11
Hi,

I have a table with the following fields:

PDI Number, Category, Reference, Description, Status, Example, Script Version, UNIX Checklist Page.

Now the list is going will have several PDI's and each one will have all of the fields above.

I look forward hearing from you.

Regards,

Michael
PDI Number: EXA000260
Category: FAT II
Reference: LINUX 3.1
Description: A shared account is not documented by the IAO.
Status: Open

For example:
ZEN000260: The following accounts require justifying documentation from the IAO.
ACCT     LOGGED IN FROM
-------   --------------
sysadmin Fri
sysadmin Mon
sysadmin Sat
sysadmin Sun
sysadmin Thu
sysadmin Tue
sysadmin Wed

Script Version: Jan2009
UNIX Checklist Page: 30

Open in new window

0
Comment
Question by:dpoper1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Expert Comment

by:MarioAlcaide
ID: 34977416
Hello,

You can use the command LOAD DATA for that purpose. For example: mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;

http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html
0
 
LVL 4

Expert Comment

by:MarioAlcaide
ID: 34978156
Was this helpful for you?
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 34980184
There's not a super-easy way to do this, but you would probably start with a perl script

makesql.pl
while (<>)
{
m/PDI Number: ([^\n]*)\nCategory: ([^\n]*)\nReference: ([^\n]*)\nDescription: ([^\n]*)\nStatus: ([^\n]*)\n\nFor example:\s*(.*?)\nScript Version: ([^\n]*)\nUNIX Checklist Page: ([^\n]*)/s;
print "INSERT table (pdi,category,reference,description,status,example,script_version,page) VALUES ('$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8');\n";
}

Then you could do something like

perl makesql.pl < textfile.txt |mysql -uUSER -pPASS DATABASE
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question