LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create a file with generated content (https://www.linuxquestions.org/questions/programming-9/create-a-file-with-generated-content-941877/)

danielbmartin 04-26-2012 10:44 AM

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

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 ...
Code:

01
02
03
04
05
06
07
08
09
10

Please advise.

Daniel B. Martin

ntubski 04-26-2012 10:48 AM

Code:

seq -w 10 > $OutFile
?

danielbmartin 04-26-2012 11:35 AM

Quote:

Originally Posted by ntubski (Post 4663628)
Code:

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 11:50 AM

First thing that springs to mind is awk:
Code:

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:
Code:

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


grail 04-26-2012 11:57 AM

Or more bash:
Code:

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 12:15 PM

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

Code:

#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.


printf
brace expansion

danielbmartin 04-26-2012 12: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 01: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 06: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.

Code:

$ 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}"
foofoofoofoofoofoo

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.

Code:

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


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