用bash中的新行换行太长的字符串输出

时间:2015-04-21 21:10:57

标签: bash

想象一下,你的脚本会输出一些真正奇怪的输出,如:

fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjerapogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJRIAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREANFDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGRNEPAGRJAEIO
我知道废话。可能这样看起来会更好:

fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjera
pogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJR
IAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREAN
FDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGR
NEPAGRJAEIO

我试图用bash中的base64编码字符串数据来做这件事。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

对于您的示例输入,fold将生成您请求的输出。假设我们有这个文件:

$ cat longline
fdjsagnfafeowkagfdsngodagfoeagnfiosnthoaogjerapogrjeagnfdosngtnsrhig ofskhnIAGNRIEAGPREAOGJRIAEGRNIEOASGNFAIGRNESUIGRIOAEGRNIAENGRUEAOREANFDOIGNRUESIGORNAEIGNREIOAGRIUESGRJOEAGRNEOSIGRNEPAGRJAEIO

制作宽度为46个字符的行:

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

如果要在管道中使用它,fold也接受stdin:

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

在Linux上,foldGNU coreutils的一部分,默认情况下应该安装。它也可以在Mac OSX下找到。