Learn about Text Processing Tools in Red Hat Linux Administration – Part 1

0
1807
text-processing

text-processing

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.

cat
The cat command is used to concatenate one or more files and print their contents on the standard output.
Syntax

Examples

  • To display the contents of the /etc/hosts file:
    hosts-file
  • To print the contents of the file /etc/sysconfig/network-scripts/ifcfg-eth0 with line numbers next to each line:
    ifcfg-eth0
  • 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:
    resolv-conf

more
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)
Syntax

Example
To view the contents of both /etc/passwd and /etc/group files one page at a time:

etc-passwd

less
The less command is similar to more, but allows backward movement in the file as well as forward movement.
Syntax

Example

notrap

tac
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.
Example
To print the contents of the file /etc/sysconfig/selinux from last line to first:
selinux

rev
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.
Syntax

Examples

The following prints the contents of the file /etc/sysconfig/network-scripts/ifcfg-eth0 with characters of each line reversed:

ifcfg-eth02

Consider the output of the w command:
w-command

The following will print the above output reversed:
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.
head
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.
Syntax

Examples

To print the first 5 lines in the /etc/passwd file:

or:

passwd-file

To print the first ten processes from the output of the ps command:

or:

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

Syntax

Where:
-K or –n K returns the last K lines.
–n +K returns the lines starting from Kth to the end.

Examples

To print the last ten lines of the /etc/group file:

or:

or:

group-file

Given the following output from /etc/passwd file:
etc-passwd-file

To print lines from the 7th to 11th of the file, use the following trick:

head-5

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here