>_ IzzyLab

Hands-on task

This task is step 2 of the course “Text in the terminal: grep, sed, awk”

Translate characters

easy~4 min

The price list /root/import/prices.txt was exported from a Windows program: its fields are separated by tabs, and the lines end Windows-style. Turn it into a proper CSV /root/import/prices.csv: fields separated by a comma, Linux line endings, and the data itself unchanged.

What you'll practise:
  • tr
  • case conversion
  • posix character classes
  • stream redirection

Topic breakdown

The tr tool is a streaming character translator: it reads stdin and replaces characters from one set with the matching ones from another, one character at a time. Sets are specified by enumeration, ranges, or POSIX classes ([:lower:], [:upper:], [:digit:]), which is handy for operations over whole categories of characters such as case changes. tr does not open files itself — it works only with a stream, so the input is supplied via redirection and the result directed into a new file.

What's checked

  1. Linux line endings
  2. fields separated by commas, data intact

What it looks like