How do you split a sentence after every nth word

Hi,

I think my problem is a "simple" one to resolve. What i am looking for is a way in sed/awk to split a long line/paragraph into say 5 words per line.

For example:

Sentence/paragraph contains: 102 103 104 105 106 107 109 110 ....

I would like the output to be (if splitting every 5 words):
102 103 104 105(newline)
106 107 109 110 (newline)
...etc....

Is this possible?

One way:

awk '{for(i=5;i<NF;i+=5){$i=$i RS};gsub(RS FS,RS,$0)}1' file
C:\cygwin\tmp>echo a b1 c22 d33 e4 f5 | sed 's/\.* /~/4' | tr "~" "\n"
a b1 c22 d33
e4 f5

I put a ~ after the 4th instance, then convert that to a new-line.

thanks all for your help

Anothe one , using xargs

xargs -n5 < file

hey joeyg, thanks. However trying your approach doesnt work on the every line, it only works on the first words:

i.e echo "as bg bh bh nj mk mu bg nh dr g y jj ko ll oo ss ff gg yy hh hh" | sed 's/\.* /~/2' | tr "~" "\n"

gave me:
as bg
bh bh nj mk mu bg nh dr g y jj ko ll oo ss ff gg yy hh hh
:frowning:

---------- Post updated at 08:19 AM ---------- Previous update was at 08:16 AM ----------

---------- Post updated at 08:20 AM ---------- Previous update was at 08:19 AM ----------

i have re-directed a load of IDs to a temp file which is space separated. The file has more than 2000 rows.

I just need a way of printing each ID out on a new line in groups of 'X' (where X can be 5, 10, 15, etc)...

I am running my script on Solaris 8 and 10 systems.