Repeat a string a specified number of times... 
Author Message
 Repeat a string a specified number of times...

Does anyone have a script to repeat a string a number of times. For
example I have a column that I want to pad with leading zeros. Now the
string must be 10 characters long after padding with zeros and the
length of data in the column varies. The part below gets the number of
zeros to pad to each line but I am struggling to get any further.

Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'



Wed, 27 Apr 2005 23:11:09 GMT  
 Repeat a string a specified number of times...

Quote:

>Does anyone have a script to repeat a string a number of times. For
>example I have a column that I want to pad with leading zeros. Now the
>string must be 10 characters long after padding with zeros and the
>length of data in the column varies. The part below gets the number of
>zeros to pad to each line but I am struggling to get any further.

>Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

use the printf function.  It bahaves just as it does in C

man awk
man printf

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 27 Apr 2005 23:47:59 GMT  
 Repeat a string a specified number of times...

Quote:

>Does anyone have a script to repeat a string a number of times. For
>example I have a column that I want to pad with leading zeros. Now the
>string must be 10 characters long after padding with zeros and the
>length of data in the column varies. The part below gets the number of
>zeros to pad to each line but I am struggling to get any further.

>Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

In TAWK, use (builtin) strdup().  Usage: x = strdup("*",10)

In Gawk, use (usage same as the TAWK builtin):

function strdup(s,n, t) {t = sprintf("%"n"s","");gsub(/ /,s,t);return t}



Wed, 27 Apr 2005 23:50:13 GMT  
 Repeat a string a specified number of times...

Quote:



>>Does anyone have a script to repeat a string a number of times. For
>>example I have a column that I want to pad with leading zeros. Now the
>>string must be 10 characters long after padding with zeros and the
>>length of data in the column varies. The part below gets the number of
>>zeros to pad to each line but I am struggling to get any further.

>>Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

>use the printf function.  It bahaves just as it does in C

>man awk
>man printf

Note, BTW, in my previous post on this thread, that I answered the question
posed, not the problem to be solved.


Wed, 27 Apr 2005 23:51:46 GMT  
 Repeat a string a specified number of times...

Quote:
> Does anyone have a script to repeat a string a number of times. For
> example I have a column that I want to pad with leading zeros. Now the
> string must be 10 characters long after padding with zeros and the
> length of data in the column varies. The part below gets the number of
> zeros to pad to each line but I am struggling to get any further.

> Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

kenster10 -

Look at the printf statement and function (identical, except you either do
or do not use parenthesis around the argument list).  Zero-padding is part
of the formatting provided there.  By the way, newlines don't come
naturally, you have to put them in yourself.

    {printf "%010d\n", $1}

The sprintf() function returns a formatted string, instead of printing it:

    {s = sprintf("%010d",$1); print s}

If you want to accomplish these same tasks without printf, or with other
characters, you can use string operations:

    {print substr("~~~~~~~~~~" $1, length($1) + 1, 10)}

The last agument to substr is not needed, and will actually cause truncation
problems if you DO get a number greater than 9,999,999,999.

    {print substr("~~~~~~~~~~" $1, length($1) + 1)}

Note that the only place 10 occurs here is in the number of pad characters.

You may want to make this more readable, complicated, and slow, by
implementing "right()":

    function right(s, n, local, x) {
        if ((x = length(s)) < n)
            return s
        else
            return substr(s,x-n+1)
    }
    {print right("0000000000" $1,10)}

But this is handy for languages that have right() built-in like Visual Basic
Scripting Edition, and you don't have to rely on having exactly 10 zeros
typed in, as long as there are more than 10.

Finally, to answer your question about "a script to repeat a string a number
of times," you do need to roll your own string creation...the string(),
repeat(), and spaces() don't exist in AWK. But you can try:

    function repeated(s, n, local, r) {while (n-- > 0) r = r s; return r}
    function spaces(n) {return repeated(" ",n)}
    BEGIN {print spaces(5) repeated("hi",5)}

