简体   繁体   English

如何从指定文件中替换IP地址

[英]how to Replace IP Address from in specified file

I have following script to for this it replaces every ip from specified file but logic is bad 我有以下脚本为此,它替换了指定文件中的每个ip,但是逻辑不好

while getopts i:h: opt
do
   case $opt in

       i)
    echo "Proposed ip will $OPTARG"
    if [[ $OPTARG =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then

    sed -i 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$/'$OPTARG'/g' sample.txt 

    echo "proposed ip is $OPTARG"
    else
    echo "fail to update"
    fi ;;

       h) ;;
  esac
done

` `

want to store all searched IP address in the array replace it how can i do this 想要将所有搜索到的IP地址存储在阵列中替换它如何执行此操作

input file is 输入文件是

ome log file entries
some log file entries
some log file entries
some log file entries
This system ip is not found
some log file entries
some log file entries
some log file entries
This system IP is 122.0.0.0
some log file entries
some log file entries
This system IP:122.0.0.0
some log file entries
some log file entries
some log file entries
Hostname:ip-172.31.18.255.ec2.internal
some log file entries
some log file entries

The following solution will allow you to store all matched IP addresses in an array and use them elsewhere in your script. 以下解决方案将允许您将所有匹配的IP地址存储在阵列中,并在脚本的其他位置使用它们。 From my observations (using your script), all of the ip addresses in your source file were being replaced except the IP address contained in the EC2 private host name. 根据我的观察(使用您的脚本),源文件中的所有IP地址都被替换了,但EC2专用主机名中包含的IP地址除外。 It's not clear if that's what you wanted or not. 尚不清楚这是否是您想要的。 Below will replace all ip addresses (including the ip contained in the EC2 host name). 下面将替换所有IP地址(包括EC2主机名中包含的IP)。 The regex I'm using was taken from the accepted answer in this post. 我正在使用的正则表达式摘自本文中的可接受答案

#!/usr/bin/env bash

#
# Directory where this script is located
#
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

declare ips=()
declare ip_regex='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
while getopts i:h: opt
do
   case $opt in

       i)
            echo "Proposed ip is $OPTARG"
            if [[ $OPTARG =~ ${regex} ]]; then
                # Store IPs in array for further processingg
                ips=$(echo $(cat "${DIR}/sample.txt") | grep -Eo ${ip_regex})

                for ip in ${ips[@]}; do
                    echo "Replacing '${ip}' with '${OPTARG}'"
                    sed -i 's/'${ip}'/'$OPTARG'/g' "${DIR}/sample.txt"
                done
            else
                echo "Failed to update"
            fi ;;

       h) ;;
  esac
done

If you don't want to replace IP addresses contained in the EC2 hostname, a regex (which I constructed from the answer to this post ) like the following should work 如果您不想替换EC2主机名中包含的IP地址,则如下所示的正则表达式(我是根据本文的答案构建

declare ip_regex='(^|[[:space:]])[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}([[:space:]]|$)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM