Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I create a data file with one column in which there will be 1000 rows with zero values?

something like:

output:

0
0
0
0
0
.
.

.

share|improve this question
up vote 7 down vote accepted

Simply,

printf '0\n%.0s' {1..1000}

or using for loop,

for i in {1..1000}; do echo "0"; done

using awk,

awk 'BEGIN{for(c=0;c<1000;c++) print "0"}'

As @StéphaneChazelas pointed out, Using {1..1000} requires zsh or recent versions of bash, yash or ksh93 and also means storing the whole range in memory (possibly several times). You'll find it becomes a lot slower (if it doesn't crash for OOM) than using awk or yes 0 | head ... for large ranges like {1..10000000}. Or in other words it doesn't scale well. Possible workaround would be to use

for ((i=0; i<=10000000;i++)); do echo 0; done 

(ksh93/zsh/bash) wouldn't have the memory issue but would still be orders of magnitude slower than a dedicated tool or real programming language approach.

share|improve this answer
    
The printf command might fail when 1000 gets replaced with 1000000 because the shell would fail execve(2) with E2BIG – Basile Starynkevitch yesterday
1  
Using {1..1000} requires zsh or recent versions of bash, yash or ksh93 and also means storing the whole range in memory (possibly several times). You'll find it becomes a lot slower (if it doesn't crash for OOM) than using awk or yes|head for large ranges like {1..10000000}. Or in other words it doesn't scale well. – Stéphane Chazelas yesterday
    
@BasileStarynkevitch, the shells that support {x..y} (zsh, ksh93, bash and yash) all have printf builtin, so the E2BIG doesn't apply. – Stéphane Chazelas yesterday
    
@StéphaneChazelas I am completely agree with you. Is there any other workaround in this case ? – Rahul yesterday
1  
for ((i=0; i<=10000000;i++)); do echo 0; done (ksh93/zsh/bash) wouldn't have the memory issue but would still be orders of magnitude slower than a dedicated tool or real programming language approach. – Stéphane Chazelas yesterday

You might use yes(1) for that (piped into head(1)...):

yes 0 | head -n 1000 > data_file_with_a_thousand_0s.txt

and if you need a million zeros, replace the 1000 with 1000000

PS. In the old days, head -1000 was enough since equivalent to head -n 1000 today.

share|improve this answer
1  
This definitely feels like "the Unix way" - glue some simple elements together into a pipe. It's worth noting though that the bare "-1000" argument is deprecated and the standard form (as listed on the man page you linked) is now "-n 1000". See e.g. unix.com/man-page/posix/1p/head and gnu.org/software/coreutils/manual/html_node/head-invocation for confirmation of this history. – IMSoP 21 hours ago
    
Brilliant. Didn't know yes existed. – Tulains Córdova 18 hours ago
perl -e 'print "0\n" x 1000' > file.txt
share|improve this answer
1  
I find it's by far the fastest for large numbers of rows (even than its python -c 'import sys; sys.stdout.write("0\n" * 1000)' equivalent or the yes|head approach). – Stéphane Chazelas yesterday
1  
However, it means storing the whole output in memory before printing it so also has scalability issues. – Stéphane Chazelas yesterday
python2 -c 'print "0\n" * 1000' > file.txt
share|improve this answer
    
that will print an extra blank line, you would have to use trailing comma (,) at the end of command to prevent this. – Rahul yesterday
    
Agree. Thanks.. – Mrigesh yesterday
1  
This only works in the old python2, in Python 3 print no longer is a statement – Anthon yesterday

seq could be used:

seq 0 0 0 | head -1000
share|improve this answer

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.