Reputation: 47196
#something here
port 4444
#something here
port 4444
So I have file like above , I would to find the line with word "port" and increment the number(the number can be any number - not necessary it's fixed at 4444) next to it by 1.
For example,result file should be of the form
#something here
port 4444
#something here
port 4445
Any thoughts, I think we need use sed , but not sure about incrementing it.
EDIT: Sorry again,I thought I get answer based in sed , so didn't think about positions. Here is the exact file contents
$cat file
data dev-1
type client
option type tcp
option host 99.160.131.163
option nodelay on
option port 6996
end
data dev-2
type client
option type tcp
option host 99.160.131.164
option nodelay on
option port 6996
end
I would like to change the port number and increment it.Sorry again for inconvenience.
Upvotes: 0
Views: 1377
Reputation: 67831
I would use awk
:
awk '
BEGIN{p=-1}
{ if($2=="port"){if(p==-1){p=$3}else{p++};print $1,$2,p} else {print} }' file
Upvotes: 1
Reputation: 342303
$ awk 'f && $2=="port"{ $NF=num} $2=="port"{ num=$NF+1; f=1 } 1 ' file
data dev-1
type client
option type tcp
option host 99.160.131.163
option nodelay on
option port 6996
end
data dev-2
type client
option type tcp
option host 99.160.131.164
option nodelay on
option port 6997
end
Upvotes: 2
Reputation: 10639
Perl script will be:
$ perl -pe 'if (/^(port )(\d+)/) { $_ = "$1" . ($2+$count) . "\n" if $count; $count++; }' < aaa.txt
#something here
port 4444
#something here
port 4445
port 4446
input data:
$ cat aaa.txt
#something here
port 4444
#something here
port 4444
port 4444
Upvotes: 0
Reputation: 9044
port=444
((port+=1))
echo $port
the reference The Double-Parentheses Construct
Upvotes: 1