adding a mysql column to a tab delimited file


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

Who is Participating?
Then use the
echo "select * from tableName" |mysql -u <username> --password="password"  | sed -e 's/\s+/\t/g' > fileofinterest

in a cron job.

fileofinterest will have all the data you want with all the autoinc information in tab separated format.
Aaron TomoskySD-WAN SimplifiedCommented:
If you open it in excel it's pretty easy to add a autoincrement Pk column
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?

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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.
andiejeAuthor 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
andiejeAuthor Commented:
My ultimate objective is to get the autoinc primary key into the tab file using a script and not a spreadsheet
andiejeAuthor 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
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?
andiejeAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.