Linux - SoftwareThis 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.