LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   copy some text from one file to another (http://www.linuxquestions.org/questions/linux-general-1/copy-some-text-from-one-file-to-another-592767/)

send2rawat 10-18-2007 11:10 AM

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

druuna 10-18-2007 12:53 PM

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.

pamuzef 11-09-2010 06:39 AM

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.

Mark1986 11-09-2010 07:06 AM

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.

druuna 11-09-2010 07:36 AM

Hi,
Quote:

Originally Posted by pamuzef (Post 4153447)
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.

Mark1986 11-09-2010 07:59 AM

Quote:

Originally Posted by druuna (Post 4153510)

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!

pamuzef 11-10-2010 02:04 AM

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

druuna 11-10-2010 02:15 AM

You're welcome :)


All times are GMT -5. The time now is 09:00 PM.