-->

Previous | Table of Contents | Next

Page 69

5.2.2.11.1. Literal Text

Any part of a format string that is not associated with tags or array iterators will be treated as literal text. Literal text is just that: It's text that is printed just as it appears in the format string. In fact, a format string can consist of nothing but literal text, although the output wouldn't tell us much about the packages being queried. Let's give the --queryformat option a try, using a format string with nothing but literal text:


# rpm -q --queryformat `This is a test!' rpm

This is a test!#

The RPM command might look a little unusual, but if you take out the --queryformat option, along with its format string, you'll see that this is just an ordinary query of the rpm package. When the --queryformat option is present, RPM will use the text immediately following the option as a format string. In this case, the format string is `This is a test!'. The single quotes are required. Otherwise, it's likely that your shell will complain about some of the characters contained in the average format string.

The output of this command appears on the second line. As you can see, the literal text from the format string was printed exactly as it was entered.

5.2.2.11.2. Carriage Control Escape Sequences

Wait a minute. What is that # doing at the end of the output? Well, that's our shell prompt. You see, we didn't direct RPM to move to a new line after producing the output, so the shell prompt ended up being tacked to the end of the output.

Is there a way to fix that? Yes, there is. We need to use an escape sequence. An escape sequence is a sequence of characters that starts with a backslash (\). Escape sequences add carriage control information to a format string. The following escape sequences can be used:

Based on this list, it seems that an \n escape sequence at the end of the format string will put our shell prompt on the next line:


# rpm -q -queryformat `This is a test!\n' rpm

This is a test!

#

Much better.

Page 70

5.2.2.11.3. Tags

The most important parts of a format string are the tags. Each tag specifies what information is to be displayed and can optionally include field width, as well as justification and data formatting instructions. (RPM uses printf to do --queryformat formatting. Therefore, you can use any of the printf format modifiers discussed in the printf(3) man page.)

But for now, let's look at the basic tag. In fact, let's look at three: the tags that print the package name, version, and release. Strangely enough, these tags are called NAME, VERSION, and RELEASE. In order to be used in a format string, the tag names must be enclosed in curly braces and preceded by a percent sign. Let's give it a try:


# rpm -q --queryformat `%{NAME}%{VERSION}%{RELEASE}\n' rpm

rpm2.31

#

Let's add a dash between the tags and see if that makes the output a little easier to read:


# rpm -q --queryformat `%{NAME}-%{VERSION}-%{RELEASE}\n' rpm

rpm-2.3-1

#

Now our format string outputs standard package labels.

5.2.2.11.4. Field Width and Justification

Sometimes it's desirable to allocate fields of a particular size for a tag. This is done by putting the desired field width between the tag's leading percent sign and the opening curly brace. Using our package-label_producing format string, let's allocate a 20-character field for the version:


# rpm -q --queryformat `%{NAME}-%20{VERSION}-%{RELEASE}\n' rpm

rpm-                 2.3-1

#

The result is a field of 20 characters: 17 spaces followed by the 3 characters that make up the version.

In this case the version field is right-justified; that is, the data is printed at the far right of the output field. We can left-justify the field by preceding the field width specification with a dash:


# rpm -q --queryformat `%{NAME}-%-20{VERSION}-%{RELEASE}\n' rpm

rpm-2.3                 -1

#

Now the version is printed at the far left of the output field. You might be wondering what would happen if the field width specification didn't leave enough room for the data being printed. The field width specification can be considered the minimum width the field will take. If the data being printed is wider, the field will expand to accommodate the data.

5.2.2.11.5. Modifiers—Making Data More Readable

While RPM does its best to appropriately display the data from a --queryformat, there are times when you'll need to lend a helping hand. Here's an example. Say we want to display the name

Page 71

of each installed package, followed by the time the package was installed. Looking through the available tags, we see INSTALLTIME. Great! Looks like this will be simple:


# rpm -qa --queryformat `%{NAME} was installed on %{INSTALLTIME}\n'

setup was installed on 845414601

pamconfig was installed on 845414602

filesystem was installed on 845414607

...

rpm was installed on 851659311

pgp was installed on 846027549

#

Well, that's a lot of output, but it's not very useful. What are those numbers? RPM didn't lie—they're the times the packages were installed. The problem is that the times are being displayed in their numeric form used internally by the operating system, and humans like to see the day, month, year, and so on.

Fortunately, there's a modifier for just this situation. The name of the modifier is :date, and it follows the tag name. Let's try our example again, this time using :date:


# rpm -qa --queryformat `%{NAME} was installed on %{INSTALLTIME:date}\n'

setup was installed on Tue Oct 15 17:23:21 1996

pamconfig was installed on Tue Oct 15 17:23:22 1996

filesystem was installed on Tue Oct 15 17:23:27 1996

...

rpm was installed on Thu Dec 26 23:01:51 1996

pgp was installed on Tue Oct 22 19:39:09 1996

#

That sure is a lot easier to understand, isn't it?

Here's a list of the available modifiers:

5.2.2.11.6. Array Iterators

Until now, we've been using tags that represent single data items. There is, for example, only one package name or installation date for each package. However, other tags can represent many different pieces of data. One such tag is FILENAMES, which can be used to display the names of every file contained in a package.

Previous | Table of Contents | Next