We help IT Professionals succeed at work.

adding a mysql column to a tab delimited file

andieje
andieje asked
on
439 Views
Last Modified: 2012-05-11
Hi

I have a mysql database with a table called Feature and an autoinc primary key.
I also have a tab delimited file with one row per feature. This file does not contain the Feature autoinc primary key. The rows in the tab delimited file are in the same order as the rows were inserted into the mysql database.

Basically i want to add the feature_id pk as a column to this tab delimited file. I could write a script but I was wondering if there was an easier way to export a mysql table column(s) to an existing tab delimted file

I've tried searching for this but all the results that come back just tell me how to export data as a new tab file but i want to add to an existing file

thanks
Comment
Watch Question

Aaron TomoskyDirector, SD-WAN Solutions
CERTIFIED EXPERT

Commented:
If you open it in excel it's pretty easy to add a autoincrement Pk column
Top Expert 2012

Commented:
Is your ultimate objective to import the file into the table?  Why not just use the load data command, map the columns, and let the auto_increment in mysql assign the values?

See http://dev.mysql.com/doc/refman/5.1/en/load-data.html
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Is the data in the mysql database complete, accurate and have the identical information that is in your tab delimited file??
If the answer is Yes, given that you want to match the id column from the mysql to the name in the Tab delimited file, exporting the data from mysql is the way to go.
Alternatively, you can insert a column in your Tab delimited file while it is open in excel, or openoffice equivalent or use a while/for loop
to add an autoincrementing number to each row.
A2 =($A1+1)
Fill down
Note that Saving data in a tab delimited/comma delimited value format will only save the values and strip out the function.

Author

Commented:
I should have pointed out that i want the process to be automated by a script or a mysql command i can call from a shell script.

Yes, arnold, the data in the database is identical to the data in the tab file except that the tab file doesn't have the autoinc primary key

Author

Commented:
My ultimate objective is to get the autoinc primary key into the tab file using a script and not a spreadsheet
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
that just exports the fields from the table into a new file and not into my existing tab file.

However i could then merge the 2 tables with the paste command
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
If the tab file is the same data that you imported what is the issue with exporting the data back that will include the auto-increament?

What is the end goal for you?

Author

Commented:
the tab file i have now contains a lot more data than the data that was originally imported. i just wanted to add the id from the database to it (i wasn't going to import the new extended data set)

but exporting the id and pasting the 2 files works fine
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.