Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

how can i add the content of a text file in the starting of another text file?

here is the scenario. there are 3 file named f1 f2 f3. all of three have some text content. how to add the content of file f1 in the starting of file f3. and content of file f2 in the end of file f3.

share|improve this question

2 Answers 2

Make a temp dir.. touch temp

cat f1 >> temp .. temp now has the content of f1

cat f3 >> temp .. temp now has content of f1 and then f3

cat f2 >> temp .. temp now has the content in following order.. f1 f3 f2

mv temp f3 .. now file f3 contains f1 ..f3..f2

share|improve this answer
    
thanks for the logic abhishek.. –  pankaj sharma Mar 24 '12 at 9:50

Use the cat command. Using your example, cat f1 f3 f2 will con*cat*enate the files together so that it will read like f1, f3, then f2. It outputs to stdout, so if you want f3 to read like that, you would redirect to a temporary file and then move that file to f3: cat f1 f3 f2 > tmp ; mv tmp f3

share|improve this answer
    
thanks chan-ho suh, but i want to make the changes permanently –  pankaj sharma Mar 24 '12 at 9:51
1  
Hello. Sorry about that. You need to redirect the output of the cat command to a new file and then simply move that file to f3. Then the change is permanent. Take a look at my edited answer. –  Chan-Ho Suh Mar 24 '12 at 10:03
    
thank you chan-Ho –  pankaj sharma Mar 24 '12 at 10:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.