Insert file.txt recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert file.txt recursively
# 1  
Old 03-28-2011
Insert file.txt recursively

Anyone knows how I can change this script so that it works recursively as well (meaning: *.c files in sub directories will get changed as well)?
Code:
for file in *.c
do
  cat file.txt "$file" > tempfile
  cat tempfile > "$file"
done
rm tempfile


Last edited by Franklin52; 03-28-2011 at 04:52 PM.. Reason: Please use code tags
# 2  
Old 03-28-2011
try:
Code:
for file in `find -type f -name *.c |sed 's/\.\///'`
do
sed -i '1{x;s/.*/cat file.txt/ep;x;}' $i
done


Last edited by yinyuemi; 03-28-2011 at 04:46 PM..
# 3  
Old 03-28-2011
Code:
# Find all directories inside ., including . itself, and print to stdout line by line
find . -type d | while read DIR # Read each line
do
        for FILE in "${DIR}"/*.c
        do
                # Ignore dirs without c files
                [ -f "$FILE" ] || break

                # Remove the echo, and change '>' to > when you're sure it looks right
                echo cat file.txt "$FILE" '>' "$DIR"/tempfile
                echo cat "$DIR"/tempfile '>' "$FILE"
        done
done

# 4  
Old 03-28-2011
If you're not shy about editing in place:
Code:
find . -type f -name \*.c -exec sh -c '
    for f; do
        printf "%s\n" "0r file.txt" w q | ed -s "$f"
    done
' sh {} +

---------- Post updated at 04:08 PM ---------- Previous update was at 04:01 PM ----------

If your find does not support aggregation with the exec...+ syntax:
Code:
find . -type f -name \*.c -exec sh -c 'printf "%s\n" "0r fill.txt" w q | ed -s "$1"' sh {} \;

Regards,
Alister
# 5  
Old 03-28-2011
Through 'ed' works slow. I think I like this the best:
Code:
find . -type f -name "*.c" -exec sh -c 'cat Head.txt "$1" > test.txt && mv test.txt "$1"' sh {} \;


Last edited by Franklin52; 03-29-2011 at 05:29 AM.. Reason: Please use code tags
# 6  
Old 03-28-2011
Interesting. On my system, testing on 30,000+ c files, your proposed solution is the slowest of all (with the exec...+ approach being twice as fast).

Code:
$ find c-files/ -type f -name \*.c | wc -l
   34115


$ time find . -type f -name "*.c" -exec sh -c 'cat fill.txt "$1" > test.txt && mv test.txt "$1"' sh {} \;

real    7m54.556s
user    1m20.358s
sys     5m37.358s


$ time find . -type f -name \*.c -exec sh -c 'printf "%s\n" "0r fill.txt" w q | ed -s "$1"' sh {} \;

real    6m52.460s
user    1m18.580s
sys     5m9.348s


$ time find . -type f -name \*.c -exec sh -c '
> for f; do
> printf "%s\n" "0r fill.txt" w q | ed -s "$f"
> done
> ' sh {} +

real    4m7.945s
user    0m48.030s
sys     3m21.675s

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

2. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

4. UNIX for Dummies Questions & Answers

shell script : log to txt and insert new line everytime

Hi, I have this script, while do ps ax|grep 5060 > log.txt echo " " sleep 1 done } I want to actually put a new line everytime the loop is executed in log.txt , but I do not know how to "embed" the echo " " inside the log.txt. (so to say... (1 Reply)
Discussion started by: peuceul
1 Replies

5. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

6. UNIX for Dummies Questions & Answers

sed insert content of file.txt to multi files

Ive this sed & find command find /home/www/ -name footer.php -exec sed -i 's/<\/body>/file.txt\n<\/body>/' what I need to place content of file.txt before </body> in all footer.php files file.txt content is google analytic script which is like 7 lines any help to adjust my command to... (2 Replies)
Discussion started by: xmoe
2 Replies

7. Shell Programming and Scripting

insert txt in the row

hi i am having text file like this 444 raju 666 ranga Clerk 999 rani officer 111 juhi i want to get the out put as 444 raju NA 666 ranga Clerk 999 rani officer 111 juhi NA pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

8. Shell Programming and Scripting

shell script to insert data from gps.txt to mysql database

Hi, I have gps receiver, by using gpsd data i can read gps log data to my database(my sql). Steps: 1. telenet localhost 2947 > gps.txt (press enter) 2. r (press enter) //then i will get the data like below in gps.txt file Trying 127.0.0.1... Connected to localhost.... (1 Reply)
Discussion started by: gudivada213
1 Replies

9. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

10. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies
Login or Register to Ask a Question