Reading and Writing Physical Files in a Shell using Rfile

Want to read source member data from within an IBM i shell environment, such as SSH, QShell, or QP2TERM? Need to copy records from a text file to a physical file? Want a quick way to upload a save file without needing FTP?

IBM i’s Rfile command can do all these things easily. Rfile is designed to be easy to integrate into scripts, so it’s a great tool to have at hand.

To try the examples below, first launch an IBM i PASE shell, such as by typing the QSH command or using SSH.

Reading and writing source file members

Rfile is a flexible command. It accepts numerous flags that change its behavior.

The -r (read) flag makes Rfile read a file and output its contents. As it runs, Rfile automatically performs CCSID conversion from EBCDIC to ASCII/UTF-8, and converts each record to a separate line.

Rfile -r /QSYS.LIB/QSYSINC.LIB/H.FILE/STDIO.MBR

By default, Rfile expects an IFS-style path to the source member, for example “/QSYS.LIB/QSYSINC.LIB/H.FILE/STDIO.MBR“. To make it take a traditional path specification, add the -Q flag. Note that you may need to wrap the path in quotes to avoid your shell from interpreting the path as syntax.

Rfile -rQ "QSYSINC/H(STDIO)"

By default, Rfile outputs data to your terminal (through standard output). To make it write to a file instead, you can use shell syntax (the > operator) to redirect it to an IFS file.

Rfile -rQ "QSYSINC/H(STDIO)" > stdio.h

The -w (write) flag makes Rfile write to the source file. By default, Rfile will get data from your terminal (through standard input), but you can use the opposite shell syntax (the < operator) to use an IFS file instead. As with reading, Rfile will perform CCSID conversion and treat each line as a record.

Rfile -wQ "MYPROJ/QCSRC(MAIN)" < main.c

The -a (append) flag, when added to -w,  lets you append to the file, instead of overwriting it.

Rfile -waQ "MYPROJ/QCSRC(MAIN)" < main.c

Reading and writing physical files

Physical files are handled by Rfile in the same manner as source files.

Rfile assumes a fixed format for text files generated from (or used to upload to) the physical file. All columns will be directly next to each other, like so:

$ Rfile -r /QSYS.LIB/QIWS.LIB/QCUSTCDT.FILE/CUSTCDT.MBR 
938472Henning G K4859 Elm Ave DallasTX7521750003003700000000
839283Jones   B D21B NW 135 StClay  NY1304104001010000000000
392859Vine    S SPO Box 79    BrotonVT0504607001043900000000
938485Johnson J A3 Alpine Way Helen GA3054599992398750003350
397267Tyron   W E13 Myrtle Dr HectorNY1484110001000000000000
389572Stevens K L208 Snow PassDenverCO8022604001005875000150
846283Alison  J S787 Lake Dr  Isle  MN5634250003001000000000
475938Doe     J W59 Archer Rd SutterCA9568507002025000010000
693829Thomas  A N3 Dove CircleCasperWY8260999992000000000000
593029WilliamsE D485 SE 2 Ave DallasTX7521802001002500000000
192837Lee     F L5963 Oak St  HectorNY1484107002048950000050
583990Abraham M T392 Mill St  Isle  MN5634299993050000000000

Likewise, you can write to the physical file from an IFS text file that has fixed-format columns.

Rfile -w /QSYS.LIB/MYPROG.LIB/PF.FILE/TABLE.MBR < records.txt

Reading and writing save files

So far, the commands we’ve been using have handled text files and database tables.

What if we needed to work with binary data, like save files? These shouldn’t have any CCSID conversion applied, or their contents will be mangled. Luckily, Rfile has the “-b” flag (binary processing), which will make Rfile read and write without any CCSID or line handling. While you can use binary mode to process physical and source files in their “raw” form, the main purpose is moving save files in and out of the IFS.

For example, you copy a save file object to the IFS with the following command:

Rfile -rbQ MYPROG/BACKUP > backup.savf

You can also go in the other direction and copy a save file from the IFS to an object. (You may need to create the savefile first by using the CRTSAVF command.)

Rfile -wbQ MYPROG/BACKUP < backup.savf

Rfile can handle many file conversions on IBM i

For more options, refer to the Rfile manual page or get in touch.

2 replies
  1. Russ Khoury
    Russ Khoury says:

    I use rfile to print text files built in the IFS by referencing a previously built printer file (PRTF). rfile -wQ mylib/myprtf < mytextdoc.txt
    The printed output looks fine.
    Thanks for your article.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.