1

I am trying to build a connection string that requires pulling 3 IP addresses from another config file. When I get those values, I need to replace the port on each. I plan to replace each port using simple Bash find and replace ${string/pattern/replacement} but my problem is I'm stuck on the best way to parse the pattern out of the IP.

Here is what I have so far:

myFile.config:

ip.1=ip-ip-1-address:1234:5678
ip.2=ip-ip-2-address:1234:5678
ip.3=ip-ip-3-address:1234:5678

Copying some other simple process, I found I can pull the value of each IP like this:

IP1=`grep "ip.1=" /path/to/conf/myFile.config  | awk -F "=" '{print $2}'`

which gives me ip.1=ip-ip-1-address:1234:5678. However, I need to replace 1234:5678 with 6543 for example. I've been looking around and I found this awesome answer that detailed using Bash prefix substitution but that relies on knowing the parameter. for example, I would have to do it this way:

test=${ip1##ip-ip-1-address:}

which results in $test being 1234:5678. That's fine but maybe I don't know the IP address as the parameter, so I'm back to considering regex unless there's a way for me to use * as the parameter or something, but I have been unsuccessful so far. For regex, I have tried a bunch such as test=${ip1/(?<=:).*/}.

4
  • Maybe sed -i 's/:.*/:6543/' myFile.config? It will replace all text starting with the first : on each line of the file with :6543. Commented Jul 29, 2020 at 19:30
  • Can you elaborate @WiktorStribiżew. Is this instead of my grep? Commented Jul 29, 2020 at 19:32
  • Yes, instead of grep, since you say you need to replace. grep is used to extract. Commented Jul 29, 2020 at 19:33
  • 1
    while IFS="=:" read -r var ip n1 n2; do echo "$var $ip $n1 $n2"; done < file? Commented Jul 29, 2020 at 19:54

1 Answer 1

1

Note that ${ip1/(?<=:).*/} you tried is an example of string manipulation syntax that does not support regex, only specific patterns.

You seem to want

x='ip.1=ip-ip-1-address:1234:5678'
echo "${x%%:*}:6543" # => ip.1=ip-ip-1-address:6543

The ${x%%:*} takes the value of x and removes all chars from the end till the first : including it. :6543 is added to the result of this manipulation using "${x%%:*}:6543".

To extract that value, you may also use

awk '/^ip\.1=/{sub("^[^:]+:", "");print}' myFile.config

The awk command finds lines starting with ip.1= and then removes all text from the start till the first colon including the colon and only prints these values.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed answer but I am not looking to modify the input file. I need to take values from it and then replace the port for another dependency, hence my attempt at grep and replace in my new variable.
@johnny_mac You may use sed to modify strings, too. ip="$(sed 's/:.*/:6543' file)". I re-wrote the answer with a string manipulation example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.