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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert content of file before the first occurrence of a line starts with a pattern in another file
# 8  
Old 10-03-2016
@disedorgue. interesting trick with the GNU e-modifier..
I think you need 0,/^unix/.... otherwise it seems to insert twice in case the first line starts with the pattern...
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 10-03-2016
Hey all, thanks for you support.. What if i want the same action after 'n'th occurrence ?

Can you please help me
# 10  
Old 10-03-2016
Quote:
Originally Posted by Jagadeesh Kumar
Hey all, thanks for you support.. What if i want the same action after 'n'th occurrence ?
Can you please help me
Hello Jagadeesh Kumar,

Let's say if you want to enter the text from Input_file2 after 5th occurrence so you could try following.
Code:
awk '($1=="unix" && ++q==6){system("cat Input_file2")} 1'  Input_file1
OR
awk '{$1=="unix"?++A[$1]:A[$1]} A[$1]==6{system("cat Input_file2")} 1' Input_file1

So point to not here is in above codes my condition checks for ==6though we want to insert after 5th occurrence, so whenever you want after nth occurrence to print statements then please put n+1 in condition as simple as that, I hope this helps you.

EDIT: If you want to print contents of Input_file2 on exact occurrence(let's say 5th in this example here) of string then following may help you in same too.
Code:
awk '{$1=="unix"?++A[$1]:A[$1]} A[$1]==5{print;system("cat Input_file2");next} 1' Input_file1
OR
awk '($1=="unix" && ++q==5){print;system("cat  Input_file2");next} 1'  Input_file1

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-03-2016 at 04:13 AM.. Reason: Added one more solution on same now.
# 11  
Old 10-03-2016
Quote:
Originally Posted by Scrutinizer
@disedorgue. interesting trick with the GNU e-modifier..
I think you need 0,/^unix/.... otherwise it seems to insert twice in case the first line starts with the pattern...
Thanks for notice, I corrected in post.

Otherwise, always with gnu sed for n'th' occurence ( 3rd occurence in example) :
Code:
sed -e '/^unix/{x;s/^/1/;/^1\{3\}$/!bno;x;s/^unix.*/cat f2.txt;echo "&"/e;b;:no;x;}' f1.txt

Regards.
# 12  
Old 10-05-2016
Linux

Hi All,

Apologies for repeated questioning on the same.. I am very much new to this scripting. Please tell me some simple command using sed or awk (without using system) to achieve the task..

Thanks all in advance.
# 13  
Old 10-05-2016
How far did you get with the proposals given?
# 14  
Old 10-05-2016
Actually i need to execute this task on a a device which taken this input inside system command.. I am not able achieve my task with the given inputs..

example:

Code:
command = sed -e '/^unix/{x;s/^/1/;/^1\{3\}$/!bno;x;s/^unix.*/cat f2.txt;echo "&"/e;b;:no;x;}' f1.txt

I need to execute like system("command")

Means system will execute the command like a shell command..

---------- Post updated at 04:00 PM ---------- Previous update was at 04:00 PM ----------

Actually i need to execute this task on a a device which taken this input inside system command.. I am not able achieve my task with the given inputs..

example:

Code:
command = sed -e '/^unix/{x;s/^/1/;/^1\{3\}$/!bno;x;s/^unix.*/cat f2.txt;echo "&"/e;b;:no;x;}' f1.txt

I need to execute like system("command")

Means system will execute the command like a shell command..




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by rbatte1; 10-05-2016 at 08:34 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies

2. Shell Programming and Scripting

Insert content of a file right after pattern in another file

suppose i have original file: original.txt: hello how are you you are wonderful what time is it I went to the store last night. and some apple juice then i have another file: anotherfile.txt: with my friends mary, john and harry. We had a great time. We bought food Suppose... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

4. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

5. Shell Programming and Scripting

Insert content of a file into another file before given pattern

I need to insert file x2 into x1 right before first BBB line. $ cat x1 AAA 1 AAA 2 AAA 3 BBB 1 BBB 2 BBB 3 $ cat x2 XXX - insert 1 XXX - insert 2 I need to get AAA 1 AAA 2 AAA 3 XXX - insert 1 XXX - insert 2 BBB 1 (2 Replies)
Discussion started by: migurus
2 Replies

6. Shell Programming and Scripting

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. > 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... (5 Replies)
Discussion started by: dimocn
5 Replies

7. 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

8. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

9. Shell Programming and Scripting

Insert content of a file after a certain line in another file

Hi, it's my first post to this forum. I just started bash and I'm stuck at one issue. I want to include content of a file in another file after a certain line. I'm using sed for inserting one line but how to insert all content of a file ? For example i have a file list.txt with a few lines and... (4 Replies)
Discussion started by: ktm
4 Replies
Login or Register to Ask a Question