INTRO
In this tutorial, We are going to learn about Tr and seq commands which is a topic of shell scripting. Shell scripting can be very useful for Linux users to automate stuff. A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script that sets up the environment, runs the program, and does any necessary cleanup, logging, etc. is called a wrapper. READ MORE…
We are not going to learn all shell scripting. but we are just going to take a look at these commands. so, we may get an idea to use these while automation. As you know, Linux most used utility is Terminal. Every task that we can do graphically, can be done with a Terminal. and It doesn’t matter what terminal you use. By the way, I use Tilix Terminal. And every Terminal has a default shell, mostly bash but you can choose it yourself. By automation, I mean that we can use a shell to repeat a task with different arguments and inputs.
Before you go any further, I would love to suggest some of my previous articles.
Honestly, these aren’t essential to read. but still, you can take a look at these to make your terminal beautiful and attractive like me. one more thing, use Bash for this tutorial, not ZSH.
COMMANDS
• TR
Let’s talk about tr
first. it is a simple but essential command for shell scripting. tr can be used to replace, delete, and squeezes strings. basically, it is a tool for string operations in shell scripting. remember that we are not learning shell scripting. so, I am not going to explain everything that I am going to do. we are going to just take a look at these commands.
Read the manual of tr, type this command:
man tr
Screenshot:
Let’s dig into examples of tr command. first of all, I would like to show you the use of tr command to remove unwanted chars from a string.
For example, we have a string “Old Town Roadxxxx” in which we have some unwanted chars “xxxx”. now we want to remove it with tr. Type this command:
echo "Old Town Roadxxxx" | tr -d "x"
Screenshot:
-d
is to tell the command that we want to delete “x” from the string. you can change the unwanted char and the string.
You can also use [=x=]
instead of “x” where “x” is a char that you want to remove.
echo "Old Town Roadxxxx" | tr -d [=x=]
If you want to remove some spaces from a string. type this command:
echo "Lucky Thandel" | tr -d [:space:]
There is another way to do the same thing, we just did. we can use this command:
echo "Lucky Thandel" | tr -d " "
If you want to replace a char with some other char, type this command:
echo "Lucky Thandel" | tr " " "\n"
Screenshot:
and obviously, you can change the string and chars that you want to replace.
A very important operation you can perform with this tool is that you can use it to replace all lower case chars with upper case chars. or you can do the opposite.
You can also use this command:
echo "Lucky Thandel" | tr a-z A-Z
Screenshot:
If you are reading this article properly, I mentioned about squeezing. It means that we can stop the repetition of a char with tr command. type this command:
echo "Laaaaaaa La Land" | tr -s "a"
Screenshot:
Before going to another command, let’s take a look at real-life examples of tr command. I have a file that I downloaded from a website. I am using the md5sum to check its hash value. let’s see the output:
Screenshot:
You can see that it shows us the hash value of the file. now let’s suppose, you need this hash value only not the file name its printing along with it. So, to do that, type this command:
Note that tr command is helpful to deal with chars, not with hole strings. you can remove, replace and squeeze chars not the whole string.
SEQ
If you know python loops, you must have heard about range function. mostly, the range function is used when we are using for
loop.
For example, if we want to print numbers from 0 to 9, type this command:
python3 -c 'for i in range(0,10):print(f"Num is {i}");i+=1'
Screenshot:
As you can see the output, we have numbers from 0 to 9. when you do the same thing in bash you use “seq”.
Type this command to get the same output as above:
for i in $(seq 0 9);do echo "num $i";done
Screenshot:
We can save time by using seq. This can be done without seq but as I said it will take more time.
Type this command:
for i in 1 2 3 4 5 6 7 8 9;do echo "num $i";done
Screenshot:
Thanks For Visiting.
Hey Lucky, love your work! Big up! What is this theme you use that has all these green accents… Looks cool if you ask me. I’d like to customize my terminal to look like so