[Bash]Attempting to Merge text from one file into another file at the line directly under a word

Hello,
This is my first post on the forums. So I want to start by thanking anyone who is kind enough to read this post and offer advise. I hope to be an active contributor now that I've found these forums.

I have an issue that I figure would be a good first post..

I have 2 text files generated once a day.. in each is a list of successful file transfers and the 2nd file has a list of unsuccessful file transfers...

I have a 3rd file I call report.txt , in this report.txt on the first line i have " List of successful transfers:" on the 3rd line I have " List of unsuccessful transfers: " ...

I want to input the contents of file1 under the line that reads " List of successful transfers:" and I want to import the contents of file2 under the line that reads " List of unsuccessful transfers:" ... since the amount of files that succeed or fail may change daily.. I cannot just use a sed statement to insert at a specific line number.. I figure If I can find out how to insert the contents on the line directly under the line containing the word "successful" and again under the line that contains the word "unsuccessful" this would do the trick.. but not sure how to do this...

Any suggestions?

You can do something like :

# >report.txt cat <<!
   List of successful transfers:
 
   `cat first_file`
 
    List of unsuccessful transfers:
 
     `cat second_file`
 
   !
1 Like

Alternatively:

$ cat transrep
List of successful transfers:

List of unsuccessful transfers:
$ cat succtrans
bcde
fghi
jklmn
$ cat failtrans
zyxw
vuts
rqpon
$ sed -e '/List of successful transfers:/r succtrans' -e '/List of unsuccessful transfers:/r failtrans' transrep
List of successful transfers:
bcde
fghi
jklmn

List of unsuccessful transfers:
zyxw
vuts
rqpon
1 Like

Check out this too :wink:

echo "Sucess files" > test3;cat Sucess.txt >> test3;echo "Failures files" >>test3;cat Failures.txt>>test3

Thanks,
Kuldeep

1 Like

All of these options are great!
Thanks so much.

I have it working now using the cat >> method.

I appreciate all the help.

-B

In bash/ksh93 one can also do this:

cat <(echo successful trans:) succtrans <(echo failed trans:) failtrans > test3

where succtrans and failtrans are the two input files..