J. Free
J. Free

Reputation: 77

Print x number of column then insert new line

I have a file that contains of tab-delimited text that I am struggling to format them into a specific number of columns, and then insert a new line. For instance, let's say my file, as it stands, looks like this:

AAAA BBBB   CCCC DDDD   WWWW XXXX   YYYY ZZZZ

I'd like to print 2 columns, then insert a new line. Ideally, the output should looks like:

AAAA BBBB   CCCC DDDD   
WWWW XXXX   YYYY ZZZZ

I have tried the xargs way (as follows), to no avail:

$ xargs -d"\t" -n2 < file
AAAA BBBB CCCC DDDD
WWWW XXXX YYYY ZZZZ

I'd like to retain the tab-delimited format, what's wrong with the above is it's essentially gotten rid of the tabs which I'd like to keep.

Upvotes: 1

Views: 637

Answers (1)

tshiono
tshiono

Reputation: 22022

Write a shell script which looks like:

#!/bin/bash

sed 's/\([^TAB]\+TAB[^TAB]\+TAB\)/\1\
/g' < text

Type Tab keys where TAB is shown instead of literally typing.

If you are familiar with Perl, it will be easier to say:

perl -pe 's/([^\t]+\t[^\t]+)\t/$1\n/g;' < text

Upvotes: 1

Related Questions