kaiserm99
kaiserm99

Reputation: 313

Increment last number of first line in file

I want to write a shell script which can increment the last value of the first line of a certain file structure:

File-structure:

p cnf integer integer   
integer integer ... 0

For Example:

p cnf 11 9   
1 -2 0  
3 -1 5 0

To:

p cnf 11 10   
1 -2 0  
3 -1 5 0

The dots should stay the same.

Upvotes: 0

Views: 249

Answers (2)

Cyrus
Cyrus

Reputation: 88829

With GNU awk:

awk 'NR==1{$NF++} {print}' file

or

awk 'NR==1{$NF++}1' file

Output:

p cnf 11 10
1 -2 0  
3 -1 5 0

$NF contains last column.

Upvotes: 1

P....
P....

Reputation: 18411

If you could use perl:

perl -pe  's/(-*\d+)$/$1+1/e' if $. == 1' inputfile

Here (-*\d+)$ is capturing integer value(optionally negative) at the end of the line and e flag allows the execution of code before replacement, so the value increments.

Upvotes: 1

Related Questions