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

Tony Arnold tony.arnold at manchester.ac.uk
Sun Feb 21 18:29:40 UTC 2010


Alex,

Alex Janssen wrote:
> Steve Flynn wrote:
>> That's not a bad idea at all come to think of it.
>>
>> Remove ALL CR/LF's to make one honking big line and then spit it back
>> out in chunks of 'n'.
>>
>> Definitely food for thought. I can feel some playing around on my
>> laptop happening on the train home this afternoon... cheers! :)
>>   
> I had to play around with it to test it and decided it should just be a 
> filter to read stdin and write stdout.  Here's a working script:
> **********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
> NEWLEN=$1
> tr -d "\n\r"|
> while read -n$NEWLEN LINE
> do
>  echo "$LINE"
> done
> exit 0
> **********end script
> If you need CR's change the echo line to read "echo -e "$LINE\r".

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.
-- 
Tony Arnold,                        Tel: +44 (0) 161 275 6093
Head of IT Security,                Fax: +44 (0) 870 136 1004
University of Manchester,           Mob: +44 (0) 773 330 0039
Manchester M13 9PL.                 Email: tony.arnold at manchester.ac.uk




More information about the ubuntu-users mailing list