0

I have a file like this

KN1234.1
KN2345.1
KN1233.1

I want to replace the . with v so that I can get output as

KN1234v1
KN2345v1
KN1233v1

After the . it is always 1 but after KN I can also have upto 5 digits. Something like this is also possible KN14345.1 and it's output should be KN14345v1

I tried sed command

sed 's/KN\d+.1/KN\d+v1/g' file.txt

but nothing happened. How can i fix this?

Perl solution would also be appreciated.

Thanks

user3138373
  • 2,559

1 Answers1

3

This:

sed 's/KN\d+.1/KN\d+v1/g' file.txt

doesn't do anything because your sed is not likely to support \d (I think it's from Perl), so the pattern doesn't match anything. You may want to refer to this: Why does my regular expression work in X but not in Y?

It wouldn't work as you want regardless, since the \d just escapes the d in the replacement part, and + isn't special, so your numbers get replaced.

In Perl, you might want something like this:

perl -pe 's/(KN\d+)\.1/$1v1/'  file.txt 

Where $1 expands to the first group in parenthesis, and the dot is escaped, since it's regex for any character. Or

perl -pe 's/KN\d+\K\.1/v1/'  file.txt 

Where \K kills the preceding part of the match, so that it isn't replaced.

Though if you don't care about the context of the dot, use tr. (or tr/// in Perl).

ilkkachu
  • 138,973