AWK experts - how would I code around this in awk...

Doug Robinson dkrr at
Sun Feb 21 19:48:47 UTC 2010

Alex Janssen wrote:
> Tony Arnold wrote:
>> You could replace the while loop with just a fold command, e.g.,
>> tr -d "\n\r" | fold -b -w $NEWLEN
>> I've not tested this, so I'd check man pages etc! I'm not sure if the -b
>> is required.
>> Regards,
>> Tony.
> fold?  Didn't know about that one.  Thanks, Tony.
> That works a lot better than "while read -n LINE".
> With the latter, If the last read is less than n characters, the 
> while-loop quits before it executes the echo command and your output is 
> short by the last read.
> The following script works.
> ********script
> #remove all CR's and LF's and reinsert LF's every n characters
> if [ $# -lt 1 ]
> then
>   echo -e "\n$0\n  Removes all CR's and LF's and reinsert LF's every n 
> characters\n  usage:\n    $0 n\nn=new line length\n  $0 reads from stdin 
> and writes stdout"
>   exit 0
> fi
> tr -d "\n\r"| fold -w $NEWLEN
> exit 0
> ********end script
> You really don't need to put it in a script file, except that it keeps 
> you from having to remember how to put the 2 commands together.
> Just do:
>   tr -d "\n\r" <oldfile | fold -w 20 >newfile
> I have bad memory, so I'll put things like this in script files.
> Maybe it is easier with awk, but now it works with bash.  I'll have to 
> test with Doug's awk script.
> Alex
fold FOLD that is the command I have been trying to
remember. Sysadmin time was 30 years ago. I like this solution best!

tr -d "\n\r" <oldfile | fold -w 20 >newfile

My guess is that it will prove fastest esp. with a multi core cpu;
common these days.


More information about the ubuntu-users mailing list