I had a question about altering a text file. I have data in the following format.

1 a,b,c
2 d,e
3 a, l, m,n

Note that some of them have spaces after the coma

Now I have to resort the data in the following format

1 a
1 b
1 c
2 d 
2 e
3 a
3 l
3 m 
3 n

Now my plan is to first parse the data, and hold a count of how many data elements there are for each number. (Ex. for the number 1, there are 3 elements). Then I would move down the data by 3 spaces, starting from the bottom. Kinda like moving an array. (The main concern here is space more than time complexity)

Then I would enter each data element at the top.

My first question is, how exactly would I parse the data in a txt file and indicate delimiters such as the coma and the possible spaces.

Second, having done this, how would I just move down a whole line inside a text file down one?

I am very sorry if my question is convoluted. I will gladly clarify anything. Thank you so much for your time! Please let me know if there is anything i need to add. Also, I'm comfortable in both java and c. So advice on either language is useful.


closed as off-topic by David Richerby, Tom van der Zanden, vonbrand, Evil, Rick Decker Dec 28 '15 at 20:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – Tom van der Zanden, vonbrand, Evil, Rick Decker
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ This is more a case for SO $\endgroup$ – vonbrand Dec 24 '15 at 23:33

I propose this, more direct to code (in java, c, etc.)

  • read each line (until CR/LF)

  • at begining, read a number, until spaces => number n

  • after that, take next characters of line

  • split them by ,

  • for each, trim (delete space before or after) => strings s1, s2, s3

  • then output n s1 CRLF n s2 CRLF n s3, etc.


Not the answer you're looking for? Browse other questions tagged or ask your own question.