Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to make several copies of a line of text, following a numeric order.

exampleline:

exampleline1
exampleline2
exampleline3
etc..

What's the basic way to get the desired number of copied lines into a file?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

In bash you do something like this:

bash-4.1$ for i in {1..5}; do
> echo exampleline$i
> done > examplefile.txt
bash-4.1$ cat examplefile.txt
exampleline1
exampleline2
exampleline3
exampleline4
exampleline5
bash-4.1$

And here's how you might do it with tcsh. (I had to use the >> append operator because tcsh doesn't support i/o redirection on a control structure.)

tcsh-6.18.01% @ i = 1
tcsh-6.18.01% while ( $i <= 5 )
while? echo exampleline$i >> examplefile.txt
while? @ i++
while? end
tcsh-6.18.01% cat examplefile.txt
exampleline1
exampleline2
exampleline3
exampleline4
exampleline5
tcsh-6.18.01%
share|improve this answer
    
thanks so much, the bash example worked great –  z4nb0t Feb 24 '13 at 17:28
    
@charley If you click to "accept" my answer you'll push me over 5000. Yippee. :) –  Nicole Hamilton Feb 24 '13 at 17:40

In bash using printf:

printf 'exampleline%s\n' {1..5} > examplefile.txt
share|improve this answer
3  
This is a very clever use of this (frankly, advanced user) behavior in bash. This particular syntactical feature has always struck me as an intriguing design choice. I'm not sure why Fox chose to make bash behave this way, iterating over the list, rather than either (a) like the C library printf, which takes only the first arg and ignores the rest, producing exampleline1 or (b) coercing the list of numbers 1 through 5 into a string, 1 2 3 4 5, producing the result exampleline1 2 3 4 5. What's odd about it is that feature only applies to printf, not just any command. –  Nicole Hamilton Feb 24 '13 at 17:58

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.