tr and cut command

tr

The tr command is used to translate or delete characters that comes from the standard input.

If say you would like to replace every character in a file with another character then you can just do something like:

echo "hello world" | tr e z // Prints out "hzllo world"

If you would like to delete characters then you would use the -d flag and provide in the set of characters that you would like to delete like such:

echo "hello world" | tr -d e // Prints out hllo world

cut

The cut command is used like the split function. To extract out the field that you want from a particular file or from standard input. For example:

echo "hello-world-lol" | cut -d "-" -f1

This will split the string according to the delimiter "-", then output the first field after splitting which is just "hello". You must specify the field that you would like to extract after splitting.

 


Revision #1
Created 29 April 2023 17:10:14 by Tamarine
Updated 29 April 2023 17:16:50 by Tamarine