creating a sequence of numbers, one per line in a file

73,885

Solution 1

There is already a command for this:

seq 100 104

will print these numbers on separate lines:

100
101
102
103
104

So just direct this output into a file:

seq 100 104 > my_file.txt

and seq 100 2 104 will print in increments of two, namely: 100, 102, 104

Solution 2

Linux ships with the seq command which does exactly that. If you don't have the seq command, it's an easy one-liner:

i=100; while [ $i -le 104 ]; do echo $i; i=$((i+1)); done >b.txt

or in ksh/bash/zsh

for ((i=100; i<=104; i++)); do echo $i; done >b.txt

or in zsh

print -l {100..104} >b.txt

Solution 3

bash:

printf '%s\n' {100..105}

perl:

perl -le 'print for 100..104'

bc:

echo 'for (i = 100 ; i <= 104 ; ++i) i' | bc

dc:

echo '100 104 sb [p 1 + d lb !<m] sm lm x' | dc

Solution 4

If you don't mind a space in front of most of them:

echo -e {100..104}\\n >numbers-file.txt

Without the space but with an extra command:

echo {100..104} | sed 's/ /\n/g' >numbers-file.txt

Edit for a bonus vim command (open vim):

i100[esc]qqyyp[ctrl-a]q2@q:w numbers-file.txt

For more numbers, increase 2 accordingly.

Solution 5

Besides using seq, while, for, printf, perl, echo as shown in previous example, you can also use Python

python -c "print list(range(100,105))"

Example:

[user@linux ~]~ python -c "print list(range(100,105))"
[100, 101, 102, 103, 104]
[user@linux ~]~ 
Share:
73,885

Related videos on Youtube

Duck
Author by

Duck

Updated on September 18, 2022

Comments

  • Duck
    Duck almost 2 years

    Is there a way to create out of thin air, a file that is a sequence of numbers, starting at a given number, one per line?

    something like

    magic_command start 100 lines 5 > b.txt
    

    and then, b.txt would be

    100
    101
    102
    103
    104
    
    • Matthew
      Matthew over 12 years
      no need to call it .txt
  • Duck
    Duck over 12 years
    wooooooooooooooooooooooooooooooooooooooowwwwwwwwwww, you are a genius. That's it. I love unix more every second! Thanks.
  • Duck
    Duck over 12 years
    is there a place where I can learn about little gems like seq? I am interested in commands that can create stuff out of thin air, like sequence of numbers, files that contain the same text line x times, commands that can generate sequence of letters "a, b, c, d..", stuff like that. thanks
  • Christian Mann
    Christian Mann over 12 years
    I love zsh more and more every day.
  • Cascabel
    Cascabel over 12 years
    @DigitalRobot: At some point you're probably going to find yourself just writing perl one-liners.
  • jamesbtate
    jamesbtate over 12 years
    @Gilles your first example will continue forever because you never increment $i.
  • camh
    camh over 12 years
    You can use printf(1) to not get the space at the start of the line: printf '%s\n' {100..104}
  • Matthew
    Matthew over 12 years
    +1 never seen anyone use bc or dc like that before
  • Matthew
    Matthew over 12 years
    and it strikes me that your dc answer is uncommonly wry and upvote-worthy
  • Peter John Acklam
    Peter John Acklam over 12 years
    It was unnecessarily complicated, but not on purpose, so I have simplified it now.
  • Kaz
    Kaz over 8 years
    @SpaceDog Your love is misplaced. seq is from GNU Coreutils, not Unix. GNU even stands for GNU is Not Unix!
  • don_crissti
    don_crissti over 6 years
    creating a sequence of numbers, one per line... and btw, this only works with python2.