LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-18-2007, 10:10 AM   #1
send2rawat
LQ Newbie
 
Registered: Jan 2007
Posts: 16

Rep: Reputation: 0
copy some text from one file to another


I have a text file, in that some text is there seperated by "$" sign.
suppose file name is ash.txt and in that some text like abcde$ghijk$lmnop$qrst . now I want to make new files as ash1.txt,ash2.txt and so on.... In that different text will be there which was sepetated by "$" sign. Like in ash1.txt there should be "abcde" and in ash2.txt there should be "ghijkl" and so on.....
Pls help me to do this
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-18-2007, 11:53 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

I think this can be done with a one-liner:

awk 'BEGIN { FS="" ; RS="$" ; i=1 } {print $0 > "ash"i".txt" } { i++ }' infile01

There is one minor shortcoming: The last file that is created also has an extra empty line.

Example:
Code:
$ cat infile01 
abcde$ghijk$lmnop$qrst

$ ls -l *ash*
ls: *ash*: No such file or directory

$ awk 'BEGIN { FS="" ; RS="$" ; i=1 } {print $0 > "ash"i".txt" } { i++ }' infile01

$ ls -l *ash*
-rw-r----- 1 druuna internet 6 Oct 18 18:51 ash1.txt
-rw-r----- 1 druuna internet 6 Oct 18 18:51 ash2.txt
-rw-r----- 1 druuna internet 6 Oct 18 18:51 ash3.txt
-rw-r----- 1 druuna internet 6 Oct 18 18:51 ash4.txt

$ cat ash1.txt 
abcde
$ cat ash4.txt 
qrst
              <----- the empty line I mentioned.
Hope this helps.
 
Old 11-09-2010, 05:39 AM   #3
pamuzef
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
Smile Copying file to another file

Hey,
m a new user in linux. I wonder how I can copy some contents from a file in a different directory to another file in another directory without overwriting the previous contents in the new file I want to paste my contents in question.

Any suggestion would be very appreciated.
 
Old 11-09-2010, 06:06 AM   #4
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
Using a double pipe sign will append the contents from the first file to the second. This is the sign: >>

Code:
file1 >> /home/user/file2
This will append the contents of file1 into file2.
 
Old 11-09-2010, 06:36 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,
Quote:
Originally Posted by pamuzef View Post
Hey,
m a new user in linux. I wonder how I can copy some contents from a file in a different directory to another file in another directory without overwriting the previous contents in the new file I want to paste my contents in question.
First: next time create a new thread, this one is 3 years old and not really relevant to your question.

Mark1986 gave a partial answer, but not a complete one....

If you want to append the contents of a file to another file you indeed need a double redirect (>>). A single one will overwrite a double one will append. But you also need a command that tells the system what to do with a file (Mark1986's example won't work, due to this). cat is a commonly used command to do so:

cat file1 >> /location/of/file2

Hope this helps.
 
2 members found this post helpful.
Old 11-09-2010, 06:59 AM   #6
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
Quote:
Originally Posted by druuna View Post

Mark1986 gave a partial answer, but not a complete one....

cat file1 >> /location/of/file2

Hope this helps.
You're right, I forgot the cat command first. Guess it's too standard for me at the moment. Sorry!
 
Old 11-10-2010, 01:04 AM   #7
pamuzef
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
Thank u guys... I get...it was helpful, and I had to make it easier by copying the file in the same directory and then append the file easily...
 
Old 11-10-2010, 01:15 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
You're welcome
 
  


Reply

Tags
copy, file, from, text


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can't copy text from firefox to vi file toedoe Linux - Software 2 08-26-2007 07:23 AM
Copy Text File and Paste to /hdd CD-RW aquatux Suse/Novell 3 12-29-2006 06:32 AM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
How to copy mysql file into text file lumba General 0 09-26-2005 05:08 AM
Copy text between terminals? Ryan_Sutton Linux - Newbie 3 07-06-2002 10:41 PM


All times are GMT -5. The time now is 02:29 PM.

Main Menu
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