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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Bash]Attempting to Merge text from one file into another file at the line directly under a word
# 1  
Old 05-23-2010
[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?
# 2  
Old 05-23-2010
You can do something like :

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

This User Gave Thanks to Reboot For This Post:
# 3  
Old 05-23-2010
Alternatively:
Code:
$ cat transrep
List of successful transfers:

List of unsuccessful transfers:

Code:
$ cat succtrans
bcde
fghi
jklmn

Code:
$ cat failtrans
zyxw
vuts
rqpon

Code:
$ sed -e '/List of successful transfers:/r succtrans' -e '/List of unsuccessful transfers:/r failtrans' transrep

Code:
List of successful transfers:
bcde
fghi
jklmn

List of unsuccessful transfers:
zyxw
vuts
rqpon

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-23-2010
Check out this too Smilie

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

Thanks,
Kuldeep
This User Gave Thanks to KuldeepSinghTCS For This Post:
# 5  
Old 05-23-2010
awsome thanks!

All of these options are great!
Thanks so much.

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

I appreciate all the help.

-B
# 6  
Old 05-23-2010
In bash/ksh93 one can also do this:
Code:
cat <(echo successful trans:) succtrans <(echo failed trans:) failtrans > test3

where succtrans and failtrans are the two input files..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

"Content-type: text/html No input file specified." Only in CRON but not when executed directly

Hi gang, I have the following code inside a the file script.sh #!/bin/bash todaysdate=$(date --date='7 day' +'%d') todaysmonth=$(date +'%m') todaysyear=$(date +'%Y') yahoodatestring=$todaysyear$todaysmonth$todaysdate nicedate=$(date --date='5 day' +'%A') nice="$nicedate,... (2 Replies)
Discussion started by: phpchick
2 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. UNIX Desktop Questions & Answers

Merge Last Word to Another File

Hi, Im a newbie to scripting. I have a file that looks like the one below. How can i change the last "/" to become a space. Thank You so Much for the help. :) hostname date Feb-9 /u100/DEVCO/Patching/a.log hostname date Jun-25 /u100/DEVCO/DumpCleaner/a.log hostname date Jun-25... (2 Replies)
Discussion started by: lienyca
2 Replies

4. Shell Programming and Scripting

Grep to isolate a text file line and Awk to select a word?

I am looking at using grep to locate the line in the text file and them use awk to select a word or words out of it. I know awk needs -v to allow a variable to be used, but also needs -F to allow the break up of the sentence and allow the location of separate variables. $line = grep "1:" File |... (8 Replies)
Discussion started by: Ironguru
8 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 Replies

7. Shell Programming and Scripting

Help need to cut the first word of a line in text file

Hi All, I would like help with a script which can get rid of the first work of all lines in text file. File 1 The name is Scott. Output : name is Scott ---------- Post updated at 02:38 PM ---------- Previous update was at 02:37 PM ---------- Hi ALL There is typo error in... (3 Replies)
Discussion started by: bubbly
3 Replies

8. Shell Programming and Scripting

BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)

I have a file that lists data about a system. It has a part that can look like: the errors I'm looking for with other errors: Alerts Password Incorrect Login Error Another Error Another Error 2 Other Info or, just the errors I need to parse for: Alerts Password Incorrect ... (9 Replies)
Discussion started by: elinenbe
9 Replies

9. Shell Programming and Scripting

How to merge multiline into line begining with specific word

Hi, The file format is like the following. timestamp=2008-02-28-23.50.29.550675;category=CONTEXT;audit event=CONNECT; event correlator=2; database=CURDOMS;userid=inst3;authid=INST3; origin node=0;coordinator node=0; application id=AC122081.FA97.054468155029;application... (2 Replies)
Discussion started by: missyou
2 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question