LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   To dump specific columns of a given file in mysql database. (https://www.linuxquestions.org/questions/linux-newbie-8/to-dump-specific-columns-of-a-given-file-in-mysql-database-4175494534/)

abhishekgit 02-11-2014 07:51 AM

To dump specific columns of a given file in mysql database.
 
Hello everyone,
I've been in a problem to dump columns of my choice into a mysql database from a given text file that looks like this.
Quote:

1234 1111 1222 1223
1111 3333 ddsd 1232
2234 4444 1121 1212
3882 0099 1111 1232
1111 8278 3344 1223
Considering the above file is delimited by tabs, how do i extract only columns 2,3,and 4 in the database and ignore the rest. Any help would be appreciated. Thank you.

Habitual 02-11-2014 08:42 AM

It may help us to know the desc of the table, so
mysql >
Code:

desc dbname.table;
output please.

abhishekgit 02-11-2014 08:54 AM

I've created a table called Station, which has three attributes namely stationID, X_coord, Y_coord. And the input file, there are around 16 attributes delimited by tabs, in which columns 3,4,and 5 match the attributes of the station table. The problem is to dump only those columns to the database and ignore the others. Thanks for your time

TB0ne 02-11-2014 09:58 AM

Quote:

Originally Posted by abhishekgit (Post 5115559)
I've created a table called Station, which has three attributes namely stationID, X_coord, Y_coord. And the input file, there are around 16 attributes delimited by tabs, in which columns 3,4,and 5 match the attributes of the station table. The problem is to dump only those columns to the database and ignore the others. Thanks for your time

Not really any problem to solve, since doing a basic MySQL select statement would do it. Have you tried looking at the MySQL documentation on the select statement? Done ANY research on this???

Basic MySQL select, easily found if you tried to look:
https://dev.mysql.com/doc/refman/5.7/en/select.html

Since you apparently want to be fed the answer:
Code:

SELECT field1,field2,field3 FROM table INTO OUTFILE '/some/file.name' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
...which answers your question: " how do i extract only columns 2,3,and 4 in the database "

However, if you want to LOAD data from a text file into a database, then strip off the first column of data, which will leave you a text file with just the three columns you want. You can use awk for that. Then use the MySQL "load data" command (again, a SMALL bit of research would tell you this):
https://dev.mysql.com/doc/refman/5.7...ng-tables.html


All times are GMT -5. The time now is 12:15 AM.