Basic Linux TutorialIntroduction – What is it? Why to learn? Linux installation directory structures Boot process Run levels in Linux Desktop Environments Different shells BASH Internal and External Commands Basic Linux Commands Important files and directories in Linux Environmental and Shell Variables Command history in Linux Character classes in Linux Text editors vim nano Searching files Creating new files Viewing File Contents File commands File permissions and ownership WildCards (Globbing) in files File compression Directory commands xargs command in Linux Comparing files Searching patterns using grep command Translating the characters using tr command Extracting data using cut command Stream editing using sed command Data extraction and reporting using awk command Sorting the file or string input uniq command in Linux Difference between grep, tr, cut, sed and awk commands Hardware commands Hard disk and memory space commands Working with Processes Managing Jobs Working with cron jobs Service command in Linux Network commands Managing Users and Groups Other Popular commands Standard streams and Redirection Pipes Package Managers in Linux
tr command in linux shelltr command is used to translate the input into another form. tr command allows you to do below things on given data.
- Replace the characters from given string with other set of characters.
- Delete specific characters from the input string.
- Squeeze the occurrences of specific character.
Another way to do same thing is by specifying the range of characters.
cat myfile | tr [:upper:] [:lower:]
If you want to remove specific set of characters, you can use below command. Here “-d” stands for delete.
cat myfile | tr [A-Z] [a-z]
To remove spaces, you can use below command.
cat myfile | tr -d [A-C]
To remove all digits, you can use below command.
cat myfile | tr -d [:space:]
To complement the output, you can use below command. In below example, everything except digits will be deleted from the file. Here “-c” option stands for complement.
cat myfile | tr -d [:digit:]
To remove repeating occurrences of a character, you can use below command. Below command will replace all repeating occurrences of a by single character – a. Here “-s” stands for squeeze.
cat myfile | tr -cd [:digit:]
cat myfile | tr -s [a]
Complex problems, Simple Solutions