Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone,

the problem is quite simple, yet I can't find an easy solution using awk.

I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file.

My problem is in the copying of the lines... Finding and modifying is alright (I read the string in a second file and substitute it by the corresponding string).

If you could give me an algorithm that I could write in awk, that would be great.

Thank you very much.

Teroc.

Provide us with the Input file and the output your expecting, so it would be easy to work on.

see here for example.

I doesn't work GhostDog, well it probably does, but not in my case.
I did something like that:

NR==FNR {a[$2]=$1;next} #I read a first file to get the search pattern
{if ($3 in a)
    {$3=a[$3]
    print
    d=9
    next}
    d--&&d>=0
}

But it does only print the first line.

Here is an example of my files.
The first file where I read the values looks like this:

a1 b1
a2 b2
a3 b3

Then the second file looks like: (there is also other stuff in the file not related)

....
/begin MEASUREMENT b1
   "comment" 
   UWORD 
   ident 
   1 
   100
   0 
   65535 
   ECU_ADDRESS 0x4000582C
  /end MEASUREMENT 
....

I want to check if $3==b1, then replace it with a1 and copy the others lines at the end of the file, and do it for each bi of the first file.

thanks for your help.

---------- Post updated at 08:53 AM ---------- Previous update was at 08:42 AM ----------

Found a solution doing this:

NR==FNR {a[$2]=$1;next}
{if ($3 in a)
    {$3=a[$3]
    print
    d=9
    while (d>0)
    {getline; print; d--}
    }
}

of course. Its just a similar example and it will NOT solve your problem exactly. The rest is up to you to understand, change and modify. Anyway, its good that you already found your solution. Lastly, provide examples of your input, and describe your desired output next time when you post a problem.

I have another problem related to those files.

Sometimes, in file1, it is possible that one value in column 1 is associated to 2 or more values like this:

a1 b1 c1
a2 b2
a3 b3 c3 d4

Now, when I need to replace and copy as described in my other post, I need to do this for each value associated with a1.
The problem is, I tried with multidimensional arrays and other stuffs, but it doesn't write the full 9 lines for the 2nd value (c1 or c3).

Is there a way to jump back 10 lines up and retest the same line so I can process it again for the 2nd and 3rd value? I've searched since last post and can't find somethinbg working...

Thanks again and again !

Here is the code I use ...

NR==FNR {for(i=2;i<=NF;i+=1) a[$1]=$i" "a[$1];next}
NR!=FNR {print $0}

{if ($3 in a && $2=="MEASUREMENT")
	{temp=$3;
	split(a[$3],separate)
	for (x in separate)
	{$3=separate[x];
	buffer[j]=$0;
	$3=temp;
	d=9;
	while (d>0) #not printing for the second value of separate because of this while I guess, once it is done once, it wont do it again for the other values
	{getline; buffer[j]=buffer[j]"\n"$0; d--}  #I use a buffer so that I can write the thing where I want
	}j++
	}
}

And here is the ouput I have

...
/begin MEASUREMENT b1  #As intended
   "comment" 
   SWORD 
   ratio_0p1 
   1 
   100
   -2000.0 
   2000.0 
   ECU_ADDRESS 0x500040B4
  /end MEASUREMENT 
/end MEASUREMENT c1 #writes c1 here instead of the whole thing