5

I need to turn a text file that has a single line per file name and output separated by a single space into specific blocks that have lines that are equal to 60 characters in length.

Like this:

>Directory1/file3 CTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCCCCSCBCCCCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCCCCSCBCCCCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCCCCSCBCCCCCCCCS
>Directory1/file4 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
...
...

turn into

>Directory1/file3
CTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCCCCSCBCC
CCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCC
CCSCBCCCCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTT
TCCCCCCCCSCBCCCCCCCCS
>Directory1/file4
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAA
...
...

How do I go about this?

7

Try:

$ awk '{print $1; for (i=1;i<=length($2);i=i+60) print substr($2,i,60)}' file
>Directory1/file3
CTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCCCCSCBCC
CCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCC
CCSCBCCCCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTT
TCCCCCCCCSCBCCCCCCCCS
>Directory1/file4
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAA

How it works:

  1. print $1

    This prints the first field on the line.

  2. for (i=1;i<=length($2);i=i+60) print substr($2,i,60)

    For the second field on the line, we print 60 characters at a time until we reach the end of the field.

  • 1
    Thanks, that worked perfectly! I appreciate the help. – RoHernEtrog May 13 at 20:37
3

You can use GNU coreutils fold:

fold -w60 file
  • 1
    you'd have first to put a new line iinstead of the first space, and then only pipe to your fold. tr ' ' '\n' <file | fold -w60 (assuming directory/filename is lower than 60 chars in length...) – Olivier Dulac May 14 at 17:19
  • Right! Read the question a bit too quickly. – Gohu May 15 at 7:22
1

this looks okay too

sed -E "s/^(>[^\s]+) /\1\n/; s/([[:alnum:]]{60})/\1\n/g" /path/file

result

$ sed -E "s/^>([^\s]+) /\1\n/; s/([[:alnum:]]{60})/\1\n/g" /path/file
>Directory1/file3
CTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCCCCSCBCC
CCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTTTCCCCCC
CCSCBCCCCCCCCSCTTSCCCTTTTTSEEEEECGGGSCEEEEECCCSSBCCCSCCCCCTT
TCCCCCCCCSCBCCCCCCCCS
>Directory1/file4
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAA
...
...
  • Regexes are neat but also not at all optimum here... Or maybe replace the [[:alnum:]] (very slow if locale is unicode) with a simple . to make it less CPU intensive? – Olivier Dulac May 14 at 17:27
0

this works:

fold -s -w 60

It produces the same output as in the question.

Assuming there are no spaces in the second part, this is the simplest way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.