Kaeros
Kaeros

Reputation: 1138

GNU sed: global substitution failing

I have a file called test.csv with the following content:

T1,T2,T3,T4
10,2,3,17
10,2,5,14
10,2,2,16
15,1,17,15
12,1,9,25

I want to replace all the values 17 on the fourth column by 25. So I tried the command:

cat test.csv | sed -r 's/(([1-9]+,){3})17/\125/g'
T1,T2,T3,T4
10,2,3,17
10,2,5,14
10,2,2,16
15,1,17,15
12,1,9,25

As you can see, only the last row was modified, but not the second.

However, if I do: cat test.csv | sed -r "s/([0-9]+,[0-9]+,[0-9]+,)17/\125/" I have the output I want. Why is that?

Upvotes: 0

Views: 158

Answers (1)

Kent
Kent

Reputation: 195079

The reason your sed line didn't work is:

if you check your sed line carefully,

cat test.csv | sed -r 's/(([1-9]+,){3})17/\125/g' (your sed line)

you had [1-9] not [0-9], fix that and try again, it should work for you.

also the cat file is not required. you can do sed '...' file

Upvotes: 1

Related Questions