1

Imagine your script outputs some real strange output like:

fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjerapogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJRIAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREANFDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGRNEPAGRJAEIO

Crap I know. Probably it would look better this way:

fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjera
pogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJR
IAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREAN
FDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGR
NEPAGRJAEIO

I am trying to do this with base64 encoded string data in bash. How could I do it?

4
  • Read this: stackoverflow.com/a/20065991/797495 Commented Apr 21, 2015 at 21:13
  • You should be able to store the output as a variable and then try something like: echo $varname | base64
    – ryekayo
    Commented Apr 21, 2015 at 21:15
  • I am not using java nor wrapping the output for parsing. I am wraping it because it would get lost if I didn't. The data is being sent over networks and it's all nastily complicated. Commented Apr 21, 2015 at 21:15
  • @ryekayo I know how to make base64 data. I just need to print it wrapped as in my question. Commented Apr 21, 2015 at 21:15

1 Answer 1

3

For your sample input, fold will produce the output that you request. Suppose that we have this file:

$ cat longline
fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjerapogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJRIAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREANFDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGRNEPAGRJAEIO

To make lines with width 46-characters:

$ fold -w 46 longline
fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjera
pogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJR
IAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREAN
FDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGR
NEPAGRJAEIO

If you want to use it in a pipeline, fold also accepts stdin:

$ fold -w 46 <longline
fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjera
pogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJR
IAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREAN
FDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGR
NEPAGRJAEIO

On Linux, fold is part of GNU coreutils and should be installed by default. It is also available under Mac OSX.

1
  • 1
    Well, would you have guessed, there's no way to not count escape codes... :-/ Commented Oct 15, 2019 at 8:34

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