This morning I wanted to find a quick and dirty way to “Scan Replace text in IFS” files without writing a program to do it…. SPOILER ALERT – it’s really really insanely simple using JAVA QSHELL and the SED command
So, before I start into techno-babble mode, here is some background of why I’m looking for IFS SCAN REPLACE
I’ve just finished writing a software install script which does all kinds of digital shenanigans and ends up saving a library to a SaveFile (*SAVF) and creating an installer script (Dos Batch File) on the IFS to automate the installation of that save file when it lands on the end clients machine. The idea is that we can simply zip it up, email it to the customer and they can simply unzip it and run the batch file which will do all the uploading, restoring and configuring for them.
note to self – put this script into a blog and share it with other IBM i folks that are equally annoyed by the repetitive FTP, BIN, NAMEFORMAT, UPLOAD nonsense
Now, I dont know about you, but I hate manual steps in the steps if their is an easy way to automate them.
Luckily there is.
Use the SED UNIX utility which lives in JAVA on our machine: Sed is a powerful tool to do some text manipulations in a file.
SEDStands for: Stream editor
Function: Stream editor for filtering and transforming text
SED lets us quickly replace text in a string – or an IFS FILE 🙂
echo 'I like beer' I like beer
if we SED it:
echo 'I like beer' | sed "s/beer/sausages/" I like sausages
Now we can do that same scan/replace in an IFS file by piping in an input and an output file.
Lets say we had an IFS file that contained the word “beer” and we want to scan for it and replace with word “sausages”
I’ve typed this into a file called test.bat in folder litten and will scan it, replacing beer for sausages (sacrilege!) and output the result to a file called result.bat in the same folder:
STRQSH CMD('sed "s/beer/sausages" /littenn/test.bat > /littenn/result.bat')
and boom it does it!
this replaces the first occurrence and if we wanted to scan the entire file we would just add the ‘/g’ switch (NOTE: must be lower case)
STRQSH CMD('sed "s/beer/sausages/g" /littenn/test.bat > /littenn/result.bat')
It’s neat and simple isn’t it?
IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and in the words of the most interesting man in the world: Stay thirsty my friend.
Funky IBM i Email Validation Program using SQL Regex
How to compare ‘100 Year Date’ to a Timestamp – aka – Weird AS400 iSeries Date formats
Modernize AS400 iSeries Queries – Convert to IBM i SQL
#IBMi Java – Avoiding the QSH “Press ENTER to end terminal session” message
Search Scan Replace text in IFS files on IBM i using QSH – iSeries AS400
From AS400 DLS to IBM i IFS – Evolution of an Integrated File System