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.
1 |
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.
1 |
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.
1 |
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.
1 |
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.
1 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ 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.
1 |
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:
1 |
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.)
1 |
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.
1. If I am writing to a file with input from the terminal, is there a way to signal the end of input other than pressing F3? When using DOS, we would use Ctrl-C or Break.
e.g.
Rfile -wQ “glennlib/qclsrc(temp)”
2. How can I write to an IFS stream file with input from the terminal?
I’ve tried the following command but get an error message: myIFSfile is not a device file or database member.
Rfile -w myIFSfile.txt
1. Control-D would be the way to signal end of file in a normal terminal, but I’m not sure with 5250 what the equivalent would be, if present. The 5250 based dumb terminal emulation is clumsy and I don’t really recommend using it if you can use SSH instead.
2. Don’t use Rfile, you can use shell input redirection (i.e. cat > file.txt, ls > file.txt, etc.).
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.
Thanks for the tip, Russ!