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

Alex Janssen alex at ourwoods.org
Sun Feb 21 19:26:01 UTC 2010


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
NEWLEN=$1
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

-- 
Ourwoods.org
 Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. - Albert Einstein (275)





More information about the ubuntu-users mailing list