Reputation: 53
I have this in one line:
We were born in the earth beyond the land
I want it in 3 words lines, to be like this:
We were born
in the earth
beyond the land
Upvotes: 5
Views: 7832
Reputation: 11456
Any system that has awk and sed will almost certainly also have Perl:
cat myfile.txt | perl -lane 'while(($a,$b,$c) = splice(@F,0,3)){print "$a $b $c"}'
Upvotes: 0
Reputation: 4935
Here's one sed solution:
sed -e 's/\s/\n/3;s/\s/\n/6;s/\s/\n/9'
It replaces the third, sixth and ninth spaces with newlines.
This one will handle longer lines, even if they aren't multiples of three words:
sed -e 's/\(\(\S\{1,\}\s*\)\{3\}\)/\1\n/g'
Upvotes: 0
Reputation: 98088
With GNU sed:
sed 's/\(\w\w*\W*\w\w*\W*\w\w*\W*\)/\1\n/g' input
and short version:
sed 's/\(\(\w\w*\W*\)\{3\}\)/\1\n/g' input
Upvotes: 0
Reputation: 36282
Using awk
:
awk '{
for ( i = 1; i <= NF; i++ ) {
printf "%s%c", $i, (i % 3 == 0) ? ORS : OFS
}
}' infile
It yields:
We were born
in the earth
beyond the land
Upvotes: 0
Reputation: 85865
$ xargs -n3 < file
We were born
in the earth
beyond the land
$ egrep -o '\S+\s+\S+\s+\S+' file
We were born
in the earth
beyond the land
Upvotes: 11