Using sed to insert text file at first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to insert text file at first line
# 8  
Old 03-28-2011
Quote:
Originally Posted by ctsgnb
@Bartus

LOL , you geek Smilie
Code:
 perl -e 'local $/;open A,"file.txt";open B,"source.txt";open C,">desti.txt";print C <A> . <B>'

Smilie
# 9  
Old 03-28-2011
insert file.txt to muliple files

Actually, I wanted to find a batch script that applies this insert 'file.txt' to all .c files in the current directory. So, in that case awk might be the best solution I guess:
Code:
find . -name "*.c" - exec awk 'NR==1{system("cat file.txt")}1' {} >{} \;

however, this command will wipe out all the contents in all .c files.
How do I further improve this script?

Thx.

Last edited by Scott; 03-28-2011 at 03:06 PM.. Reason: Code tags, please...
# 10  
Old 03-28-2011
Try:
Code:
for file in *.c
do
  cat file.txt "$file" > tempfile && mv tempfile "$file"
done

# 11  
Old 03-28-2011
Quote:
Originally Posted by Franklin52
Try:
Code:
for file in *.c
do
  cat file.txt "$file" > tempfile && mv tempfile "$file"
done

Yes but (just in case) if you care about keeping inodes of the $file you should


cat tempfile >"$file" ; rm tempfile instead of the mv
# 12  
Old 03-28-2011
Quote:
Originally Posted by ctsgnb
Yes but (just in case) if you care about keeping inodes of the $file you should


cat tempfile >"$file" ; rm tempfile instead of the mv
In that case you can remove the tempfile after the loop instead of in the loop:
Code:
for file in *.c
do
  cat file.txt "$file" > tempfile
  cat tempfile > "$file"
done
rm tempfile

# 13  
Old 03-28-2011
Obviously Smilie
# 14  
Old 03-28-2011
Thanks guys. One more difficulty. I run the following script from cygwin:

#!/bin/bash
for file in *.c
do
cat Head.c "$file" > tempfile && mv tempfile "$file"
done

...and get the following error:

./insert.sh: line 3: syntax error near unexpected token `$'do\r''
'/insert.sh: line 3: `do

..hmmm...what did I wrong?

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. UNIX for Dummies Questions & Answers

Insert a line in a text file

I want to insert a line with text after the 9th line of a text file. How would I do this using sed or awk? (2 Replies)
Discussion started by: lost.identity
2 Replies

4. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

5. Shell Programming and Scripting

Insert text file only after the first match with SED

Hello, I'm new in Shell scripting but i should write a script, which inserts the license header out of a txt-File into the files in our Projekt. For the Java classes it runs without Problems but for XML files not. At xml-files i have to put the license Header after the xml-Header (?xml... (1 Reply)
Discussion started by: PhoenixONE
1 Replies

6. Shell Programming and Scripting

sed insert text at particular line

I know that sed -n '12p' file will print line 12 but how might I insert text to a specified line? thanks (2 Replies)
Discussion started by: action_owl
2 Replies

7. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 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
Login or Register to Ask a Question