- Dan



Wed, 27 Apr 2005 23:55:00 GMT  
 Repeat a string a specified number of times...

Quote:



>> Does anyone have a script to repeat a string a number of times. For
>> example I have a column that I want to pad with leading zeros. Now the
>> string must be 10 characters long after padding with zeros and the
>> length of data in the column varies. The part below gets the number of
>> zeros to pad to each line but I am struggling to get any further.

>> Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

>kenster10 -

>Look at the printf statement and function (identical, except you either do
>or do not use parenthesis around the argument list).

Wrong.  printf is never a function.

% gawk 'BEGIN {x=printf("abc\n")}'
gawk: cmd. line:1: BEGIN {x=printf("abc\n")}
gawk: cmd. line:1:          ^ parse error
%



Thu, 28 Apr 2005 00:02:22 GMT  
 Repeat a string a specified number of times...

Quote:


> > Does anyone have a script to repeat a string a number of times. For
> > example I have a column that I want to pad with leading zeros. Now the
> > string must be 10 characters long after padding with zeros and the
> > length of data in the column varies. The part below gets the number of
> > zeros to pad to each line but I am struggling to get any further.

> > Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

> kenster10 -

> Look at the printf statement and function (identical, except you either do
> or do not use parenthesis around the argument list).  Zero-padding is part
> of the formatting provided there.  By the way, newlines don't come
> naturally, you have to put them in yourself.

>     {printf "%010d\n", $1}

> The sprintf() function returns a formatted string, instead of printing it:

>     {s = sprintf("%010d",$1); print s}

> If you want to accomplish these same tasks without printf, or with other
> characters, you can use string operations:

>     {print substr("~~~~~~~~~~" $1, length($1) + 1, 10)}

> The last agument to substr is not needed, and will actually cause
truncation
> problems if you DO get a number greater than 9,999,999,999.

>     {print substr("~~~~~~~~~~" $1, length($1) + 1)}

> Note that the only place 10 occurs here is in the number of pad
characters.

> You may want to make this more readable, complicated, and slow, by
> implementing "right()":

>     function right(s, n, local, x) {
>         if ((x = length(s)) < n)
>             return s
>         else
>             return substr(s,x-n+1)
>     }
>     {print right("0000000000" $1,10)}

> But this is handy for languages that have right() built-in like Visual
Basic
> Scripting Edition, and you don't have to rely on having exactly 10 zeros
> typed in, as long as there are more than 10.

> Finally, to answer your question about "a script to repeat a string a
number
> of times," you do need to roll your own string creation...the string(),
> repeat(), and spaces() don't exist in AWK. But you can try:

>     function repeated(s, n, local, r) {while (n-- > 0) r = r s; return r}
>     function spaces(n) {return repeated(" ",n)}
>     BEGIN {print spaces(5) repeated("hi",5)}

> - Dan

kenster10 -

I just saw Kenny's post...and if you want to repeat a string, his method
using sprintf() and gsub() is also a nice way to implement repeated(), and
you get to learn sprintf() and gsub() in the process.

    - Dan



Thu, 28 Apr 2005 00:03:48 GMT  
 Repeat a string a specified number of times...

Quote:


> >Does anyone have a script to repeat a string a number of times. For
> >example I have a column that I want to pad with leading zeros. Now the
> >string must be 10 characters long after padding with zeros and the
> >length of data in the column varies. The part below gets the number of
> >zeros to pad to each line but I am struggling to get any further.

> >Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

> In TAWK, use (builtin) strdup().  Usage: x = strdup("*",10)

> In Gawk, use (usage same as the TAWK builtin):

> function strdup(s,n, t) {t = sprintf("%"n"s","");gsub(/ /,s,t);return t}

How odd that Thompson should use the name strdup(), when it is so very
specific in its abbreviation, and has such a veneralable history in C
circles (and Yoix, among others) as something completely different.  Not
that it isn't appropos.  That coopting of the term is almost as good as the
kind of stuff Microsoft does.

