100 Useful Command-Line Utilities

by Oliver; 2014

86. fold

The fold command allows you to restrict the number of characters per line written to std:out. For example:
$ echo -e "asdfasdfasdf\nasdfasdfasdf"
asdfasdfasdf
asdfasdfasdf
Using a 5 for the width flag produces:
$ echo -e "asdfasdfasdf\nasdfasdfasdf" | fold -w 5
asdfa
sdfas
df
asdfa
sdfas
df
Transpose a string (i.e., convert from row to column):
$ echo Joe | fold -w 1
J
o
e
Here's an example from Wiki - Bioinformatics. How would you find the sequence composition of the following fasta file?
$ cat myfasta.fa
>entry1
AACCCGGCTGCGTACGTACCACAGAGAGGGGTGTA
>entry2
TTATGCGATAAACCCGGGTGTAATTTTATTTTTTT
Here's the answer:
$ cat myfasta.fa | grep -v ">" | fold -w 1 | sort | uniq -c
     17 A
     13 C
     18 G
     22 T

<PREV   NEXT>