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.:

user1@domain.com
user2@domain.com
user3@domain.com
user4@domain.com
user5@domain.com
user6@domain.com

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:

user1@domain.com;user2@domain.com;user3@domain.com;user4@domain.com;user5@domain.com;user6@domain.com

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