2014-01-27 34 views
0

我想實現增量計數器使用sed我可能是錯的,但它可能與sedsed增量計數器沒有任何循環程序

file.txt的包含以下

127.0.0.1

我想運行file.txt的sed所以每次它,如果我運行在同一個文件結果的sed與i++所以下一次增量數時間會127.0.0.2等和上。

我不想使用任何類型的while循環等。只需使用sedawk是否有可能?

+0

我相信我已經搞清楚了'sed's/127.0.0.1/127.0.0。'$((i ++))'/ g'文件。 txt' – Satish

+0

它不起作用。想想'127.0.0.4556' @Satish – Kent

+0

@kent我只需要10次:) – Satish

回答

2
$ cat file 
127.0.0.1 
$ awk 'BEGIN{FS=OFS="."} {$NF=$NF+1} 1' file 
127.0.0.2 
2

你可以選擇具有IP地址的處理模塊的語言,例如:

$ cat file.txt 
192.168.1.254 
$ ruby -ripaddr -i -lne 'puts IPAddr.new($_).succ' file.txt 
$ cat file.txt 
192.168.1.255 
$ ruby -ripaddr -i -lne 'puts IPAddr.new($_).succ' file.txt 
$ cat file.txt 
192.168.2.0 
1

使用GNU的sed:

sed -i -r 's/(.*)([^.]+)$/echo \1$((\2+1))/e' file 

下面是測試結果:

$cat file 
127.0.0.1 

$ sed -i -r 's/(.*)([^.]+)$/echo \1$((\2+1))/e' file 

$ cat file 
127.0.0.2 

$ sed -i -r 's/(.*)([^.]+)$/echo \1$((\2+1))/e' file 

$ cat file 
127.0.0.3 
0
sed 's/9$/10/;t 
h;y/12345678/23456789/;G;s/.*\(.\)\n\(.*\)./\2\1/' 

限於.1 - > .10,如定義在評論