LPI 1 Article #7 Various File Manipulations


The use of od to convert files into octal
There are files that are almost impossible to be displayed in ASCII format. For instance, most of the files in the media field such as graphics and audio use characters not supported by ASCII. Just try to view these files using cat command and you will see what it will do to your terminal area. However, if you would wish to check the data structure of such files. The command od does wonders.
$ od name_of_file

Have you ever wanted to sort the contents of a file in a particular fashion and you had no idea how you are going to accomplish that task? Well, there is an intuitive command that will make the task a cinch. The sort command can sort Monthly, using numbers, in reverse, and also by filed. These are just a few of the ways that sort command can arrange the output of a file.
Ignore case: the -f or –ignore-case options causes the command to not consider cases
Sort monthly: the -M or –month-sort will sort the file from JAN to DEC using three letters as shown
Ordering in reverse: the -r or –reverse does the trick
Sorting by field: the -k field or also –key=field

The uniq command
Suppose you have a file with let us say names or items and you would wish to view its contents without viewing duplicate items in the file. The uniq i command comes as a tool for your use and will assist you to list the items only once without displaying another name or item of similar characteristics’
$ sort file.txt | uniq

How to format texts in a specified file
There are three common commands that are used to format text files namely nl, fmt and pr.
i. fmt
There are text files that have lengths of lines that are too long and they are quite inconvenient to read them on a text editor. Chances are that you would wish to make it a bit friendly by formatting the lengths of the texts to a more convenient form. The fmt tool does that kind of a job well. When it is issued without any options to the file being desired to be read, the tool formats the text to a default paragraph that is no more than 75 characters wide. It assumes a paragraph when there is a delimitation of two or more blank lines or if there are any changes in the indentation. The man page of the command will be helpful in finding out more of its capabilities
$fmt file1.txt
nl is a line numbering tool. This can be such a useful way for the programmers or any other text viewer if they would wish to refer to a particular line without the strain of counting one line after the other.
Numbering header and footer: nl -h style and nl -f style for header and footer respectively.
Style of the body numbers: this is specified by option -b style

to number the non empty lines in a file, just do:
$ nl -b t file1.txt

Style code What it does
a All lines are numbered including empty ones
n Omits line numbers. Can use it for headers and footers
t Numbers non empty lines
pREGEXP Lines with the inclination of the regular expression stated are numbered

to number the non empty lines in a file, just do:
$ nl -b t file1.txt


Please enter your comment!
Please enter your name here