i Linux: Text Manipulation – All things in moderation

Linux: Text Manipulation

Hi everyone! In this post we will research text manipulation. With so many text files, manipulating text becomes crucial in managing Linux and Linux applications. In this tutorial, we’ll look at several of the commands and techniques for manipulating text in Linux.
Cat
Cat is probably the most basic text display command. Let’s cat the php5.conf file in /etc/apache2/mods-enabled/

You can see, the php5.conf is displayed on our screen until it comes to the end of the file. Not the most convenient or practical way to work with this file.

Take the Head
If you want view beginning of a file, you can use “head”. Default, this command displays the first 10 line of a file.
Command:

Result:

If you want to more or less than default 10 lines. You can use command with lines number.

You can see the first 15 lines in php5.conf are displays.

Tail
If you want view last of a file, you can by using “tail”.

Command:

Result:

If you want to more or less than default 10 lines. You can use command with lines number.

View numbers lines
Sometimes—especially with very long files—we may want the file displayed with line numbers. This makes it easier to reference changes and come back to the same place within a file. To display a file with line number, we simply type:

Result:

Search with stream editor
The sed command essentially allows us to search for occurrences of a word or text pattern and then do some work on it.
Let’s search for the word “it” in the php5.conf file using grep.

We can see grep command found 4 occurrences of the word it.
We will use sed and replace every occurrence of it with cloudi. and then save the new file to Cloudi.conf.

That’s it for this lesson, but there are many more to come, so check out our section on learning Linux/Unix to stay up to date.If you have any questions about this article, ask away in the comments below.

Leave a Reply