seq for OS X

It has always irked me that OS X doesn’t have the seq command (I am easily irked). Brian Peterson’s old e-mail Re: seq from core utils has it, but the link to sh-utils doesn’t work any more, since the project has been archived. Here’s the new link: http://ftp.gnu.org/old-gnu/sh-utils/. Compile it as Brian suggested, and all will be well.

$ seq 1 12
1
2
3
4
5
6
7
8
9
10
11
12

Joy!
(at least 99% of you will be mystified why anyone would want this.)

Comments

21 Responses to “seq for OS X”

  1. jasper Avatar

    .. or install macports and do a
    $ sudo port install coreutils
    which will get you a gseq:
    $ gseq 1 12
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

  2. scruss Avatar

    Yeah, but it’s called ‘seq’ in all the scripts I have, and it wouldn’t be worth me having macports for just one command.

  3. markus Avatar
    markus

    Hi, agreed, it bothers me too…. so much did i want this functionality without needing to do darwin ports … and having seen your recent post on this topic, i’ve just knocked together a 2 min bash script to add this functionality without actually installing anything,

    i just made this script and called it /usr/bin/seq
    chmod +x /usr/bin/seq
    and i’m good to go..

    … i also hope this posts ok…. i have a feeling it may get filtered by the forum… maybe you can figure it out again

    #!/bin/bash
    # marks rude seq replacement for osx.

    if [ $# -eq 2 ]
    then
    let from=”$1″;
    let to=”$2″;
    elif [ $# -eq 1 ]
    then
    let from=”0″;
    let to=”$1″;
    else
    echo “USAGE: seq [num from] [num to]”
    exit 1;
    fi

    while [ $from -lt $to ]
    do
    printf “$from “;
    from=$[$from+1]
    done
    printf “$from “;

  4. Robert Borkowski Avatar
    Robert Borkowski

    sudo ln -s /opt/local/bin/gseq /opt/local/bin/seq

    🙂

  5. scruss Avatar

    see the second comment, Robert. And /opt is a crime against nature!

  6. scruss Avatar

    Oh, and there’s also always ‘jot’, seq’s weird hippie cousin which is installed by default: https://web.archive.org/web/20110105235757/http://www.nevdull.com/2007/09/24/gnu-seqs-cousin-on-freebsd-is-jot/

    Nice try with that shell, Markus. It would work for maybe 80% of uses, but would fail for the rest.

  7. scruss Avatar

    Playing with jot a bit (I’m on a train), I rather like it. What jot can do in one command

    jot -w %02x 255 0

    (printing hexadecimal numbers 00 .. ff) seq needs a composite command:

    seq 0 255 | xargs -n1 printf “%02x\n”

    neat!

  8. prof_k Avatar
    prof_k

    Just an improvement of the bash script (which doesn’t yet work properly for jot with an increment). Further improvement is welcome !

    #!/bin/bash
    # marks rude seq replacement for osx.

    # set -ex

    method=1

    case ${method} in
    1)
    # a simple bash script:
    if [ $# -eq 2 ]
    then
    let from=”$1″;
    let to=”$2″;
    let step=1
    elif [ $# -eq 1 ]
    then
    let from=”0″;
    let to=”$1″;
    let step=1
    elif [ $# -eq 3 ]
    then
    let from=”$1″
    let to=”$3″
    let step=”$2″
    else
    echo “USAGE: seq [num from] [[num step]] [num to]”
    exit 1;
    fi

    while [ $from -le $to ]
    do
    printf “$from \n”;
    from=$[$from+$step]
    done
    ;;
    2)
    # Using the jot command:

    if [ $# -eq 2 ]
    then
    let numstep=`expr ‘(‘ $2 – $1 + 1 ‘)’ `
    jot $numstep $1 $2
    elif [ $# -eq 3 ]
    then
    let numstep=`expr ‘(‘ $3 – $1 + 1 ‘)’ / $2 `
    let to=`expr $3 – ‘(‘ ‘(‘ $3 – $1 + 1 ‘)’ % $numstep ‘)’ `
    jot ${numstep} $1 $to
    else
    echo “USAGE: seq [num from] [[num step]] [num to]”
    exit 1;
    fi
    ;;
    esac

  9. scruss Avatar

    thanks. WordPress may have munged the quotes – I hate when it does that.

  10. someone Avatar
    someone

    Thanks, but the seq builds are for PPC and not for intel.

    but the macports idea is good!

  11. scruss Avatar

    erm, but the sh-utils build trick is platform-neutral.

  12. bash Avatar
    bash

    If you’re using BASH (the default in OS X), try just using the curly braces BASH expansion:

    for i in {1..10}; do echo $i; done

  13. seth Avatar
    seth

    just install coreutils and then link to /usr/bin

    1. sudo port install coreutils
    2. which gseq
    3. sudo ln -s /opt/local/bin/gseq /usr/bin/seq

  14. scruss Avatar

    err, installing macports and coreutils is a bunch of work. And I still stand by what I said about /opt – if not more so!

    I like the bash solution best of all, ‘cos I don’t have a single machine without it.

  15. chupoptero Avatar
    chupoptero

    The curly brackets doesn’ t seem to work on all platforms. i am using bash on 10.4 and it doesn’ t recognizes it. jot worked fine and I didn’ t have to install anything. Thanks for the tip!

  16. Michal Chruszcz Avatar
    Michal Chruszcz

    I created a very similar tool in Python: http://python.pastebin.com/Ubcy1xdp. It features a subset of original seq options, providing all of the essential ones, though.

  17. scruss Avatar

    I’m now totally at one with jot, and wish it were on all my systems.

  18. crasyboy Avatar
    crasyboy

    sudo port install coreutils
    sudo ln -sf /opt/local/bin/gseq /opt/local/bin/seq

  19. scruss Avatar

    yes, crasyboy – but jot is actually much easier to use, and more flexible. Double win!

  20. […] but it gave a encoding error. A bit of googling around led me to conclude that the file was utf-8 with BOM. To strip the BOM from the file I used the shell script found here: http://thegreyblog.blogspot.com/2010/09/shell-script-to-find-and-remo… (if you are on OS X then you will have to install seq for it to work - instructions here: http://scruss.com/blog/2008/02/08/seq-for-os-x/ […]

  21. […] Quelle 25.07.2011 Categories: Linux, Mac, OSX Tags: bash, brew, coreutils, homebrew, install, linux, macports, numbers, OSX, qseq, range, seq Kommentare (0) Trackbacks (0) Kommentar schreiben Trackback […]

Leave a Reply

Your email address will not be published. Required fields are marked *