It is also odd that a while loop with string concatenation (see my other
post) in user space is 20% slower than invoking both the formatting code and
the regular expression engine.  I imagine TAWK's built-in is even faster.

    - Dan



Thu, 28 Apr 2005 00:32:41 GMT  
 Repeat a string a specified number of times...

Quote:




> >> Does anyone have a script to repeat a string a number of times. For
> >> example I have a column that I want to pad with leading zeros. Now the
> >> string must be 10 characters long after padding with zeros and the
> >> length of data in the column varies. The part below gets the number of
> >> zeros to pad to each line but I am struggling to get any further.

> >> Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

> >kenster10 -

> >Look at the printf statement and function (identical, except you either
do
> >or do not use parenthesis around the argument list).

> Wrong.  printf is never a function.

> % gawk 'BEGIN {x=printf("abc\n")}'
> gawk: cmd. line:1: BEGIN {x=printf("abc\n")}
> gawk: cmd. line:1:          ^ parse error
> %

OK, OK..."a statment with an alternative function-like syntax."  How would
YOU describe this syntactic anomoly?

    - Dan



Thu, 28 Apr 2005 00:37:13 GMT  
 Repeat a string a specified number of times...
Need create new function, e,g 'repeat':

...
print repeat("0",10-length($1)) $1;
/* $1="1234", result: "0000001234" */
...

function repeat(x,n  ,o,i){o="";for(i=1;i<=n;i++){o=o x;};return(o);}


Quote:
> Does anyone have a script to repeat a string a number of times. For
> example I have a column that I want to pad with leading zeros. Now the
> string must be 10 characters long after padding with zeros and the
> length of data in the column varies. The part below gets the number of
> zeros to pad to each line but I am struggling to get any further.

> Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'



Thu, 28 Apr 2005 05:13:13 GMT  
 Repeat a string a specified number of times...

Quote:







>> >> Does anyone have a script to repeat a string a number of times. For
>> >> example I have a column that I want to pad with leading zeros. Now the
>> >> string must be 10 characters long after padding with zeros and the
>> >> length of data in the column varies. The part below gets the number of
>> >> zeros to pad to each line but I am struggling to get any further.

>> >> Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

>> >kenster10 -

>> >Look at the printf statement and function (identical, except you either
>do
>> >or do not use parenthesis around the argument list).

>> Wrong.  printf is never a function.

>> % gawk 'BEGIN {x=printf("abc\n")}'
>> gawk: cmd. line:1: BEGIN {x=printf("abc\n")}
>> gawk: cmd. line:1:          ^ parse error
>> %

>OK, OK..."a statment with an alternative function-like syntax."  How would
>YOU describe this syntactic anomoly?

>    - Dan

I think of it as a procedure (In Pascal-ish terms).

Something interesting in TAWK;

% tawk 'BEGIN {x=printf("abc\n")}'
tawk: error in PROGRAM line 1: illegal expression (bad assignment statement)
tawk: aborting due to compilation errors
% tawk 'BEGIN {x=call("printf","abc\n");print "x="x}'
abc
x=
%



Thu, 28 Apr 2005 05:24:00 GMT  
 Repeat a string a specified number of times...

Quote:

> Does anyone have a script to repeat a string a number of times. For
> example I have a column that I want to pad with leading zeros. Now the
> string must be 10 characters long after padding with zeros and the
> length of data in the column varies. The part below gets the number of
> zeros to pad to each line but I am struggling to get any further.

> Here we go awk 'BEGIN {FS="|"}; {print 10-length($1')}'

         awk 'BEGIN {FS = "|"} {printf "%010s", $1}'

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Thu, 28 Apr 2005 05:46:33 GMT  
 Repeat a string a specified number of times...

...

Quote:
>How odd that Thompson should use the name strdup(), when it is so very
>specific in its abbreviation, and has such a veneralable history in C
>circles (and Yoix, among others) as something completely different.  Not
>that it isn't appropos.  That coopting of the term is almost as good as the
>kind of stuff Microsoft does.

