Tuesday, May 11, 2010

Unix Fold

Fold is not just a formatting tool for word wrapping but can come in handy for text editing as well.

check this code
cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'

When we pass the content of apple to fold command, it breaks the content to 1 character per line as defined by the width parameter of fold.

$cat apple
apple

$cat apple | fold -1 (use fold -w1 for ksh)
a
p
p
l
e

And when we add sort to the above command and look lines beginning for vowels in
$cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'
a
e

Isn't that neat.

Base code from here.

No comments:

Post a Comment