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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-30-2014, 06:26 PM
|
#1
|
Member
Registered: Jan 2012
Posts: 72
Rep:
|
mysql insert issue in bash script
Dear All,
I am writing a bash script which can read data from file. File contains output of "last" command with some awk tricks.
Following is my file data format.
kamran pts/0 10.111.11.51 Thu-Oct-30 22:33 logged
kamran pts/0 10.111.11.51 Thu-Oct-30 21:06 22:33
kamran pts/0 10.111.11.51 Wed-Oct-29 21:29 05:00
kamran pts/1 10.111.11.51 Tue-Oct-28 22:48 04:33
kamran pts/0 10.111.11.51 Tue-Oct-28 21:21 00:21
Now my bash script is following to put data in mysql database.
Issue I am facing is that the below script inserted data from file with picking up the 1st row of my data file i.e all rows in mysql table has same records matching with row1 of data file.
#/!bin/bash
#Defining Variables
ssh_sessions_file="/home/kamran/ssh_logins.txt"
#Begin Code
# extrcting the Information of ssh logins from file ssh_logins.txt
#
NumberOfLines=$(cat $ssh_sessions_file | wc -l)
j=1
while [ $NumberOfLines -gt 0 ]
do
Login_Name=$(cat $ssh_sessions_file | awk '{print $1}')
Login_Terminal=$(cat $ssh_sessions_file | awk '{print $2}')
Login_IP=$(cat $ssh_sessions_file | awk '{print $3}')
Login_Date=$(cat $ssh_sessions_file | awk '{print $4}')
Login_Start_Time=$(cat $ssh_sessions_file | awk '{print $5}')
Login_End_Time=$(cat $ssh_sessions_file | awk '{print $6}')
echo $Login_Name
echo $Login_Terminal
echo $Login_IP
echo $Login_Date
echo $Login_Start_Time
echo $Login_End_Time
let "j +=1"
let "NumberOfLines -=1"
mysql -u root -ppassword ssh_logins <<- _END_
INSERT INTO ssh_logins (Login_Name, Login_Terminal, Login_IP, Login_Date, Login_Start_Time, Login_End_Time) VALUES ('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Time');
_END_
done
##################
Please anybody can help why this script duplicating 1st row in all my mysql table rows. Why not inserting correct row records from file with help of loop in script.
regards,
kamran
|
|
|
10-30-2014, 06:56 PM
|
#2
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,464
|
Looks complicated. Instead of the bash script, why not use SQL?
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
|
|
1 members found this post helpful.
|
10-31-2014, 01:38 AM
|
#3
|
Member
Registered: Mar 2013
Posts: 636
Rep:
|
Your script says, database name is ssh_logins as well as table name is ssh_logins. Is it confirmed?
You should not run queries like this from a bash script
Try this code
Code:
mysql -u root -p'password' ssh_logins -B -e "INSERT INTO ssh_logins (Login_Name, Login_Terminal, Login_IP, Login_Date, Login_Start_Time, Login_End_Time) VALUES ('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Time');"
|
|
1 members found this post helpful.
|
10-31-2014, 10:22 AM
|
#4
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,418
|
Quote:
Originally Posted by kamran.ayub
Dear All,
I am writing a bash script which can read data from file. File contains output of "last" command with some awk tricks. Following is my file data format.
kamran pts/0 10.111.11.51 Thu-Oct-30 22:33 logged
kamran pts/0 10.111.11.51 Thu-Oct-30 21:06 22:33
kamran pts/0 10.111.11.51 Wed-Oct-29 21:29 05:00
kamran pts/1 10.111.11.51 Tue-Oct-28 22:48 04:33
kamran pts/0 10.111.11.51 Tue-Oct-28 21:21 00:21
Now my bash script is following to put data in mysql database. Issue I am facing is that the below script inserted data from file with picking up the 1st row of my data file i.e all rows in mysql table has same records matching with row1 of data file.
Please anybody can help why this script duplicating 1st row in all my mysql table rows. Why not inserting correct row records from file with help of loop in script.
|
Similar to your other thread:
http://www.linuxquestions.org/questi...db-4175523722/
There are THOUSANDS of very easily found bash scripting tutorials...even one in my posting signature, and many others online that have examples on how to do MySQL inserts from files. Have you looked at ANY of them?
And I'll again suggest, as both Guttorm and eklavya have, that you just use the standard MySQL commands to load a file.
|
|
|
10-31-2014, 11:42 AM
|
#5
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear eklavya,
Yes I have set DB name and table name ssh_logins. I have tried your provided query.
But result is same that it is inserting 1st row record of my data file in all rows of my mysql table.
Any other suggestion please.
Regards,
Kamran
|
|
|
10-31-2014, 01:38 PM
|
#6
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,418
|
Quote:
Originally Posted by kamran.ayub
Dear eklavya,
Yes I have set DB name and table name ssh_logins. I have tried your provided query. But result is same that it is inserting 1st row record of my data file in all rows of my mysql table.
Any other suggestion please.
|
You got other suggestions; did you read and understand them??
AGAIN: you need to use the native MySQL commands both correctly, and use all of their capabilities. You were already provided links that tell you how to import a CSV file using nothing by MySQL...did you read that? Try it?
And again, it was suggested that you look at any of the bash scripting tutorials..did you? Because if you're only getting the first row of data, it would seem VERY obvious that you aren't looping through the file correctly.
|
|
|
10-31-2014, 02:57 PM
|
#7
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear All,
Thanks for your replies and suggestions.
Issue resolved by reading output file in csv format. Following is now a simple bash script code for doing all.
#/!bin/bash
IFS=,
while read Login_Name Login_Terminal Login_IP Login_Date Login_Start_Time Login_End_Time
do
echo "INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time) VALUES ('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Time');"
done < /home/kamran/output.csv | mysql -u root -p asterisk ssh_logins;
#################
Regards,
Kamran
|
|
1 members found this post helpful.
|
10-31-2014, 03:53 PM
|
#8
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,418
|
Quote:
Originally Posted by kamran.ayub
Dear All,
Thanks for your replies and suggestions. Issue resolved by reading output file in csv format. Following is now a simple bash script code for doing all.
Code:
#/!bin/bash
IFS=,
while read Login_Name Login_Terminal Login_IP Login_Date Login_Start_Time Login_End_Time
do
echo "INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time) VALUES ('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Time');"
done < /home/kamran/output.csv | mysql -u root -p asterisk ssh_logins;
|
Glad it's working, but: - You were given advice SEVERAL TIMES about how you are doing that insert. You were told it was a bad idea, yet you did it anyway.
- You were given advice about how to use the built-in MySQL tools that already exist to do nothing BUT import CSV files, but didn't acknowledge or use them.
- You were asked to use CODE tags around your code, and didn't
If you're going to ask for advice and suggestions, you might want to consider at least ACKNOWLEDGING them. If you're going to ignore what people tell you, there's little point in posting.
|
|
|
All times are GMT -5. The time now is 01:08 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|