-   Programming (
-   -   Create a file with generated content (

danielbmartin 04-26-2012 11:44 AM

Create a file with generated content
I am able to create a file with ...

cat < '/dev/null'  \
> $OutFile

... but this is an empty file. I'd like to create a file with content my code generates such as ten lines with each line containing a sequential number created by nl. In other words, start with nothing and create ...


Please advise.

Daniel B. Martin

ntubski 04-26-2012 11:48 AM


seq -w 10 > $OutFile

danielbmartin 04-26-2012 12:35 PM


Originally Posted by ntubski (Post 4663628)

seq -w 10 > $OutFile

Thank you for your prompt response. A lovely concise solution.

Allow me to broaden the question by proposing similar examples.

1) How may I create a new file consisting of six records, with each record containing nine blank characters?

2) How may I create a new file consisting of four records, with each record containing the characters QWERTY?

I could use seq to create the file (as you taught) and then sed to substitute other content. Is there a more direct method?

Daniel B. Martin

ntubski 04-26-2012 12:50 PM

First thing that springs to mind is awk:

awk 'BEGIN{for(i=0; i<6; i++) print "        "}'
awk 'BEGIN{for(i=0; i<4; i++) print "QWERTY"}'

Another is possibility is yes with head:

yes '        ' | head -6
yes QWERTY | head -4

grail 04-26-2012 12:57 PM

Or more bash:

for i in {1..6};do echo "        " > file$i;done
for i in {1..4};do echo "QWERTY" > File$i;done

David the H. 04-26-2012 01:15 PM

You can do an awful lot in the shell by using combinations of printf, brace expansion, and/or loops.


#prints numbers 01 to 08, one number per line
printf '%s\n' {01..08} > outfile

#print six lines with 9 blanks each
for ((x=1; x<7; x++)); do echo "        " >> outfile ; done

# or as a single redirection
{ for ((x=1; x<7; x++)); do echo "        " ; done ;} >outfile

#print "QWERTY" four times
printf '%s\n' QWERTY{,,,} > outfile

The last one uses a neat little brace expansion trick. I generates the string "QWERTY<nothing>" four times. you could also of course change the string to quoted spaces and use it instead of the loop in the second example.

brace expansion

danielbmartin 04-26-2012 01:38 PM

Thanks to all LQers who posted to this thread.
It's good to see different approaches to solving a problem.
Let's mark this one SOLVED!

Daniel B. Martin

grail 04-26-2012 02:06 PM

oops ... my bad ... just realised it was a single file with multiple columns and not multiple files with a single column :redface:

David the H. 06-14-2012 07:38 PM

Since I just dug up this old thread for posting elsewhere, I thought I might take the opportunity to add one last trick.

If you want to duplicate a character or string an arbitrary number of times (say from a number contained in a variable), you can use a combination of printf and parameter substitution.


$ n=6
$ x=#

$ printf -v line "%${n}s"        #stores a string of $n spaces in variable $line
$ echo "${line// /$x}"                #replaces the spaces with the contents of $x

$ x=foo
$ echo "${line// /$x}"

You can alternately pass the padding setting to the printf format from outside, using "*". It will use the number from the first argument as the setting.


printf -v line '%*s' "$n"

All times are GMT -5. The time now is 07:38 PM.