1

I'm trying to use tr in bash to replace only the final character of a match. The string will have a substring with 5 digits followed by a dash, but I want to replace that dash with a slash.

I want to use something like this:

echo "xyzvb12345-Ab-C5678-dEf" | tr "#####-" "#####/"

To get an output like this:

xyzvb12345/Ab-C5678-dEf

Is there a way to do this with tr? Or maybe sed?

EDIT: This is not a duplicate of the many tickets out there that merely find and replace text. Please read carefully before marking as a duplicate.

Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69
  • Need more info to come up with the right regex.Is it ALWAYS at the start of the string (so just replace the first dash)? Could there be more than one match in a string? Does it always follow 6 alpha characters? etc? – Gary_W Mar 13 '19 at 15:11
  • So you want to replace the first dash with a slash? – m0skit0 Mar 13 '19 at 15:13
  • @tripleee This is definitely not a duplicate of the question that you mentioned. Please take a closer look and you will see that they are only tangentially related. – Jeff Wolski Mar 13 '19 at 15:35
  • Your example data matches the answers there exactly. If you can explain how your data is not representative - maybe [edit] your question to update it - we can find a better duplicate. – tripleee Mar 13 '19 at 15:39
  • @tripleee. I don't think it does match. The dash after 12345 should get replaced, but the dash after 5678 should not get replaced. The answers on that other question do not address this. Also, the main purpose was to try to do this with `tr`, while the secondary purpose was to use `sed` if it's actually impossible to do it with `tr` – Jeff Wolski Mar 13 '19 at 15:46
  • @tripleee. Also, the answer provided by lyang actually address my question, while none of the answers on that other question do. – Jeff Wolski Mar 13 '19 at 15:55
  • `tr` is completely the wrong tool for this. I'll add a second duplicate which emphasizes this. In the meantime, the answers on the original duplicate are specifically about replating only the firs occurrence on a line; again, how is yours different? – tripleee Mar 13 '19 at 15:57
  • Not a stellar second duplicate but it seems to be the canonical for "can I do this with `tr`"; the answer is "no." – tripleee Mar 13 '19 at 16:05
  • My question is not about replacing only the first occurrence. My question is about replacing all occurrences of exactly 5 digits followed by a dash. – Jeff Wolski Mar 13 '19 at 16:10
  • Classical regex. Use `sed`. – tripleee Mar 13 '19 at 16:44

2 Answers2

2
echo "xyzvb12345-Ab-C5678-dEf" | sed 's/\([0-9]\{5\}\)-/\1\//g'

[0-9] matches numbers

\{5\} matches five of the previous group (numers)

\(...\) set the matching group so as to be referred in replacement (as \1)

g at the end tells sed to replace all matches in the input

lnyng
  • 925
  • 6
  • 18
0
echo "xyzvb12345-Ab-C5678-dEf" | sed '0,/-/s//\//'

Thanks to this other answer .

m0skit0
  • 25,268
  • 11
  • 79
  • 127