如何在一行中找到ip并将其替换为bash脚本?

时间:2019-01-27 10:23:45

标签: bash shell awk sed

我有一个像这样的文件

www       IN       A       192.168.10.1
webmail   IN       A       192.168.10.2
mail      IN       A       192.168.10.3

我想编写一个bash脚本,这样可以获得3个输入

./script.sh network.com www 192.168.10.10

脚本是

project=$1
server=$2
ip=$3
h=$(awk -F "$server       IN       A" '{print $2}' /home/forward.$project)
sed -i "s/$h/$ip/"

我想找到以第二个输入开头的行,并用第三个输入替换(ip),但是我的脚本不起作用。

3 个答案:

答案 0 :(得分:3)

您可以选择sed中的行并进行regexp替换。

project=$1
server=${2//./\\.}  # escape '.' to avoid problems with sed if $server contains some
ip=$3
sed  -E "/^$server /s/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/$ip/" "$1"

答案 1 :(得分:2)

使用awk:

$ echo network.com www 192.168.10.10 | 
awk '
NR==FNR {
    a=$2   # store hostname
    b=$3   # and ip
    next   # .
}
$1==a {    # if hostname matches
    sub(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,b)  # replace ip looking string
}1' - a_file                                                 # output

输出:

www       IN       A       192.168.10.10
webmail   IN       A       192.168.10.2
mail      IN       A       192.168.10.3

修改

将不匹配的记录添加到末尾的版本:

$ echo network.com www2 192.168.10.10 |
awk '
NR==FNR {
    a=$2           # store hostname
    b=$3           # and ip
    next           # not needed for this input but good practise
} 
FNR==1 { t=$0 }    # store a template record for later use
$1==a {            # if hostname matches
    sub(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,b)  # replace ip looking string
    f=1            # flag up when there was replace
}
1;                 # output
END {              # in the end 
    if(!f) {       # if there was no replace
        sub(/^[^ \t]+/,a,t)  # replace the template
        sub(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/,b,t)
    print t        # and output it
    }
}' - a_file

输出:

www       IN       A       192.168.10.1
webmail   IN       A       192.168.10.2
mail      IN       A       192.168.10.3
www2       IN       A       192.168.10.10

答案 2 :(得分:2)

$ cat script.sh
#!/bin/env bash
project=$1
server=$2
ip=$3
file="file"     # change to "/home/forward.$project"

awk -v server="$server" -v ip="$ip" '
    $1 == server {
        sub(/[^[:space:]]+[[:space:]]*$/,"")
        $0 = $0 ip
        found = 1
    }
    { print; lastLine=$0 }
    END {
        if ( !found ) {
            match(lastLine,/^[^[:space:]]+[[:space:]]+/)
            gsub(/^[^[:space:]]+[[:space:]]+|[^[:space:]]+[[:space:]]*$/,"",lastLine)
            printf "%-*s %s%s\n", RLENGTH-1, server, lastLine, ip
        }
    }
' "$file"

$ ./script.sh network.com www 192.168.10.10
www       IN       A       192.168.10.10
webmail   IN       A       192.168.10.2
mail      IN       A       192.168.10.3

$ ./script.sh network.com fluffy 192.168.10.10
www       IN       A       192.168.10.1
webmail   IN       A       192.168.10.2
mail      IN       A       192.168.10.3
fluffy    IN       A       192.168.10.10

$ ./script.sh network.com super_long_server 192.168.10.10
www       IN       A       192.168.10.1
webmail   IN       A       192.168.10.2
mail      IN       A       192.168.10.3
super_long_server IN       A       192.168.10.10

以上内容可在任何外壳上的任何shell中使用任何awk进行移植,并且功能强大(例如,由于部分匹配或正则表达式字符或定界符(例如/出现在输入或参数中)而不会导致错误匹配或其他失败) UNIX框

要写回原始文件,如果使用的是GNU awk,则可以在awk脚本的前面添加-i inplace,否则可以在其末尾添加> tmp && mv tmp "$file"