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
# 1  
Old 09-29-2016
Linux 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
======
Code:
working on linux
its unix world
working on unix
its linux world
unix is best          --> first occurrence of line starts with unix
linux is easy
unix is easy
unix is best
linux is best

file2
====
Code:
Here is my content
i want to insert this

output
=====
Code:
working on linux
its unix world
working on unix
its linux world
Here is my content
i want to insert this
unix is best      -->  first occurrence of line starts with unix
linux is easy
unix is easy
unix is best
linux is best

Can you please suggest me one liner shell command to get this output..

Really appreciate your help...
# 2  
Old 09-29-2016
Try
Code:
awk '/^unix/ {while (0 < getline X < FI) print X} 1' FI="file2" file1
working on linux
its unix world
working on unix
its linux world
Here is my content
i want to insert this
unix is best          --> first occurrence of line starts with unix
linux is easy
unix is easy
unix is best
linux is best

# 3  
Old 09-29-2016
Hello Jagadeesh kumar,

Could you please try following and let me know if this helps you.
Code:
awk '{$1=="unix"?++A[$1]:A[$1]} A[$1]==1{system("cat Input_file2")} 1' Input_file1

Output will be as follows.
Code:
working on linux
its unix world
working on unix
its linux world
Here is my content
i want to insert this
unix is best
linux is easy
unix is easy
unix is best
linux is best

EDIT: Following may help you in same too, a little different from previous one(not having array here).
Code:
awk '($1=="unix" && ++q==1){system("cat  Input_file2")} 1'  Input_file1

Output will be as follows.
Code:
working on linux
its unix world
working on unix
its linux world
Here is my content
i want to insert this
unix is best
linux is easy
unix is easy
unix is best
linux is best

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-29-2016 at 09:35 AM..
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 09-29-2016
R. Singh's answer is great.
You can also write it this way :
Code:
awk '$1=="unix" && !i {i++; system("cat  Input_file2")}1'  Input_file1

Comments :
I don't support the ++ operator in the pattern section of the script . This is why i++ appears in the action section ( i.e between {} )
Parenthesis are useless in the first pattern section.

Last edited by blastit.fr; 09-29-2016 at 07:13 PM..
# 5  
Old 09-29-2016
Another approach is to use ed (I learnt it from Don C on this forum, thanks!)

Let's say I need to insert content of file f2 into file f1 before line starting with 3.

Code:
 
$ cat f1
1
2
3
$ cat f2
INSERT LINE
$ ed -s f1 <<-EOF > f3
> H
> /^3/-1r f2
> ,p
> Q
> EOF

This script says go to the beginning of the file (H),
then find first line starting with 3 (/^3)
then step one line up (-1)
then read in content of f2 (r f2)
then print out whole new content with (p), then quit (Q)

Result

Code:
 
$ cat f3
1
2
INSERT LINE
3

# 6  
Old 09-29-2016
Quote:
Originally Posted by migurus
Another approach is to use ed (I learnt it from Don C on this forum, thanks!)

Let's say I need to insert content of file f2 into file f1 before line starting with 3.

Code:
 
$ cat f1
1
2
3
$ cat f2
INSERT LINE
$ ed -s f1 <<-EOF > f3
> H
> /^3/-1r f2
> ,p
> Q
> EOF

This script says go to the beginning of the file (H),
then find first line starting with 3 (/^3)
then step one line up (-1)
then read in content of f2 (r f2)
then print out whole new content with (p), then quit (Q)

Result

Code:
 
$ cat f3
1
2
INSERT LINE
3

Almost...
The H command in ed doesn't move to the beginning of the file, it tells ed that if any of the following commands fail, ed should print a textual diagnostic message (or Help message) explaining what went wrong instead of just printing a question mark.

And, if I understand the requirements correctly, instead of printing results to the terminal, we want to update the file we are editing in place. So, in this case we would need something more like:
Code:
ed -s file1 <<EOF
	H
	/^unix/-1r file2
	w
	q
EOF

Where ed -s file1 uses ed to open a file named file1 without printing the size of the file when we open it and without printing the size of the file again when the script writes the updated file and <<EOF says that the following lines until we find a line that contains only EOF is to be treated as commands to be read and execute by ed. And the commands given to ed are:
  • H to turn on verbose help messages,
  • /^unix/-1r file2 to search for the first line starting with the string unix, move up one line from there, and read the contents of the file named file2 into the editing buffer after that line,
  • w to write the updated contents of the editing buffer back to the file named on the command line, and
  • q to cleanly quit. The q command can be skipped here and get the same results; I just like to tell ed to quit instead of having it hit EOF on the here-document when it is looking for another command to execute.
The things that could go wrong in this script include:
  1. not finding a line that starts with the string unix, and
  2. not finding a file named file2.
Getting a message likescript, line 2: no match or script, line 2: cannot open input file instead of just a ? helps the user of your script figure out what went wrong. That is why I give ed the H command (even though I know my script will work perfectly Smilie ).
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 10-02-2016
Hi,
Another way with gnu sed:
Code:
$ cat f1.txt 
working on linux
its unix world
working on unix
its linux world
unix is best          --> first occurrence of line starts with unix
linux is easy
unix is easy
unix is best
linux is best

Code:
$ cat f2.txt 
Here is my content
i want to insert this

Code:
$ sed -e '0,/^unix/{/^unix.*/s//cat f2.txt;echo "&"/e;}' f1.txt
working on linux
its unix world
working on unix
its linux world
Here is my content
i want to insert this
unix is best          --> first occurrence of line starts with unix
linux is easy
unix is easy
unix is best
linux is best

Regards.

Last edited by disedorgue; 10-03-2016 at 05:36 AM.. Reason: to correct error noticed by scrutinizer
This User Gave Thanks to disedorgue For This Post:
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