[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#12482: Feature Request: support for int, octal, and hex types in seq
From: |
Jim Meyering |
Subject: |
bug#12482: Feature Request: support for int, octal, and hex types in seq --format |
Date: |
Fri, 21 Sep 2012 10:19:13 +0200 |
retitle 12482 RFE: seq: add support for int, octal, hex formats in --format
thanks
Voelker, Bernhard wrote:
> Craig Sanders wrote:
>
>> seq only supports floating point types like f and g in the --format string.
>>
>> Other types, including i,d,o,u,x,X would also be useful.
>>
>> e.g. "seq --format 'prefix%02isuffix' 1 50" to print zero-padded 1-50 with
>> user-specified prefix and suffix strings.
>
> IMO custom format strings for pre- or suffixing are not seq's job.
I agreed, initially. The texinfo documentation gives examples
of how to convert seq output to hexadecimal (%x) using printf:
--------------------
If you want hexadecimal integer output, you can use `printf' to
perform the conversion:
$ printf '%x\n' `seq 1048575 1024 1050623`
fffff
1003ff
1007ff
For very long lists of numbers, use xargs to avoid system
limitations on the length of an argument list:
$ seq 1000000 | xargs printf '%x\n' | tail -n 3
f423e
f423f
f4240
To generate octal output, use the printf `%o' format instead of `%x'.
--------------------
> The OP wanted a little shell solution to create 50 directories
> with a fixed prefix and suffix, so what about this?
>
> seq -w 50 | sed 's/^/prefix/; s/$/suffix/' | xargs mkdir
Hmm...
The first time I ran an example like the above, I used -w
without realizing that there was no need, since the printf
format would handle the fixed width part.
(the disadvantage with this approach is that you have to pre-compute
the width and use that number in the printf format, whereas in Bernie's
example, that's done automatically by seq -w. This suggests that seq's
--equal-width (-w) option *would* be handy in conjunction with the requested
integer format directives. Then, we'd get the benefit of -w along with
the more direct use of a seq format string. )
$ seq -w 12 | xargs printf 'a-%02x-b\n'
a-01-b
a-02-b
a-03-b
a-04-b
a-05-b
a-06-b
a-07-b
a-printf: 08: value not completely converted
00-b
a-printf: 09: value not completely converted
00-b
a-0a-b
a-0b-b
a-0c-b
[Exit 123]
That looks like a bug, but printf is actually required to reject
those two input strings. A leading "0" means octal.
This does what you want:
$ seq 12 | xargs printf 'a-%02x-b\n'
a-01-b
a-02-b
a-03-b
a-04-b
a-05-b
a-06-b
a-07-b
a-08-b
a-09-b
a-0a-b
a-0b-b
a-0c-b