Formatting Text with Linux Part-2

Cyberwizardy23
3 min readOct 13, 2022

--

Formatting Output With Linux

In our second part we will see more tools are:
●pr — Prepare text for printing
●printf — Format and print data

pr — Format Text For Printing
The pr program is used to paginate text. When printing text, it is often desirable to separate the pages of output with several lines of whitespace, to provide a top and bottom margin for each page. Further, this whitespace can be used to insert a header and footer on each page.

┌──(me㉿kali)-[~/Desktop/blog_examples]
└─$ pr -l 15 -w 65 distros.txt

2022–10–05 18:58 distros.txt Page 1

SUSE 10.2 12/07/2006
Fedora 10 11/25/2008
SUSE 11.0 06/19/2008
Ubuntu 8.04 04/24/2008
Fedora 8 11/08/2007

2022–10–05 18:58 distros.txt Page 2

SUSE 10.3 10/04/2007
Ubuntu 6.10 10/26/2006
Fedora 7 05/31/2007
Ubuntu 7.10 10/18/2007
Ubuntu 7.04 04/19/2007

2022–10–05 18:58 distros.txt Page 3

SUSE 10.1 05/11/2006
Fedora 6 10/24/2006
Fedora 9 05/13/2008

In this example, we see the -l option for page length and the -w option page width to define a “page” that is 65 columns wide and 15 lines long. pr paginates the contents of the distros.txt file, separates each page with several lines of whitespace and creates a default header containing the file modification time, filename, and page number.

printf — Format And Print Data
the printf command is not used for pipelines (it does not accept standard input) nor does it find frequent application directly on the command line it’s mostly used in scripts.

printf (from the phrase “print formatted”) was originally developed for the C programming language and has been implemented in many programming languages including the shell. In fact, in bash and zsh, printf is a builtin.

syntax:
printf “format” arguments

┌──(me㉿kali)-[~]
└─$ printf “I formatted the string: %s\n” foo
I formatted the string: foo

The format string may contain literal text (like “I formatted the string:”), escape sequences (such as \n, a newline character), and sequences beginning with the % character, which are called conversion specifications. In the example above, the conversion specification %s is used to format the string “foo” and place it in the command’s output.

Common printf Data Type specifiers

Specifier :-> Description
d :-> Format a number as a signed decimal integer.
f :-> Format and output a floating point number.
o :-> Format an integer as an octal number.
s :-> Format a string.
x :-> Format an integer as a hexadecimal number using lowercase a-f where needed.
X :-> Same as x but use uppercase letters.
% :-> Print a literal % symbol (i.e., specify “%%”)

Here another example of different specifier shows different results:

┌──(me㉿kali)-[~]
└─$ printf “%d, %f, %o, %s, %x, %X\n” 240 240 240 240 240 240
240, 240.000000, 360, 240, f0, F0

we specified six conversion specifiers, we must also supply six arguments for printf to process. The six results show the effect of each specifier.

Printf is used mostly in scripts where it is employed to fotmat tabular data, rather than on the command line directly.

┌──(me㉿kali)-[~]
└─$ printf “<html>\n\t<head>\n\t\t<title>%s</title>\n
\t</head>\n\t<body>\n\t\t<p>%s</p>\n\t</body>\n</html>\n” “Page Title” “Page Content”

<html>
<head>
<title>Page Title</title>

</head>

<body>
<p>Page Content</p>
</body>
</html>

Thanks for Reading

Here we finished Basic and some advance commands and now we will see scripts and its commands and maybe in future we will see these basics commands in very deep.

--

--

Cyberwizardy23

Linux and Networking Learner , Interested in Cyber Security and Active Learner