views:

55

answers:

4
 #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.

+1  A: 
port=444
((port+=1))
echo $port

the reference The Double-Parentheses Construct

Dyno Fu
Also in BSD/OSX /bin/sh
Lachlan Roche
Also: `((port++))`, but your answer doesn't address the OP's question.
Dennis Williamson
the question has been edited...
Dyno Fu
+1  A: 

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
mouviciel
please see my edit comments above.thanks
lakshmipathi
I edited my anwser accordingly.
mouviciel
+2  A: 
$ 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
ghostdog74
please see my edit comments above.thanks
lakshmipathi
thanks it's worked well. Thank you all
lakshmipathi
A: 

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
dma_k
please see my edit comments above.thanks
lakshmipathi