AWK and CUT in Linux - a few examples!

in linux •  2 years ago 

I am not writing this to teach you anything, I am rather writing it so that I will better remember these commands and how to use them. After all, it is easy to understand them, but it is harder to remember them.

So, let us take a quick look at these commands.

awk

This is a command for editing and modifying texts and outputs. It is quite useful, though it feel strange to modify documents bigtime in the command line instead of in a normal word processor like Word or Writer. How can I use this command?

ls -l | awk '{print $1}'

Do you know what this command will do? It will create a list of the directory you are in, but with the awk command, you will tell it only to print the first colon of the list. As a result, you will only see the file permissions. If you would change the $1 with the $9, it will only print the name of the file or the directory. You can also combine several and type $1,$3,$5 if you want to print only those bars.

That is a tiny example of the awk. It can also be used to change content of a file.

cat actors | awk '{if($2 == "Jackson") print $0}'

What will this do? If the second colon in the file actors contain the name Jackson, it will print those lines and nothing else. You can even use the command to change the name Jackson to Ronaldo by using the following command.

cat actors | awk '{if($2 == "Jackson") print $1,$2="Ronaldo"}'

Cool, right? You need to create the actors file first and add some names to it, and then you can play around. Tomorrow, I will write about the CUT command as this was enough for today!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!