string - How can I split a line in 2 by words/characters? -


i convert:

hello hello hi hello hello hi hi hi hi hello 

into:

hello hello hi hello hello hi hi hi hi hello 

i want split single line in 2, having them have closest amount of characters possible, without cutting off word.

i think can wc -w, wc -c, cut, and/or awk, maybe guys can provide better ideas!

this can job fold, handy util wrap lines:

$ s="hello hello hi hello hello hi hi hi hi hello" $ fold -w $(( ${#s} / 2 + 1)) -s <<< "$s" hello hello hi hello hello hi hi hi hi hello 

this uses fold wrap each line given length. , how calculate length? using ${#variable} , dividing 2 (+1 prevent getting 3 lines if length odd). use -s prevent breaking words.

from man fold:

fold - wrap each input line fit in specified width     -s, --spaces           break @ spaces     -w, --width=width           use width columns instead of 80 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -