Solved

Bash script to insert text into MYSQL

Posted on 2011-02-24
3
756 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…

733 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