Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
no need to call it .txt –  ixtmixilix Nov 29 '11 at 23:41
add comment

4 Answers 4

up vote 21 down vote accepted

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

share|improve this answer
    
wooooooooooooooooooooooooooooooooooooooowwwwwwwwwww, you are a genius. That's it. I love unix more every second! Thanks. –  Digital Robot Nov 28 '11 at 23:55
    
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 –  Digital Robot Nov 28 '11 at 23:58
    
@DigitalRobot: At some point you're probably going to find yourself just writing perl one-liners. –  Jefromi Nov 29 '11 at 5:00
add comment

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
share|improve this answer
3  
I love zsh more and more every day. –  Christian Mann Nov 29 '11 at 4:20
1  
@Gilles your first example will continue forever because you never increment $i. –  Puddingfox Nov 29 '11 at 5:52
    
+1 for answering the question the OP would not have actually been able to ask ('i don't have seq, so i can't xyz') -- those sorts of answers are the real gems –  ixtmixilix Nov 29 '11 at 23:38
add comment

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
share|improve this answer
    
+1 never seen anyone use bc or dc like that before –  ixtmixilix Nov 29 '11 at 23:38
    
and it strikes me that your dc answer is uncommonly wry and upvote-worthy –  ixtmixilix Nov 29 '11 at 23:39
    
It was unnecessarily complicated, but not on purpose, so I have simplified it now. –  Peter John Acklam Nov 30 '11 at 8:38
add comment

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.

share|improve this answer
1  
You can use printf(1) to not get the space at the start of the line: printf '%s\n' {100..104} –  camh Nov 29 '11 at 7:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.