Your code does not seem to match the logic.
The outer for loops through line numbers in output.2 (say, 1,2).
The inner for loops through port numbers (?) in portlist (say, 10,20) for each line number.
So, you essentially have something called a cartesian product here. For the data shown above, you have:
Iteration 1 => Outer: 1 - Inner: 10
Iteration 2 => Outer: 1 - Inner: 20
Iteration 3 => Outer: 2 - Inner: 10
Iteration 4 => Outer: 2 - Inner: 20
Now, assuming that you have found out a way to determine the line number, the two for loops would work as follows:
(Iteration 1) Put port no. 10 in line 1.
(Iteration 2) Put port no. 20 in line 1, thereby overwriting the port no. put in iteration (1).
(Iteration 3) Put port no. 10 in line 2.
(Iteration 4) Put port no. 20 in line 2, thereby overwriting the port no. put in iteration (3).
As you can see, this does not meet your stated goal of "replacing a specific string on a specific line number".
For this, you need a one-to-one relation between a line number and that specific string (which I assume is the port number).
Given below is a very simple file whose data shows such a relation:
$
$ cat output.2
2 222
5 555
6 666
7 777
$
So now, I want to put
- the string "222" in line no. 2,
- the string "555" in line 5,
- the string "666" in line 6 and
- the string "777" in line 7
of the file, say, "output.clean".
$
$ cat output.clean
this is line 1
Destination port matches
this is line 3
this is line 4
Destination port matches
Destination port matches
Destination port matches
this is line 8
$
$
In fact, in such a case, you do not even need to search for "Destination port matches". That's because the substitution is based on the line number and not on a string match.
An awk program can be written for the same:
$
$ awk '{if (FILENAME=="output.2"){a[$1]=$2}else{if (a[FNR]==""){print $0}else{print "Dest port match",a[FNR]}}}' output.2 output.clean
this is line 1
Dest port match 222
this is line 3
this is line 4
Dest port match 555
Dest port match 666
Dest port match 777
this is line 8
$
$
You may want to brush up some concepts here.
- FILENAME stores the name of the file being currently processed.
- FNR is the record number of the current file.
- The first if condition checks if the FILENAME is "output.2" and if so, it creates an associative array with the line number as the key and the port number as its value.
- The else part goes to the "output.clean" file.
- If the current line number of "output.clean" is not a key of the associative array (created earlier), then simply print the line.
- Otherwise, print the string you want with the hash value in it.
- Again, since the associative array must be created before "output.clean" is processed, the order of files has to be correct.
HTH,
tyler_durden