Data is the most valuable asset for any organization. Data may be numeric: like integers that represent serial numbers, day of the month, and Julian date, and like floating-point decimals that represent monetary info, longitudes and latitudes. Data could be also in the form of strings of text that represent many types of information like first name, home address, Scientific qualification, place of birth, employer, and book title. The ability to process or manipulate text is then of great importance; that is why this article was written. In this article, we are going to learn together the necessary skills required to effectively work with text data. So, bring your coffee and your attention.
Display File Contents
Every thing in Linux is a file. So, data strings are stored in files. We have learned earlier in this series how to create and edit files using the vim editor. We have seen also how to redirect command outputs into files. Those were two ways to store data in files. Now, we need to know how to read or display the contents of a file. In this context, Red Hat Linux offers five tools that we will present and illustrate now with examples.
The cat command is used to concatenate one or more files and print their contents on the standard output.
cat [OPTION]... [FILE]...
- To display the contents of the /etc/hosts file:
- To print the contents of the file /etc/sysconfig/network-scripts/ifcfg-eth0 with line numbers next to each line:
- To concatenate (combine) the contents of the files /etc/sysconfig/network, /etc/hosts, and /etc/resolv.conf and redirect the combined text into a file:
The more command is used for paging through text one screen-full at a time. This is useful to scroll across lengthy files (without having to open the file in vi editor that may cause corruptions or accidental modifications)
more FILE …
To view the contents of both /etc/passwd and /etc/group files one page at a time:
more /etc/passwd /etc/group
The less command is similar to more, but allows backward movement in the file as well as forward movement.
less FILE …
The opposite of cat; it writes one or more files to standard output, with last line first. If no files specified, the command reads and processes the standard input.
To print the contents of the file /etc/sysconfig/selinux from last line to first:
The rev utility copies the specified files to the standard output, reversing the order of characters in every line. If no files are specified, the standard input is read.
The following prints the contents of the file /etc/sysconfig/network-scripts/ifcfg-eth0 with characters of each line reversed:
Consider the output of the w command:
The following will print the above output reversed:
Extracting Portions of Files
Sometime, you need to extract only several lines from a file. For this purpose, the head and tail utilities were created.
This command prints the first K lines of each argument file to the standard output. With more than one file, extracted portions from each file are preceded with a header giving the file name. With no file specified, or when FILE is -, the standard input is used. If the number of lines to extract K is not specified, the command prints the first ten lines.
head [–n K] [FILE]… COMMAND | head [-n K]
To print the first 5 lines in the /etc/passwd file:
head -5 /etc/passwd
head –n 5 /etc/passwd
To print the first ten processes from the output of the ps command:
ps -ef | head -10
ps -ef | head
The tail command prints the last K lines of each argument file to the standard output. With more than one file provided, it precedes each with a header giving the file name. With no argument files, or with -, it reads standard input.
As with head, if the number of lines to extract is not specified, the command returns the last ten lines.
tail [±K] [FILE]… COMMAND | tail [±K]
-K or –n K returns the last K lines.
–n +K returns the lines starting from Kth to the end.
To print the last ten lines of the /etc/group file:
tail -n 10 /etc/group
tail -10 /etc/group
Given the following output from /etc/passwd file:
To print lines from the 7th to 11th of the file, use the following trick:
tail -n +7 /etc/passwd | head -5
Red Hat provides a rich set of tools that help administrators and users better manage text processing.
- The commands cat, more, less, tac, and rev display files’ contents.
- The head and tail commands extract lines from the beginning and from the end of files, respectively.
That was part one of the Text Processing topic. See you in part two.