2

AFAIC both sed and awk are general purpose text processing utilities with whom a user can get quite similar results, in a slightly different syntax:

With both, a user could add, replace/translate and delete content in a file.

What is the main difference between these two general purpose text processing utilities and the tr text processing utility?

I assume that tr's functionality is included in both sed and awk so it is just narrowed to the specific context of replacing one string in another, but I'm not sure I'm accurate here or translating it to another.

  • they ARE different, by no accident – RomanPerekhrest Mar 3 '18 at 21:28
4

Yes, tr is a "simple" tool compared to awk and sed, and both awk and sed can easily mimic most of its basic behaviour, but neither of sed or awk has "tr built in" in the sense that there is some single thing in them that exactly does all the things that tr does.

tr works on characters, not strings, and converts characters from one set to characters in another (as in tr 'A-Z' 'a-z' to lowercase input). Its name is short from "translate" or "transliterate".

It does have some nice features, like being able to delete multiple consecutive characters that are the same, which may be a bit fiddly to implement with a single sed expression.

For example,

tr -s '\n'

will squeeze all consecutive newlines from the input into single newlines.

To characterize the three tools crudely:

  • tr works on characters (changes or deletes them).
  • sed works on lines (modifies words or other parts of lines, or inserts or deletes lines).
  • awk work on records with fields (by default whitespace separated fields on a line, but this may be changed by setting FS and RS).
5

sed has a y command that is analogous to tr:

y/string1/string2/

Replace all occurrences of characters in string1 with the corresponding characters in string2.

$ echo 'a big cat' | sed -e 'y/abc/def/'
d eig fdt

y does not support character ranges a-z or classes [:alpha:] like tr does, nor the complement modes -c and -C. Deletion (-d) is possible with s//, as is replacing runs with single characters (-s). As in all of sed it's fundamentally line-oriented. y is sometimes useful in the middle of a longer sed script, but tr likely does a better job if that's all you're doing.

awk has no equivalent to tr's base functionality, though you could of course replace characters one at a time. The awk language is quite general and you can write loops and conditionals sufficient to implement any transformation you want if you're motivated enough. If you were even more motivated you could probably manage that in sed too. The tools are not really equivalent in any sense.

0
$ echo ooops not good | tr oops nice
iice nit giid
$  echo oops not good | sed 's/oops/nice/'
nice not good
tr translate character by character, not word by word, so it will change all such characters, not only characters that have this exact sequence.

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.