daday001
daday001

Reputation: 101

Sed replace the first value

I want to replace the first value (in first column and line so here 1) and add one to this value, so I have a file like this

1
1 1
2 5
1 6

I use this sentence

read -r a < file
echo $aa
sed  "s/$aa/$(($aa + 1))/" file
# or 
sed  's/$aa/$(($aa + 1))/' file

But when I make that, he change all first column one into two. I have try to change the quote but it make nothing.

Upvotes: 0

Views: 57

Answers (2)

JJoao
JJoao

Reputation: 5347

perl -p0e 's/(\d+)/$1+1/e' file

Upvotes: 1

karakfa
karakfa

Reputation: 67497

restrict the script to first line only, i.e.

sed '1s/old/new/'

awk might be a better tool for this.

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

for the first line add 1 to the first field and print. Can be rewritten as

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

or

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

Upvotes: 1

Related Questions