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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-26-2012, 10:44 AM
|
#1
|
|
Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 765
Rep: 
|
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
|
|
|
|
04-26-2012, 10:48 AM
|
#2
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,023
|
Code:
seq -w 10 > $OutFile
?
|
|
|
1 members found this post helpful.
|
04-26-2012, 11:35 AM
|
#3
|
|
Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 765
Original Poster
Rep: 
|
Quote:
Originally Posted by ntubski
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
|
|
|
|
04-26-2012, 11:50 AM
|
#4
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,023
|
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.
|
04-26-2012, 11:57 AM
|
#5
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,329
|
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.
|
04-26-2012, 12:15 PM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589
|
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 12:18 PM.
Reason: added one
|
|
|
1 members found this post helpful.
|
04-26-2012, 12:38 PM
|
#7
|
|
Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 765
Original Poster
Rep: 
|
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
|
|
|
|
04-26-2012, 01:06 PM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,329
|
oops ... my bad ... just realised it was a single file with multiple columns and not multiple files with a single column 
|
|
|
|
06-14-2012, 06:38 PM
|
#9
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:12 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|