Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am not a programmer and don't have a lot of experience scripting (only some editing of existing scripts). I have two csv files that both contain the same "header columns" or first lines that need to be combined into one csv file keeping the one header column and deleting the other.
I also may need to change the order of some "columns" of csv data.
Any help is appreciated. I don't think this should be a lot of code but am not sure what to do.
A "cheater" way of doing this could also be to import both files to separate open office spreadsheets, adjust the columns as needed, copy the rows over to combine the files, and then export it as a CSV. I know, not a script and not very elegant, but effective none the less...
Yes. That has been the way I have done it in the past and it just adds time I don't have. I am trying to get automate these sorts of manual monkey tasks. I am going to see if I can edit the script that creates the originals to printout the data in the same order. That will allow me to use the suggestion above.
If anyone has a quick change columns suggestions in case I run into problems, those are appreciated as well.
I wrote a program similar to what you need for my unix class a year ago, using bash, awk, and sed. It was a clunker. I honestly think PERL is your best option. Use the first line to get the re-ordering, then use split and print to reorder the lines.
Perl certainly has a lot of power but for what you originally wrote my solution would work fine and is a 3 line scipt already done.
I see in latter posts some discussion of reordering the columns which wasn't part of your original. You can do this with the awk command since its a comma separated file by using comma as the delimiter.
For example:
Code:
awk -F, '{print $2","$1","$4","$3","$5}' filename
Would print your original records that have format:
ID,Password,Last Name,First Name,Category
with new format:
Password,ID,First Name,Last Name,Category.
The -F tells awk which character to use as delimiter. The $1 through $5 are the 5 fields. You have to put quotes around the commas in the print statement as you want a literal comma to appear in the output (without the quotes it would just put a space between them).
You of course can change the order anyway you want.
Here again simple shell script with 3 lines would suffice.
Perl certainly has a lot of power but for what you originally wrote my solution would work fine and is a 3 line scipt already done.
I see in latter posts some discussion of reordering the columns which wasn't part of your original. You can do this with the awk command since its a comma separated file by using comma as the delimiter.
...
This is actually quite an elegant solution, though his original post did have columns in different orders and did (briefly) mention reordering them.
I stand (sit actually) corrected. His post did mention possibly needing to reorder the columns. Not only that I'd completely missed the fact that he had a slightly different order in csv1 than in csv2.
He'd just have to change the awk numbering for the two files to get them the same. In my code I'd put the same numbering in both awk lines but that wouldn't work. (e.g. $2 in csv1 is $4 in csv2).
Last edited by MensaWater; 01-11-2006 at 11:08 AM.
Somewhat odd reports he seems to be dealing with, of course. Out of curiousity, what part of the ATL are you from? (Don't mean to get side-tracked, just noticed you're from Atlanta)
Not that bad - the only thing that is bad is it seems to imply he has passwords stored in non-encrypted files. Hackers would love to find those csv files.
As for me I'm in Cobb county. How about you? Also are you a member of ALE?
You know, I never noticed the PASSWORD field in there. We can hope they're crypted passwords.
I live in North DeKalb. I've never managed to make it to ALE, though I would really like to. Unfortunately, being a college student, I have a class until 7:10 on tuesdays and thursdays.
Actually, the passwords are somewhat irrelevant in that they are generated for both sets of accounts but are overridden by the server referencing LDAP. One of the files generated does have relevant passwords but these don't have much value to anyone as they are mainly a way of getting a user viewpoint for an administrator who by default can test from this vantage point.
Thanks for the heads up though. I am going to see how far I can get with all the suggestions. From what I do know I think they should do the trick.
I am not a member of ALE (need to wikipedia that one). The code level work I do is somewhat a sideline. I am working to grow more and more into managerial roles. This sort of thing gives me a better understanding of the dark cubicle world.
I have to say this first experience using this group has been far better than other forums I have tried.
ALE is a "Atlanta Linux Enthusiasts" a metro Atlanta Linux Users Group (LUG). My question was directed at Matir as we both happen to be in the the Atlanta area. There likely is a LUG in your area and its sometimes nice to go and hear what others are doing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.