Inserting file content into a searched pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting file content into a searched pattern
# 1  
Old 05-16-2011
Bug Inserting file content into a searched pattern

Hi,
i have to insert the content of source.txt into the searched pattern of the file second.txt.

Code:
 
$cat source.txt
One
Two
Three
.
.
 
$cat second.txt
This is second file
pattern match start here
 
pattern match end here
end of the file

so the result will be like this
Code:
 
$cat second.txt
This is second file
pattern match start here
One
Two
Three
.
.
pattern match end here
end of the file

i can able to do with help of head and tail with number count. But as the pattern position is not always in fixed line. so believe the AWK is helpful.
Thanks
# 2  
Old 05-16-2011
Quote:
i can able to do with help of head and tail with number count. But as the pattern position is not always in fixed line.
I think you could use grep with line number to fine the where the pattern starts and ends and use those variable in your head and tail command
# 3  
Old 05-16-2011
Try this:
Code:
awk '{print}/pattern/{system("cat source.txt")}' second.txt

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-25-2011
Hi ,
Is it possible to use the some variable rather than the "source.txt"(as hardcode) so that we can use it as run time variable.
i tried with -v for variable assignment but failed to do so.

Code:
read abc
awk -v var="$abc" '{print}/pattern/{system("cat $var")}' second.txt

# 5  
Old 05-25-2011
Quote:
Originally Posted by posix
Hi ,
Is it possible to use the some variable rather than the "source.txt"(as hardcode) so that we can use it as run time variable.
i tried with -v for variable assignment but failed to do so.

Code:
read abc
awk -v var="$abc" '{print}/pattern/{system("cat $var")}' second.txt

Should be something like:
Code:
read abc
awk -v var="$abc" '{print}/pattern/{system("cat " var)}' second.txt

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

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

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

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

5. Shell Programming and Scripting

Help with scripting-->Inserting a line before a pattern

Hi Guys, I have written the following script test.sh in Linux . read -p "Please enter the name of the application: " DIRTOCREATE read -p "Please enter the number of associates to be given access to svn:" COUNT for (( i=0 ; i<$COUNT ; i++ )) do read -p "Enter the associate id :"... (2 Replies)
Discussion started by: Pradeep_1990
2 Replies

6. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

7. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

8. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

9. UNIX for Dummies Questions & Answers

Formating/inserting the content

Hi All, I have a two different files with below informations. First file contains the content are as below: Second file contents are as below: Now the problem is i need to insert the respective file name and status infomration from the second file into the first file exactly next to... (7 Replies)
Discussion started by: vino_hymi
7 Replies

10. Shell Programming and Scripting

Replacing a paragraph between pattern , with the content 4m another file

hi, i wanted to put the output of file f1 into the pattern space of file f2 f1: wjwjwjwjwjwjwj //these line go in file f2 jwjwjwjwjwjjwjw wjwjwjwjjwjwjwj f2: Pattern_start __________ //these are the line to be replaced __________ Pattern_end i m... (4 Replies)
Discussion started by: go4desperado
4 Replies
Login or Register to Ask a Question