How to insert file contents after nth occurrence of a string using sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert file contents after nth occurrence of a string using sed?
# 1  
Old 08-20-2014
How to insert file contents after nth occurrence of a string using sed?

Hi,
I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g.

Code:
> cat file1
banana
apple
orange
apple
banana
pear
tangerine
apple 

> cat file2
I don't like apples

What would be the sed command to insert the contents of file2 after the second occurrence of apple in file1? i.e the output would be:

Code:
banana
apple
orange
apple
I don't like apples
banana
pear
tangerine
apple

Thanks in advance..

Moderator's Comments:
Mod Comment Please use code tags, thanks.

Last edited by zaxxon; 08-20-2014 at 02:17 AM.. Reason: code tags
# 2  
Old 08-20-2014
You can try with like this also . I am not using
Code:
sed

here.

Code:
awk -v place=`awk '$1~/apple/ {a=a+1;if(a==2){b=NR}} END {print b} ' file1` '{ if(FILENAME == "file1" && NR <=place) { print }; if(FILENAME == "file2") { print }}' file1 file2 > outputfile
awk -v place=`awk '$1~/apple/ {a=a+1;if(a==2){b=NR}} END {print b} ' file1` '{ if(FILENAME == "file1" && NR >place) { print }}' file1 >> outputfile

.

output is like this with number of occurance 2 .
Code:
cat outputfile
banana
apple
orange
apple
I don't like apples
banana
pear
tangerine
apple

This User Gave Thanks to sayami00 For This Post:
# 3  
Old 08-20-2014
Try
Code:
sed '1,/apple/{p;d}; /apple/,$ {1,1 r file2
}' file1
banana
apple
orange
apple
I don't like apples
banana
pear
tangerine
apple

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-20-2014
Code:
[akshay@nio tmp]$ cat file1.txt
banana
apple
orange
apple
banana
pear
tangerine
apple

Code:
[akshay@nio tmp]$ cat file2.txt
I don't like apples

Code:
[akshay@nio tmp]$ awk '$0 ~ pattern{++c}1 ; c==occurence && !f{f=1; while(getline < content){ print }close(content) }' pattern='apple' occurence='2' content='file2.txt'  file1.txt

Resulting
Code:
banana
apple
orange
apple
I don't like apples
banana
pear
tangerine
apple

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 08-20-2014
Thank you all so very much!
# 6  
Old 08-27-2014
Thank you all for your replies; much appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

2. Shell Programming and Scripting

Sed: how to use file contents in replacement string

I want to replace a string by contents of file. I am trying the following sed command: cat sample | sed "s^<enter description here>^`cat details`^" But it is not working. a=`cat details` and using $a will not help since it will affect the whitespaces. What am I missing in the above sed... (5 Replies)
Discussion started by: anand_bh
5 Replies

3. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

8. Shell Programming and Scripting

Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting. I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the... (7 Replies)
Discussion started by: imr
7 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. Shell Programming and Scripting

SED To insert Directory Contents to file

I am on a HP-UX machine I have a directory called "/u01/blobs" and the files look like this: ls -1 7398 7399 7400 I need to produce a comma delimited file with the following format: filename,location/filename i.e: 7398,/u01/blobs/7398 7399,/u01/blobs/7399 7400,/u01/blobs/7400 What... (3 Replies)
Discussion started by: NomDeGuerre
3 Replies
Login or Register to Ask a Question