Appending a files contents to the end of a specific file name in several directories


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Appending a files contents to the end of a specific file name in several directories
# 1  
Old 11-29-2013
Appending a files contents to the end of a specific file name in several directories

Here is my dir structure:
Code:
/tmp/dave/myappend.txt
/tmp/dave/dir1/test.txt
/tmp/dave/dir2/test.txt
/tmp/dave/dir3/test.txt
/tmp/dave/dir4/test.txt

I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/

I have tried this:
Code:
find /tmp/dave/* -type d -exec sed -e "\$a/r /tmp/dave/myappend.txt" -e "//d" test.txt {}/ \;

and this is what I get:
Code:
sed: -e expression #2, char 0: no previous regular expression
sed: -e expression #2, char 0: no previous regular expression
sed: -e expression #2, char 0: no previous regular expression
sed: -e expression #2, char 0: no previous regular expression


Last edited by Franklin52; 11-30-2013 at 09:08 AM.. Reason: Please use code tags
# 2  
Old 11-30-2013
With this directory structure, the simplest thing to do is:
Code:
cd /tmp/dave
for i in */test.txt
do      cat append.txt >> $i
done

# 3  
Old 11-30-2013
Beside Don's straight solution, an untested attempt to improve your attempt with sed
Code:
find /tmp/dave/* -type f -name test.txt -type d -exec sed '$r /tmp/dave/myappend.txt' {} \;

This goes all to stdout. To write back to the input files, use sed -i option.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending newline character End of File

Hi Gurus, Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file. Sample Data: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#Output: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#\n -- append only... (13 Replies)
Discussion started by: Gouri Solleti
13 Replies

2. Shell Programming and Scripting

appending a blank line for a group of files at the end

hi, i m having a group of files starting with name 'Itemdelete<timestamp>' . my requirment is to append a blank line at the end of files ,using unix in all the Itemdelete* files with a single unix command without using scripts.can any body put some light to this requiremnt. regards Angel (4 Replies)
Discussion started by: angel12345
4 Replies

3. Shell Programming and Scripting

Appending a new field at the end in a file

can anyone tell me please ......how to append a new field at the end of a file with the help of sed or some other command in bourne shell (8 Replies)
Discussion started by: amitpta
8 Replies

4. Emergency UNIX and Linux Support

Appending the contents of two files in computational manner

Hi, I have two files say file 1 file 2 File1 1 2 4 5 File 2 asdf adf How to get the ouput something like asdf1 adf1 asdf2 adf2 asdf4 adf4 asdf5 (5 Replies)
Discussion started by: ecearund
5 Replies

5. Shell Programming and Scripting

Appending string at the end of the file

Hello, I wanted to append 'XYZ' at the end of the text file. How can i do this? I searched the forums and i am not getting what i want. Any help is highly appreciated. Thanks (2 Replies)
Discussion started by: govindts
2 Replies

6. Shell Programming and Scripting

appending date at the end of the file

I have file called xx Now i want to rename this file as xxYYYYMMDD_HHMIAM.xls Here is my code.. export DATE1=`date +%Y%m%d` mv xx xx$DATE1 This code renames as xxYYYYMMDD Now how can i append HHMIAM at the end of the file? Any help is appreciated... (3 Replies)
Discussion started by: govindts
3 Replies

7. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

8. Shell Programming and Scripting

appending a line to the end of several hundred files

I have a bunch of files named publish.php within subdirs. I need to append a line at the end of each file. I thought I could do it with find and echo like this: find . -name publish.php -exec echo "<? include('path/to/file.php'); ?>" >> '{}' \; but that appends the line to a file named {}... (2 Replies)
Discussion started by: surroscape
2 Replies

9. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies
Login or Register to Ask a Question