Here is an example of my txt file


Here is the example of the target txt file

John, NY,2019,APR
Mark, SC,2019,JAN
Ava,  CA,2018,AUG

The number of fields wont change. Basically how am I able to write a script that can identify that every 4th line is the month and the line after that is the name?

  • Are the spaces before the location field values significant? if not, a simple de-serialization using paste should be sufficient – steeldriver Apr 15 at 21:50
  • This is very simple to do with awk, but please show what you've tried, and how it did not work as expected or intended. – DopeGhoti Apr 15 at 22:01

I'm having trouble telling which of two potential questions you're asking - so I'll give both a broad shot, and if I get round to it, I'll look into the actual terminal / shell commands you might use tomorrow morning (and expand my answer then!).

So your question can either be understood as

  • a): Sort a file from a "line by line" arrangement into a "table-like" (in your example its a comma-separated-value file [also known as csv]) arrangement. That's effectively rather straightforward, and would require a loop structure (ie repeating the same set of instructions for each entry), and within that you would take the value's for the 4 lines that make up an entry (eg "John", "NY", "2019", "APR" ) and append them - in turn - to a new line, inserting commata (",") between the values.
  • b): Sort the values of a file according to the type of variable and put everything back together in a nice sorted "table-like" arrangement (eg as a csv file, with the output looking much the same as described above). This is a good bit more tricky / complicated, but as long as your range of values/variables is restricted - as in your example (12 three letter abbreviations for months, 50 two letter abbreviations for US-States, etc.), then you can compare each of your values to the set/range of variables that is possible, and determine (relatively reliably) which kind of variable it is in that manner. And then of course, you put them together like beads on a string for your new file, just with a little more magic/ glue than as described above to get them in the right spot (that is, assuming as I have been here, that the values belonging to one entry are not always in the same order).

That's it for now - it would be nice to know which of the two above questions you meant before I go for an example terminal/shell input :D !

Regards, Sean Scherer


There doesn't seem to be any sorting involved here - just de-serialization into a fixed number (4) of columns. You can use

paste -d, - - - - < file


pr -aT -s, -4 < file


awk '{ORS = NR%4 ? "," : "\n"} 1' file


$ awk 'BEGIN{print "Name,Location,Year,Month"} {ORS = NR%4 ? "," : "\n"} 1' file
  • is there a way to make it that it doesnt re-print the header every time? So every time i run it , the data is only added on. Right now it re-prints the header every time you run it. – James Apr 17 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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