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 have a textfile looking like Format-1. How can it be changed to Format-2, so that it can easily be opened as a "space delimited" file in excel to make charts?
Format-1:
2017-03-01 Joe 7
2017-03-01 Andy 0
2017-03-01 Lily 1
2017-03-01 Bob 6
2017-03-01 Vanessa 2
2017-03-02 Joe 3
2017-03-02 Andy 2
2017-03-02 Lily 9
2017-03-02 Bob 15
2017-03-02 Vanessa 14
2017-03-03 Joe 5
2017-03-03 Andy 25
2017-03-03 Lily 0
2017-03-03 Bob 8
2017-03-03 Vanessa 162
Format-2:
Date Joe Andy Lily Bob Vanessa
2017-03-01 7 0 1 6 2
2017-03-02 3 2 9 15 14
2017-03-03 5 25 0 8 162
No. I don't have any useful code to show here, 'cause I don't know how to approach this task.
If someone could tell me in plain text how it could be done, then I might produce some code. I know what I want, but I don't know how to get there...
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,669
Rep:
I cringe at myself as I type this (I actually am) but wouldn't this be just as quick to do in excel using the transpose paste thingy (whatever it's called)?
It would be cool to see a script to do it though.
No. I don't have any useful code to show here, 'cause I don't know how to approach this task.
If someone could tell me in plain text how it could be done, then I might produce some code. I know what I want, but I don't know how to get there...
There are several ways to approach this.
One way is with two arrays. One contains Joe Andy Lily Bob Vanessa as keys and values are the numeric positions for their data in the second array. The other is multidimensional and uses the date as the primary key and using the numeric value looked up from the first array to store the data point for that name. Then at the end print out the second array.
If Joe Andy Lily Bob Vanessa are fixed values they can be hard coded. If they are not fixed values then you probably need to make an extra pass at the beginning to harvest the names and generate a lookup table. I'd lean towards perl in the latter situation.
No need to get clever. Use 'grep' in a for loop to extract each individual's data to a separate file (sorted), then use those as input to a 'join' command keyed on the date field and specifying the desired output fields with the '-o' option.
edit: on reflection, you might be better off with the perl option as the join invocations start to get unwieldy as the number of names increases.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.