My goal is to make a script to find/replace the variable "PORT" with a unique number.
Like the following
<VirtualHost 174.120.36.236:PORT>
ServerName architect.com.ph
ServerAlias www.architect.com.ph
DocumentRoot /home/architec/public_html
ServerAdmin webmaster@architect.com.ph
UseCanonicalName Off
CustomLog /usr/local/apache/domlogs/architect.com.ph combined
CustomLog /usr/local/apache/domlogs/architect.com.ph-bytes_log "%{%s}t %I .\n%{%s}t %O ."
## User architec # Needed for Cpanel::ApacheConf
<IfModule mod_suphp.c>
suPHP_UserGroup architec architec
</IfModule>
<IfModule !mod_disable_suexec.c>
SuexecUserGroup architec architec
</IfModule>
ScriptAlias /cgi-bin/ /home/architec/public_html/cgi-bin/
# To customize this VirtualHost use an include file at the following location
# Include "/usr/local/apache/conf/userdata/std/2/architec/architect.com.ph/*.conf"
</VirtualHost>
backend architect.com.ph {
set backend.host = "architect.com.ph";
set backend.port = "PORT";
}
The problem is that inside this file there are hundreds of virtualhosts, and other config files.
I am wondering if sed can go through a pattern of every 2 instances it finds of "PORT" to be replaced with a unique number, or better yet sequential, is this possible?
That is why I wanted it in a pattern, for every 2 instances it finds the word "PORT" to replace it with a unique number, and every 2 instances it finds to replace it with a unique number, and so forth.
Pretend this file has thousands of instances that say PORT
I want it to replace every two instances it finds that say PORT with a unique number. None are the same so incremental is fine. But every time it find two instances of PORT to replace those two, then move on to the next two, and so forth.
Is such possible?
---------- Post updated at 02:36 PM ---------- Previous update was at 02:36 PM ----------
Pretend this file has thousands of instances that say PORT
I want it to replace every two instances it finds that say PORT with a unique number. None are the same so incremental is fine. But every time it find two instances of PORT to replace those two, then move on to the next two, and so forth.
This should work in Bourne and BASH shells.
Increase the value 1500 to reflect the number of lines to be changed in your file, in my example's case it could have been set to 1011.
NR won't work in this case, though. NR will increment for every line regardless of whether a PORT substituion occured. The "substitution" counter needs to be something independent of NR (j in my version).
On an unrelated note, since gsub returns the number of substitutions made, we can further reduce the code to:
awk 'gsub(/PORT/,i){i+=!(++j%n)}1' i=1 n=2 file
Like the earlier versions of this approach, it treats multiple substitutions on a single line as one. That's probably a bug. Though if we can be certain that only one instance of PORT will occur on a line, then it's fine, and as a bonus we can reduce by one more character:
awk 'sub(/PORT/,i){i+=!(++j%n)}1' i=1 n=2 file
And that concludes this episode of OCD AWKing. Thank you for tuning in.