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

  • Hi, I was thinking something else. This would do sed 's/\./v/g file.txt' Thanks for pointing out – user3138373 Oct 4 '16 at 18:41
  • I will look for tr command manual. Thanks for pointing it out. – user3138373 Oct 4 '16 at 18:45
  • sed 's/\./v/' file.txt – Satō Katsura Oct 4 '16 at 19:39
2

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.