>我有这样的文件
www IN A 192.168.10.1
webmail IN A 192.168.10.2
mail IN A 192.168.10.3
我想写一个像这样获得 3 个输入的 bash 脚本
./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) 的行,但我的脚本不起作用。
您可以在 sed 中选择该行并进行正则表达式替换。
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"
使用 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
$ 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%sn", 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
以上将可移植且健壮地工作(即,由于部分匹配或正则表达式字符或分隔符(如输入或参数中出现的/
)而没有错误匹配或其他失败的机会)在任何 UNIX 框上的任何 shell 中使用任何 awk
要写回原始文件,如果您使用的是 GNU awk,您可以在 awk 脚本的前面添加-i inplace
,否则可以在脚本末尾添加> tmp && mv tmp "$file"
。