awk 和 sed配合为文本里面的数字加一操作

2020-05-27  本文已影响0人  Odven

文本格式

cat /root/test.txt

105 1.196.116.15
30 23.129.64.100
12 209.107.216.190
39 218.19.169.89

脚本操作

#!/bin/bash

file="/root/test.txt"

incr(){
    ip=$1
    n=$(awk -v ip=${ip} '$2==ip{print $1+1}' ${file})
    # sed -ri "/${ip}/s#[0-9]+\s+${ip}#${n} ${ip}#g" ${file}
    sed -ri "/${ip}/s#[0-9]+(\s+${ip})#${n}\1#g" ${file}
}

while read line
do
    ip=$(echo ${line} | awk '{print $2}')
    incr $ip
done < ${file}
上一篇 下一篇

猜你喜欢

热点阅读