I have a problem. Right now, a file that was supposed to be tab-delimited is missing a few “newlines”… My file looks something like this right now
Field1 Field2 Field3
Field1 Field2 Field3 Field1 Field2 Field3 Field1 Field2 Field3
Field1 Field2 Field3 Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field3 Field1 Field2 Field3
Field1 Field2 Field3
I want to make it look uniform, with each “field1” starting at a new line
Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field3
Field1 Field2 Field3
The problem is, each of these columns has a unique set of data, so I can’t find a familiar place to split it into a new line. Any help is greatly appreciated!
PS: doing this in sed or tr would be greatly appreciated
PS: there can be up to 150 columns, not just 6 or 9 or any other multiple of 3
This might work for you:
Explanation:
The third white space character (space or tab) is replaced by a newline
s/\s/\n/3The string upto the first newline is printed
PThe string upto the first newline is deleted
DThe
Dcommand has a split personality. If there is no newline it deletes the string and the next line is read in. If, however, a newline exists, it deletes the string upto the newline and then the cycle is started on the same string until no newlines exist.