LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2011, 03:09 AM   #1
Latios
Member
 
Registered: Dec 2010
Distribution: Arch
Posts: 115

Rep: Reputation: 21
Importing MYSQL table from text file wo column delimiters


I am trying to import a data file from old DOS application into MYSQL table

The file is clear text file with fixed-width columns, without column delimiters

Example file :
Code:
4444333666666
2222666555555
iiiiwwwcccccc
ttttxxxaaaaaa
Code:
mysql> create table t (col1 char(4) , col2 char(3) , col3 char (6) ) ;
Query OK, 0 rows affected (0.01 sec)

mysql> load data infile '/home/ash/1.txt' into table t fields terminated by '' enclosed by '' ;
Query OK, 4 rows affected, 8 warnings (0.00 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 8

mysql> select * from t ;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 4444 | 6    |      |
| 2222 | 5    |      |
| iiii | c    |      |
| tttt | a    |      |
+------+------+------+
4 rows in set (0.00 sec)
How to import it properly ?

I tried to change char to varchar and it did not change anything
 
Old 06-13-2011, 03:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I'm not sure if you can tackle this using mysql, but you might consider changing the input file and make it comma separated:
Code:
$ cat infile
4444333666666
2222666555555
iiiiwwwcccccc
ttttxxxaaaaaa

$ sed -r 's/([[:alnum:]]{4})([[:alnum:]]{3})(.*)/\1,\2,\3/' infile
4444,333,666666
2222,666,555555
iiii,www,cccccc
tttt,xxx,aaaaaa
Hope this helps.
 
Old 06-13-2011, 03:52 AM   #3
Latios
Member
 
Registered: Dec 2010
Distribution: Arch
Posts: 115

Original Poster
Rep: Reputation: 21
Thanks !
 
Old 06-13-2011, 04:03 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert a column of text to a text file docaia Linux - Newbie 5 06-02-2011 10:55 AM
Problem importing text files into a mysql database kaplan71 Linux - Software 8 06-02-2010 02:35 PM
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
Swap column of text file kushalkoolwal Programming 4 09-05-2008 11:30 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration