System ProgrammingLearn about Text Processing Tools in Red Hat Linux Administration - Part...

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

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

cat [OPTION]... [FILE]...

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

more FILE …

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

more /etc/passwd /etc/group

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

less FILE …

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

rev [FILE…]

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

head [–n K] [FILE]…
COMMAND | head [-n K]

Examples

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

head -5 /etc/passwd

or:

head –n 5 /etc/passwd

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

ps -ef | head -10

or:

ps -ef | head

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

tail [±K] [FILE]…
COMMAND | tail [±K]

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:

tail -n 10 /etc/group

or:

tail -10 /etc/group

or:

tail /etc/group

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:

tail -n +7 /etc/passwd | head -5

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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -