Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for a pattern and replace something in the same line after = and ""

I am trying to replace 10.10.10.10 with some other IP, which were passed as arguments during the executing of the script so it should update the code in my file contains the below code. (As example...)

$cat environment.ts
.....
.........
const WEB_URL = 'http://10.10.10.10';
const API_URL = 'http://20.20.20.20:8080';
.....
............
......
END OF File

If it is only IP address, then I can straight away use sed command as below just search for the IP : 10.10.10.10 and replace it with some IP like 1.1.1.1

sed -i 's/10.10.10.10/1.1.1.1/g' environment.ts

But, I can't relay on IP address since the file "filename.ts" may have different IP's updated frequently.

So I am searching for the pattern "WEB_URL" and trying to replace new IP after '=' or after "//" . I tried sed and awk and wasn't able to succeed

Expected result:

Need a command like sed or awk to replace the old IP in the above mentioned file "environment.ts" and it should replace the old IP with a new IP . (As mentioned above, old IP is not constant, it may be different so the sed or awk command which I use should search with the pattern like WEB_URL or API_URL and replace the new IP next to '=' or http//)

From:

.....
const WEB_URL = 'http://<OLD_IP1>';
const API_URL = 'http://<OLD_IP2>:8080';
.....

To:

const WEB_URL = 'http://<NEW_IP1>';
const API_URL = 'http://<NEW_IP2>:8080';
like image 273
phani Avatar asked Dec 31 '22 19:12

phani


2 Answers

The sed form that locates an occurrence and then uses alternate delimiters to avoid having to escape '/' characters can be written:

sed '/locate/s#find#replace#'

But since you want to be able to pass the IP as a variable, you need to double-quote the expression so variable expansion takes place and we will want to use {x,y} quantifiers, so we will need the -r option to enable extended regex matching, resulting in the form:

sed -r "/locate/s#find#replace#"

In your case given the IP provided in the variable ip and your desire to locate either WEB_URL or API_URL (followed by a whitespace and '=' sign) and then only replace the IP address with the one contained in $ip, you could use:

sed -r "/(WEB|API)_URL\s=/s#[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}#$ip#"

Where your

  • locate is /(WEB|API)_URL\s=/
  • find for the IP is [0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}
  • replace is $ip

Example Input File

The example file shows OTH_URL and API_URL (not followed by whitespace and an '=') that remain untouched:

$ cat environment.ts
const OTH_URL = 'http://10.10.10.10';
const WEB_URL = 'http://10.10.10.10';
const API_URL = 'http://20.20.20.20:8080';
const API_URL (can either be 'http://10.10.10.10' or 'http://10.10.10.10:8080')

Running the sed expression on your example file above would result in:

$ ip=12.12.12.12
$ sed -r "/(WEB|API)_URL\s=/s#[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}#$ip#" \
environment.ts
const OTH_URL = 'http://10.10.10.10';
const WEB_URL = 'http://12.12.12.12';
const API_URL = 'http://12.12.12.12:8080';
const API_URL (can either be 'http://10.10.10.10' or 'http://10.10.10.10:8080')

Where neither the OTH_URL line or the line where API_URL is not followed by whitespace and an '=' remain unchanged.

You will need to make separate calls to change WEB_URL and API_URL to different IP addresses, if that is wanted. That can be done by using separate sed expressions -e, example:

$ ip1=12.12.12.12
$ ip2=14.14.14.14
$ sed -r -e "/WEB_URL\s=/s#[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}#$ip1#" \
         -e "/API_URL\s=/s#[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}#$ip2#" \
         environment.ts
const OTH_URL = 'http://10.10.10.10';
const WEB_URL = 'http://12.12.12.12';
const API_URL = 'http://14.14.14.14:8080';
const API_URL (can either be 'http://10.10.10.10' or 'http://10.10.10.10:8080')

Look things over and let me know if you have questions.

like image 70
David C. Rankin Avatar answered Jan 03 '23 09:01

David C. Rankin


Easy done with powershell:

Add-Type -AssemblyName System.Collections
Add-Type -AssemblyName System.Text.RegularExpressions

[System.Collections.Generic.List[string]]$content = @()

$inputFile   = 'D:\content.txt'
$outputFile  = 'D:\content1.txt'

$regex       = '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'

$newIP1      = '1.1.1.1'
$newIP2      = '1.1.1.2'

foreach($line in [System.IO.File]::ReadLines($inputFile)) {

    if( $line -like '*API_URL*' ) {
        [void]$content.Add( ($line -replace $regex, $newIP2) )
    elseif( $line -like '*WEB_URL*' ) {
        [void]$content.Add( ($line -replace $regex, $newIP1) )            }
    else {
        [void]$content.Add( $line )
    }
}

[System.IO.File]::WriteAllLines( $outputFile, $content ) | Out-Null
like image 42
f6a4 Avatar answered Jan 03 '23 08:01

f6a4