Padding Zeros in awk

Padding Zeros in awk

Post by joh » Thu, 06 Sep 2001 02:03:49



Thanks for the replies to my previous message - which led to this one.

I have an awk statement that looks like this:

awk '{print "cp", $1, "/directory/STR" $2}' infile

which copies $1 (the file) to $2 (the directory).  The trouble is, the
value of $2 needs to be 4 digits, with leading zeros, while the infile
has stripped off the leading zeros.  For example, if the file looks
like:

filename         8
filename        13
filename         5

Then the file is copied to /directory/STR8, /directory/STR13, and
/directory/STR5.

I need to find a way to pad the value of $2 with leading zeros, so in
the above example the filename would be copied to

/directory/STR0008
/directory/STR0013

etc.

I've been round and round with trying different variations of echo and
printf, and just can't seem to get it right - I may have a backtick or
quote problem, but I'm so new to awk and scripting in general, I don't
have a clue.

Any help would be appreciated - thanks!

 
 
 

Padding Zeros in awk

Post by Ben.Altma » Thu, 06 Sep 2001 02:22:13




Quote:> Thanks for the replies to my previous message - which led to this one.

> I have an awk statement that looks like this:

> awk '{print "cp", $1, "/directory/STR" $2}' infile

> which copies $1 (the file) to $2 (the directory).  The trouble is, the
> value of $2 needs to be 4 digits, with leading zeros, while the infile
> has stripped off the leading zeros.  For example, if the file looks
> like:

> filename         8
> filename        13
> filename         5

> Then the file is copied to /directory/STR8, /directory/STR13, and
> /directory/STR5.

> I need to find a way to pad the value of $2 with leading zeros, so in
> the above example the filename would be copied to

This worked for me (HP-UX):
        $ awk 'BEGIN {printf("%0.5d\n", 2)}'
        00002

regards,
Ben Altman

 
 
 

Padding Zeros in awk

Post by Chris F.A. Johnso » Thu, 06 Sep 2001 02:49:54



Quote:> Thanks for the replies to my previous message - which led to this one.

> I have an awk statement that looks like this:

> awk '{print "cp", $1, "/directory/STR" $2}' infile

> which copies $1 (the file) to $2 (the directory).  The trouble is, the
> value of $2 needs to be 4 digits, with leading zeros, while the infile
> has stripped off the leading zeros.  For example, if the file looks
> like:

> filename         8
> filename        13
> filename         5

> Then the file is copied to /directory/STR8, /directory/STR13, and
> /directory/STR5.

> I need to find a way to pad the value of $2 with leading zeros, so in
> the above example the filename would be copied to

> /directory/STR0008
> /directory/STR0013

> etc.

> I've been round and round with trying different variations of echo and
> printf, and just can't seem to get it right - I may have a backtick or
> quote problem, but I'm so new to awk and scripting in general, I don't
> have a clue.

while read filename directory
do
    cp $filename `printf "/directory/STR%04d" $directory`
done < infile

or:

awk '{  cmd = sprintf("cp %s /directory/STR%04d", $1, $2)
        system(cmd) }'

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

 
 
 

Padding Zeros in awk

Post by Sebastien DAUBIGN » Thu, 06 Sep 2001 07:27:40




Quote:> /directory/STR0008
> /directory/STR0013

awk '{printf("cp %s /directory/STR%03d\n",$1,$2)}'

See manual page for printf(1) or printf(3S).

 
 
 

Padding Zeros in awk

Post by Peter S Tillie » Thu, 06 Sep 2001 17:40:20





> > /directory/STR0008
> > /directory/STR0013

> awk '{printf("cp %s /directory/STR%03d\n",$1,$2)}'

The OP asked for 4 digit numbers so the format should be "%04d" ...

Quote:

> See manual page for printf(1) or printf(3S).

awk's printf does not always work exactly as shown in the printf man pages,
although the formats are normally the same as those shown in printf(3).  In
particular printf in awk is not a function, it is a statement, so that it
does not return a value when used.

man awk and read about the printf implementation there to be sure how it
works for the awk implementation that you are using.

HTH
--
Peter S Tillier

Opinions expressed are my own and not necessarily
those of my employer.

 
 
 

Padding Zeros in awk

Post by joh » Thu, 06 Sep 2001 21:51:08





> > /directory/STR0008
> > /directory/STR0013

> awk '{printf("cp %s /directory/STR%03d\n",$1,$2)}'

> See manual page for printf(1) or printf(3S).

Thanks, that's what I was looking for.  I echo that to a command file,
then run the command file.

Thanks again for everyone's responses!

 
 
 

1. How to zero pad numeric variables?

I want to zero pad an incrementing numeric variable so that I can sort
some thing alphabetically.

Does anyone know an easier way to zero pad numeric variables than
this nasty kludge I have have come up with?

set n = 000$n

expr substr $n `expr \( length $n \) - 2` `expr length $n`

Thanks

--

2. MySQL and C

3. problem with tar -- "padding with zeros" error

4. i am stuck greping

5. padding with zeroes ?

6. lilo: bad geometry 29/7/0

7. Floppy reports size as zero sectors, zero tracks, zero bytes

8. Icewm in redhat 6.2

9. Zero padding in sprintf() doesn't work for strings - HELP !

10. Padding a number with zeroes in a search and replace.

11. printf to pad with spaces BUT trips over when data being padded contains spaces

12. awk: best way to handle multiple files with awk ?

13. awk: Using awk as grep