Interesting that you should mention this.  One of the (many) nice things
about TAWK is the ability to access "system" functions almost as seamlessly
as builtin functions (all it takes is an "extern" declaration of the
external function).  However, every once in a while, you run into a
name conflict between a desired system function and an AWK builtin.  I ran
into just such a situation with strdup().  The solution is as follows:

    extern void * name "strdup" _strdup(char *)

The "name" parameter is the actual name of the desired function in the
system library (libc.so, in this case) and the name after that is the name
by which this function will be known in the AWK program.  So, in the AWK
program, you end up with strdup() referring to the AWK builtin, and
_strdup() referring to the system function (which makes a non-volatile copy
of a string).

Cool, eh?



Thu, 28 Apr 2005 21:45:22 GMT  
 Repeat a string a specified number of times...

Quote:


> ...
> >How odd that Thompson should use the name strdup(), when it is so very
> >specific in its abbreviation, and has such a veneralable history in C
> >circles (and Yoix, among others) as something completely different.  Not
> >that it isn't appropos.  That coopting of the term is almost as good as
the
> >kind of stuff Microsoft does.

> Interesting that you should mention this.  One of the (many) nice things
> about TAWK is the ability to access "system" functions almost as
seamlessly
> as builtin functions (all it takes is an "extern" declaration of the
> external function).  However, every once in a while, you run into a
> name conflict between a desired system function and an AWK builtin.  I ran
> into just such a situation with strdup().  The solution is as follows:

>     extern void * name "strdup" _strdup(char *)

> The "name" parameter is the actual name of the desired function in the
> system library (libc.so, in this case) and the name after that is the name
> by which this function will be known in the AWK program.  So, in the AWK
> program, you end up with strdup() referring to the AWK builtin, and
> _strdup() referring to the system function (which makes a non-volatile
copy
> of a string).

> Cool, eh?

Very good.  (It's too bad that reference naming doesn't work for some of the
technologies that Microsoft has "embraced and enhanced.")  I'm glad that
TAWK does "extern" code, too.
    - Dan


Sat, 30 Apr 2005 09:30:00 GMT  
 Repeat a string a specified number of times...

Quote:


> ...
> >How odd that Thompson should use the name strdup(), when it is so
very
> >specific in its abbreviation, and has such a veneralable history in C
> >circles (and Yoix, among others) as something completely different.
Not
> >that it isn't appropos.  That coopting of the term is almost as good
as the
> >kind of stuff Microsoft does.

> Interesting that you should mention this.  One of the (many) nice
things
> about TAWK is the ability to access "system" functions almost as
seamlessly
> as builtin functions (all it takes is an "extern" declaration of the
> external function).  However, every once in a while, you run into a
> name conflict between a desired system function and an AWK builtin.  I
ran
> into just such a situation with strdup().  The solution is as follows:

>     extern void * name "strdup" _strdup(char *)

> The "name" parameter is the actual name of the desired function in the
> system library (libc.so, in this case) and the name after that is the
name
> by which this function will be known in the AWK program.  So, in the
AWK
> program, you end up with strdup() referring to the AWK builtin, and
> _strdup() referring to the system function (which makes a non-volatile
copy
> of a string).

> Cool, eh?

Yes it is.  Visual Basic and some other M$ language implementations have
similar renaming capabilities to allow for these types of name clash.

--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Sat, 30 Apr 2005 13:01:53 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. BUG: Scientific Number to String/String to Number

2. number to string .... string to number

3. Non-repeating Random Number Generator

4. help: how to print preview by specify the page number

5. how to specify unknown number of format before hand

6. How to count repeated strings in the lines ?

7. Help play movie, wait Xsec, repeat N times

8. indexing numbers as numbers or strings

9. VPREXX Help? Adding number to string number

10. number to string to number

11. Mac WorldView plug-in repeated load time delay?

12. Repeated string

 

 
Powered by phpBB® Forum Software