LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-26-2012, 11:44 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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
 
Old 04-26-2012, 11:48 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Code:
seq -w 10 > $OutFile
?
 
1 members found this post helpful.
Old 04-26-2012, 12:35 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Original Poster
Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
Quote:
Originally Posted by ntubski View Post
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
 
Old 04-26-2012, 12:50 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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
 
1 members found this post helpful.
Old 04-26-2012, 12:57 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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
 
1 members found this post helpful.
Old 04-26-2012, 01:15 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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

Last edited by David the H.; 04-26-2012 at 01:18 PM. Reason: added one
 
1 members found this post helpful.
Old 04-26-2012, 01:38 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Original Poster
Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
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
 
Old 04-26-2012, 02:06 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
oops ... my bad ... just realised it was a single file with multiple columns and not multiple files with a single column
 
Old 06-14-2012, 07:38 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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"
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Squid Proxy cache dynamic web content generated by POST method? di11rod Linux - Software 1 06-16-2011 01:32 PM
Dividing content of one file by content of another larspend Linux - Newbie 5 04-12-2011 09:00 PM
how to create a new file including content from reading another file !? silverhand Programming 2 10-06-2007 05:33 PM
How to create smaller PDF files? OpenOffice or Abiword generated a large one. Mr_Shameless Linux - Software 2 09-11-2007 03:52 PM
Maillog file not being generated sanjibgupta Linux - Newbie 1 02-13-2007 12:33 AM


All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration