Archive

Archive for the ‘Quick Hacks’ Category

Export result of query from mysql console to CSV file

April 13th, 2010 Michael Stepanov 2 comments

Sure all GUI DB tools can do export of result of query into CSV file. But what if you have to do this from command line? There is a simple way perform that task. Here is an example:

SELECT *
INTO OUTFILE ‘/tmp/products.csv’
FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘
ESCAPED BY ‘\’
LINES TERMINATED BY ‘\n’
FROM products

When you run that query all records from the table products will be dumped into file /tmp/products.csv. All fields of that dump will be delimited by comma, enclosed by double quotes and escaped by two slashes. Each row will be ended by end of the line character.

Sure you can change delimiter, enclose, escape symbols as well as add any others SQL statements such JOIN, WHERE, LIMIT ORDER etc. Enjoy!

UPDATED: Don’t forget to drop the export file before re-export data. Otherwise you’ll get the error:

ERROR 1086 (HY000): File ‘/tmp/products.csv’ already exists

[via Electrictoolbox]

CSV files and IE

August 9th, 2007 Michael Stepanov 2 comments

Today I spent about three hours to play with “extremely practical and user friendly” interface of WinXP. The problem was that Windows is too smart (at least for me). It tries to foresee your desires. But as result you wast your time to switch off next “intelligent” feature. In my case I heed to download CSV file from site supported IE only. But IE opens that file in browser window with Excel, corrupted its data. Finally I found solution:

  • Open Control Panel
  • Open Folder Options
  • Click File Types
  • Locate “CSV” and click the Advanced button
  • Check the box that says “Confirm open after download”
  • Do the same for “XLS Microsoft Excel Worksheet” as well
  • Click Ok

It was amazing. I checked Alan Simpson’s Windows XP Bible and O’Reilly – Fixing Windows XP Annoyances but without luck. Maybe that issue so trivial that everybody knows how to solve it. Anyway, once again I saw that Windows is not my choice!

Sending email with attachment from command line

May 31st, 2007 Michael Stepanov No comments

Recently I needed to send a script to one of my co-workers from the server. One way to do that is copy the script using scp to my PC and send it as usual. But there is a more efficient way to do that with mutt:

$ mutt -s “Subject” -a /tmp/file.tar.gz your@email.addr < /tmp/message-body.txt

I hope that all parameters are understandable. So, having mutt and using that command you can easily send email with attached file from command line.

Categories: Digital Life, Quick Hacks Tags:

Keep your mobile phone book online!

May 31st, 2007 Michael Stepanov No comments

Mobical
Once Alexey suggested me to use online service Mobical to store info from my mobile phone. I didn’t start use it immediately. But recently I decided to try it. It’s really cool and useful service. Once registered there you can synchronize phone book, calendar, tasks and other stuff from your mobile phone with your online account. Moreover, it’s possible to export your contacts as vCard-file and calendar as vCard-file or iCalendar-file for backup or for some other reason.

Categories: Digital Life, Quick Hacks Tags:

Print from Linux via Samba

February 23rd, 2007 Michael Stepanov No comments

Recently I faced with problem of printing from the Linux server to the printer connected to the Windows PC via USB. So, here is a brief how to do it using CUPS.
1. Type http://localhost:631/ in your browser to open CUPS web interface and go to “Add new printer” page.
2. For device choose “Windows Printer via SAMBA”. If this options isn’t presented in the drop down list run following command as root:

ln -s `which smbspool` /usr/lib/cups/backend/smb

3. Device URI type in following format:

smb://win_user:win_pass@win_domen/win_PC/printer_name

4. Choose appropriate driver for your printer. I didn’t find my model in the list. So, I chose one from Generic:

Generic -> Generic PCL 6/PCL XL Printer Foomatic/pxlmono

For more information, please refer to OpenPrinting database.

See also:

Categories: Linux, Quick Hacks Tags: , , , ,

Perl Oneliner: Recursive Search and Replace

February 15th, 2007 Michael Stepanov No comments

I like Perl and I use it every day. It’s cool to make a simple scripts in one line to do some routine work. For example, my first Perl oneliner formats Apache log to be easy readable. Here is another good example of Perl onliner (it isn’t my actually). It makes a recursive search and replacement:

Perl -p -i -e ‘s/oldstring/newstring/g’ `grep -ril oldstring *`

[via About: Perl]

Spam protected contact form

February 5th, 2007 Michael Stepanov 2 comments

Recently I started to receive a lot of spam messages from contact form of my blog. It really makes me crazy! I hate spam and I hate spammers! Great plugin SpamKarma protects my posts but it doesn’t do anything with contact form. So, I googled it and found a patched version of Ryan Duff’s plugin. There is a customized question which helps to identify a human (2+2 by default). It can be changed easily via admin interface. Enjoy!

How to print PDF directly to the printer

February 2nd, 2007 Michael Stepanov No comments

We use following approach to build print view of documents in our web-based application. At the begging the document template is filled by real data and HTML page is generated. After that this page is sent to HTMLDOC which converts it into PDF. The users open the document via Acrobat plugin installed in the web browser. This approach has worked fine for years.
But recently we decided to send PDF directly to the printer. I’ve added printer to the our server and implemented a simple function (we have a Xerox printer with PostScript support):

lp -d printer_name -o sides=two-sided-long-edge -o media=A4 -o portrait -o page-ranges=1-7 PDF_file

The strange thing appeared right after that. One type of documents couldn’t print anymore. No errors in the CUPS log or on the printer display. The job just disappeared from the queue and was marked as printed. After spending some time I found the way to solve this problem. I use acroread to convert PDF into PostScript and then send it to the printer:

acroread -toPostScript PDF_file

Acroread creates the PostScript file with the same name but with different extension (.ps) and stores it in the current directory. But you can pass desired directory name as second parameter:

acroread -toPostScript PDF_file /store-dir/

I use Acrobat 5 from DAG repository. It works fine even the server doesn’t have installed X. But if you want to use Acrobat 7 you should do additional work. It needs X and if try to run it from command line it give you an error:

(acroread:6488): GTK-WARNING **: cannot open display:

To solve this just install Xvfb – X server that can run on machines with no display hardware and no physical input devices, and run following command:

xinit acroread -toPostScript PDF_file /store-dir/ — /usr/X11R6/bin/Xvfb :9 -ac

Get Adobe Flash playerPlugin by wpburn.com wordpress themes