LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-26-2011, 11:38 PM   #1
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Rep: Reputation: Disabled
How do I paste text into another text?


How do I take the contents from one file and paste it into several other files?

=====================================

Example:

#file-01.txt
cat
dog
bird

#file-02.txt
tree

#file-03.txt
flower

=====================================

My goal:

#file-02.txt
cat
dog
bird
tree

#file-03.txt
cat
dog
bird
flower

=====================================

Last edited by Mouse750; 08-26-2011 at 11:50 PM.
 
Old 08-27-2011, 12:12 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Since you want to append the content of one file onto another, the solution is trivial.
Code:
cat file1 >> file2
appends file1 to file2. The '>>' shell operator is the same as '>' but uses 'append' mode instead of 'over-write'.

--- rod.
 
Old 08-27-2011, 12:49 AM   #3
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
2 problems.

First,

=====================================

I ended up with:

#file-02.txt
tree
cat
dog
bird


#file-03.txt
flower
cat
dog
bird

=====================================

Instead of my goal.

Second,

I got an error when I tried a wild card

cat /first/path/file-01.txt >> /second/path/*txt

/second/path/*txt: ambiguous redirect

Last edited by Mouse750; 08-27-2011 at 12:53 AM.
 
Old 08-27-2011, 01:10 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
If you have Ruby(1.9+)
Code:
#!/usr/bin/env ruby
data=File.open("file-01").read
ARGF.argv.each do |file|
    o=File.open("temp","w")
    o.write(data)
    File.open(file).each { |x| o.write(x) }
    o.close
    File.rename("temp",file)
end
Pass the files are argument on the command line
Code:
$ ruby myscript.rb file-02.txt file-03.txt
 
Old 08-27-2011, 01:28 AM   #5
EmaRsk
Member
 
Registered: Mar 2006
Distribution: Mint, WSL Ubuntu
Posts: 134

Rep: Reputation: 32
Bash:
Code:
mv file-01.txt foo
for target in file-??.txt; do
    mv $target bar
    cat foo bar >$target
done
rm bar
mv foo file-01.txt
 
Old 08-27-2011, 06:22 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Mouse750 View Post
cat /first/path/file-01.txt >> /second/path/*txt
You can't redirect into multiple files.

Also, use "*.txt", not "*txt". That way, you only match ".txt" files, not files whose names happen to end with "txt" (with or without the dot). Remember, Linux has no concept of file extensions.
 
1 members found this post helpful.
Old 08-27-2011, 11:05 AM   #7
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
EmaRsk Can you please explain your code, I new at this. Also I need to paste this into several files, can I do that?

Thanks.
 
Old 08-28-2011, 03:52 AM   #8
EmaRsk
Member
 
Registered: Mar 2006
Distribution: Mint, WSL Ubuntu
Posts: 134

Rep: Reputation: 32
Rename file-01.txt so it won't get matched by the "for" loop:
Code:
mv file-01.txt foo
For every file whose name match the shell pattern "file-??.txt" ("file-", 2 characters, ".txt"), do whatever is written until "done", with the file name being stored in the variable "target".
Code:
for target in file-??.txt; do
rename the file so we won't mess with the next step:
Code:
    mv $target bar
concatenate the content of "foo" (was "file-01.txt") and the file we just renamed into a file with that name:
Code:
    cat foo bar >$target
end of the "for" loop:
Code:
done
remove the last renamed file:
Code:
rm bar
and you can rename back "file-01.txt"
Code:
mv foo file-01.txt
"foo" and "bar" are just two names for temporary files: I wrote this code thinking that you'd type it directly in the shell, so you can check they won't accidentally overwrite existing files and change them if necessary, but if this is going into a script, you'll need some extra code to make the script check that.
Also, this code works only with the given set of filenames, if you need to "paste this into several files" you'll probably need a bit of reworking to make it more generic.
 
Old 08-28-2011, 12:28 PM   #9
Mouse750
Member
 
Registered: Aug 2011
Distribution: Ubuntu
Posts: 50

Original Poster
Rep: Reputation: Disabled
Thank You EmaRsk!
 
  


Reply



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
How to Copy/Paste from Nano to an outside text editor big_matt Linux - Newbie 4 03-28-2011 02:50 AM
[SOLVED] copy and paste text among text documents in Linux ethereal1m Linux - Newbie 5 03-28-2010 03:14 AM
paste commonly used chunks of text? Chriswaterguy Linux - Software 10 11-30-2009 07:28 AM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
paste text from a virtual console to X? Rhatlinux Slackware 1 10-12-2004 08:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 AM.

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
Open Source Consulting | Domain Registration