Skip to main content

how to insert new line if a line has more than 60 characters [Resolved]

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?


Question Credit: RoHernEtrog
Question Reference
Asked May 13, 2019
Tags: linux, text
Posted Under: Unix Linux
28 views
1 Answers

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.


credit: John1024
Answered May 13, 2019
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA