Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting lines from one file at a particular postion in second file.

Status
Not open for further replies.

skuthe

Programmer
Sep 10, 2002
33
US
Hi,
I have two set of files. I need to match a pattern from one file and insert the matched pattern into the second file at the same line where I found the pattern in the first file.

The pattern to be matched can be a user input.

Any help would be greatly appreciated.

Thanks in advance.

eg:
File 1
======
This is a great forum and I like it very much.
My name is skuthe and I am not an awk programmer dude.
I hope to get answers quickly.
Everybody is a good programmer here.

File 2
======
Jack
Henry
James
Tom
Rick
...
..

I need to search the work "programmer" in file 1. The word in the file 1 is found on line 2 and 4, Therefore I need to insert the whole thing on line no. 2 after the word programmer at line # 2 in file2 and line 4 at the line # 4 in file2.

After the said program execution the file2 should look as follows..

File 2
======
Jack
programmer dude
Henry
programmer here
James
Tom
Rick
...
..
 
This may not be exactly what you want but it should get you started.
BEGIN {
while ((getline < &quot;file1&quot;) > 0) {
if ($0 ~ var1) a[++ix] = 1
}
}
{
print
if (a[NR]) print var1
}
~
Run it by entering

awk -v var1=&quot;programmer&quot; -f script.awk file2 CaKiwi
 
I did it the hard way, and it doesn't quite work.
My solution may also help you, it is not very good
without a way to merge the results of each pass however
and I believe the order you wanted was different.

perhaps cakiwi or someone else could come up with a way
to merge the two methods...

#########################################

function loader(fname,ln,v, array,x,n) {
while ((getline < fname) > 0) {
array[x++] = $0
}
close(fname)


for (n in array) {
if (ln == n) {
t = array[n]
array[n] = v
bump(array,ln,x,t)
}
}
parray(array,x)
}

function bump(arra,n,max,val) {
tmp = arra[n + 1]
arra[n + 1] = val
n++
if (n < max) {
bump(arra,n,max,tmp)
}
}

function parray(arra,max, i) {
while (i <= max) {
i++
print arra
}
}

{
t = index($0,&quot;programmer&quot;)
if (t) {
print &quot;Got: &quot;, t
myrec[NR] = substr($0,t,length($0) - t)
}
}

END {
for (yy in myrec) {
print &quot;At&quot;, yy, myrec[yy]
loader(&quot;/home/mars/s2.txt&quot;,yy,myrec[yy])
}
}
##################################################
Output:
Got: 39
Got: 21

At 4 programmer here
Henry
James
Tom
programmer here
Rick

At 2 programmer dude
Henry
programmer dude
James
Tom
Rick
 
No guys, I am sorry none of your programs work.

Pl. help guys !!.
 
Please be more specific. What do they do or fail to do? CaKiwi
 
A slight variation of CaKiwi's version:

BEGIN {
while (( getline < &quot;file1&quot;) >0) {
line++
if (match($0, pat))
arr[line]=substr($0, RSTART);
}
}

{
print;
if (arr[FNR]) print arr[FNR]
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Ooops - here's how to call it:

nawk -v pat='programmer' -f a.awk file2 vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top