Domanda

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

È stato utile?

Soluzione

This might work for you:

sed 's/\s/\n/3;P;D' file

Explanation:

  • The third white space character (space or tab) is replaced by a newline s/\s/\n/3

  • The string upto the first newline is printed P

  • The string upto the first newline is deleted D

    The D command 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.

Altri suggerimenti

This will work on the example you gave...

sed -e 's/\([^\t ]* [^\t ]* [^\t ]*\)[\t ]/\1\n/g'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top