?

Log in

No account? Create an account

Previous Entry | Next Entry

Pushing data to Mysql using BASH

Assume your application generates some output in a file named "fan.out" in the following format.

FAN_NAME|TIME|RPM|STATUS
FAN1|1195699322|4566|Moderate Condition
FAN12|1195699112|3562|Bad Condition
FAN11|1195699321|5676|Good Condition
FAN15|1195699117|8923|Good Condition
..
..



You want these data to be automatically pushed to your mysql "FAN.DETAILS" table through a BASH script.

This is how I did that.

Step1: Created the database and table in mysql as below.


so the db is ready.

Step2: From the above fan.out, my idea for creating the corresponding .sql

$ sed -e 1d -e 's/|/","/g'  -e 's/^/insert into FAN.DETAILS values ("/' -e 's/$/");/' fan.out > /tmp/fan.out.sql
insert into FAN.DETAILS values ("FAN1","1195699322","4566","Moderate Condition");
insert into FAN.DETAILS values ("FAN12","1195699112","3562","Bad Condition");
insert into FAN.DETAILS values ("FAN11","1195699321","5676","Good Condition");
insert into FAN.DETAILS values ("FAN15","1195699117","8923","Good Condition");

Step3: Here my script "push2mysql.sh" is ready

#!/bin/bash
#http://jaduks.livejournal.com/

SQLFILE=/tmp/fan.out.sql
trap "rm -f $SQLFILE" EXIT

MYUSER=root
INPUTF=./fan.out
TABLE="FAN.DETAILS"

sed -e 1d -e 's/|/","/g'  -e 's/^/insert into '"$TABLE"' values ("/' -e 's/$/");/' $INPUTF > $SQLFILE
echo "quit" >> $SQLFILE

/usr/local/mysql/bin/mysql -u $MYUSER  < $SQLFILE
echo "Completed"



<A Sample RUN>
$ ./push2mysql.sh
Completed

<FROM MYSQL>

Hope you like this post :-) Got to go to office, getting late.

Profile

jaduks
Jadu Kumar Saikia

Latest Month

January 2008
S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  
Powered by LiveJournal.com
Designed by chasethestars