Visit Our UNIX and Linux User Community


adding text from a file to the top of the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers adding text from a file to the top of the file
# 1  
Old 04-09-2009
adding text from a file to the top of the file

Hi all,

I need a include a text from the file and include it in the top of the other file..

i.e file 1 contains
AAA
BBB
CCC
DDD
EEE
file2 contains
333
44
23
452
54235
232341
4321414

Now i need to insert the text from file1 to file2 at the top of the file2.

The output file should contain:

AAA
BBB
CCC
DDD
EEE
333
44
23
452
54235
232341
4321414

Please help on this...
# 2  
Old 04-09-2009
Code:
cat file1 file2 > newfile

# 3  
Old 04-09-2009
Hi Zaxxon,

Thanks for your quick reply.
The problem is I should not use the new file. In file2 itself the contents of file1 have to be included...so usage of new file is not allowed here....
# 4  
Old 04-09-2009
Why that? Is there no space in the filesystems left? I have only solutions with at least having a temp file in the process, that you can mv to the filename you desire.
Else you might want to write a C program or maybe it could be done with Perl, idk.
# 5  
Old 04-09-2009
Maybe it is acceptable for you to have the new contents in file1 instead of file2 which would make it very easy of course:

Code:
cat file2 >> file1

Edit:

Maybe sed -i is an option for you.
# 6  
Old 04-09-2009
I am using kornshell...when i give sed -i '1i $file1' > file2

Its showing illegal option -i...or sed command garbled...
# 7  
Old 04-09-2009
Code:
printf '$\nr file2\nw! file2\nq!' | ex - file1

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #142
Difficulty: Easy
Version 4 Unix was rewritten in C in 1977.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

2. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

3. Shell Programming and Scripting

Adding text into file

Hello, I after some assistance please. Below is a file I need to read and for each line write an output. My input file looks like this 2 20008 2003-08-26 2 20032 2003-08-26 2 20041 2003-08-26 2 20037 2003-08-26 2 20050 2003-08-26 6 ... (6 Replies)
Discussion started by: giles.cardew
6 Replies

4. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

5. Shell Programming and Scripting

adding text to a file

Hi All I am making a script in which.I have a problem i try to discribe. File1 content need to be append in file 2 but such as if content already exist then dont add. File1 IS Like this myname yourname hername hisname File2 %AddNameHere myname yourname hername hisname (3 Replies)
Discussion started by: aliahsan81
3 Replies

6. UNIX for Dummies Questions & Answers

appending text on top of another file

Dear All, I have two files One is script file in which I am writing commands to append a text in a normal file. I want to insert the text on top of the file. I dont want to use sed and awk commands nor temp file. is it possible? (3 Replies)
Discussion started by: anjali
3 Replies

7. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

8. UNIX for Dummies Questions & Answers

Shell scripting adding text to top of file

Hi this is quite simple i am sure but without using awk or sed i need to add text to the top of a file this is what i have got so far #!bin/bash echo "Add text to top of file" read line echo $line >> file1 This adds the text to the bottom of the file can some1 please help cheers (7 Replies)
Discussion started by: meadhere
7 Replies

9. Shell Programming and Scripting

insert text into top of file

how would you insert text into a existing file using aguments first arguments being the line of text and the second argument being file name (1 Reply)
Discussion started by: jimbob
1 Replies

10. Shell Programming and Scripting

SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed? (12 Replies)
Discussion started by: MBGPS
12 Replies

Featured Tech Videos