Tuesday 10 January 2017

Easily replacing characters with the tr command in Linux

Just like many programming languages you can replace a character or characters within a string - this is also possible with Unix/Linux using the tr command - for example lets say we have a long list of emails separated line by line e.g.:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

We can use tr to replace the line break with a semi-colan - so that it can easily be imported into an email clinet:

cat file.txt | tr '\n' ';'

Wish provides us with the following output:

[email protected];[email protected];[email protected];[email protected];[email protected];[email protected]

We can also do some mundane tasks such as capitalizing words in a text file - for example:

cat file.txt
this is a test

cat file.txt | tr a-z A-Z

would produce:

THIS IS A TEST

0 comments:

Post a Comment