Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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 was working on shell script in which I have multiple Insert is performing on single table getting data from different table. But sometime it works correctly and sometime it does not.
I am in search is there any way through which I can story my query result into a file. Please do not take me wrong by the words query result, as here Query Result means the result that you get when you run your query i.e, 12 rows effected.
I have tried bla bla method but it did not work for me.
For your help i m providing you the sample :
Query is as follows :
INSERT INTO table_name1(fk_req_id,cli,points,source,gift_redeemed,gift_code,points_befor_redmptn,points_after_re dmptn,NAME,address,city,state,pin,age,dateofbirth,brand,COMMENT,intensity,aniversary,education,servi ce,business,completestatus,update_date)select fk_req_id,cli,ttl_points,'PANEL',gift_redeemed,gift_code,points_befor_redmptn,points_after_redmptn,n ame,address,city,state,pin,age,dateofbirth,curr_barand,comment,intensity,aniversary,education,servic e,business,completestatus,insert_date from table_name2 where date(insert_date) = date_sub(current_date,interval 4 day)" >>$myfile
Here $myfile is the log path where I want to write the logs or redirect my output.
Any help and suggestion would be helpful.
regards,
Pranjal
Last edited by onebuck; 12-14-2012 at 04:31 PM.
Reason: correct title from move post actions by mod
I was working on shell script in which I have multiple Insert is performing on single table getting data from different table. But sometime it works correctly and sometime it does not.
Redirecting "12 rows effected" using a piped redirect is NOT the way to do debugging of shell scripts.
Quote:
I am in search is there any way through which I can story my query result into a file
What query is that? You are doing inserts.
It's already in a file. (see "working on shell script")
Quote:
I have tried bla bla method but it did not work for me.
Never heard of it. Did you author this method too?
Redirecting a shell script for an "Insert into" statement(s), near as I can remember,
the only output IS "nn rows effected"
An insert query on the command line does not return any message about the number of inserted rows. The only thing you can do is to check if the query was executed succesfully.
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null);"
Enter password:
wim@VirtualBox:~$ echo $?
0
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null);"
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
wim@VirtualBox:~$ echo $?
1
wim@VirtualBox:~$
What you're after is probably the following.
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null); select row_count()"
Enter password:
+-------------+
| row_count() |
+-------------+
| 1 |
+-------------+
Depending on your needs, you can make it a bit easier for further parsing
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null); select row_count()\G"
Enter password:
*************************** 1. row ***************************
row_count(): 1
wim@VirtualBox:~$
You can get rid of the '**....**' using grep -v and you can get rid of the row_count() using cut (both used in the example below)
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13','1989-05-13'); select row_count()\G" | cut -d: -f 2 |grep -v "\*"
Enter password:
1
wim@VirtualBox:~$
And lastly you can redirect the output to a file
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13','abc'); select row_count()\G" | cut -d: -f 2 |grep -v "\*" > mysql.test.txt
Enter password:
wim@VirtualBox:~$ cat mysql.test.txt
1
wim@VirtualBox:~$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.