From 31b7a0a6e3e9dd29010004e31e42ca11b9ba1688 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 10 Feb 2014 14:30:00 -0800 Subject: [PATCH] Created SC2020 (markdown) --- SC2020.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 SC2020.md diff --git a/SC2020.md b/SC2020.md new file mode 100644 index 0000000..fb106f6 --- /dev/null +++ b/SC2020.md @@ -0,0 +1,21 @@ +# tr replaces sets of chars, not words (mentioned due to duplicates). + +### Problematic code: + + echo 'hello world' | tr 'hello' 'goodbye' + +### Correct code: + + echo 'hello world' | sed -e 's/hello/goodbye' + +### Rationale: + +`tr` is for `tr`ansliteration, turning some characters into other characters. It doesn't match strings or words, only individual characters. + +In this case, it transliterates h->g, e->o, l->d, o->y, resulting in the string "goddb wbrdd" instead of "goodbye world". + +The solution is to use a tool that does string search and replace, such as sed. + +### Contraindications + +None. \ No newline at end of file