LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Change format (https://www.linuxquestions.org/questions/linux-newbie-8/change-format-4175601385/)

Bartonsen 03-09-2017 09:07 AM

Change format
 
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

Turbocapitalist 03-09-2017 09:11 AM

It would be fairly easy with awk or perl. Which are you trying and how far have you gotten?

Bartonsen 03-09-2017 09:22 AM

I've tried with awk and 'for date in ...', but I've not been successful in getting any useful output....

Turbocapitalist 03-09-2017 09:50 AM

"On the Internet nobody can hear you being subtle."
Can you show some or all of your code and explain which approach you are trying? That will give people here something to comment on.

Bartonsen 03-09-2017 11:38 AM

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...

273 03-09-2017 11:42 AM

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.

Turbocapitalist 03-09-2017 11:59 AM

Quote:

Originally Posted by Bartonsen (Post 5681198)
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.

GazL 03-09-2017 01:12 PM

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.


All times are GMT -5. The time now is 05:30 AM.