November 8

4 comments

Search Scan Replace text in IFS files on IBM i using QSH – iSeries AS400

By NickLitten

November 8, 2017

IFS, QSH, replace, scan, SED

Search Scan Replace text in IFS files on IBM i using QSH

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

Background

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

Problem

  • The batchfile is a generic installer so parameters like SAVE FILE and LIBRARY name will be changing for each install.
  • The batchfile is copied from a master copy (in the change management store) into a folder on the IFS for this particular distribution.
  • We then have a manual step that says “edit script file and update name of save-file and library to match those being distributed”

Now, I don’t know about you but I hate manual steps in the steps if there is an easy way to automate them.

Luckily there is.

Solution

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.

SED
Stands 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”

Search scan replace text in ifs files on ibm i using qsh - iseries as400 1

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')
Search scan replace text in ifs files on ibm i using qsh - iseries as400 2
Search scan replace text in ifs files on ibm i using qsh - iseries as400 3

It’s neat and simple isn’t it?

  • I love the idea behind this tool But i cant figure out how to keep the file in tact. I have created a file homeFileA.csv that I created from CPYTOIMPF. I can view it in Notepad or Excel. Once i perform the Command QSH CMD(‘sed ”s/|/,/g” /home/FileA.csv > /home/FileB.csv’ i can not display this new file. I can open it but it is garbage in notepad. I can view it in WRKLNK, but i need to send this to a user to be opened in Excel. What am i doing wrong?

  • This is excellent, Nick.

    Using this on a .csv file to remove double-quote character and output to a file, it appears to be successful but the output file is one long hex string. Any ideas on how to make the output file _stay_ as a .csv file?

    Here’s the SED command I used in QSH:

    sed ‘s/”//g’ FILEIN.csv > fileout.csv

    • Hi Folks, I’d like the syntax to find a string in the ifs…I thought this would work;
      QSH CMD(‘find /home -name -exec grep “VASQUEZ;” {} ;’)
      …but it’s not working.
      Any ideas on how to make it right?
      Any help/info appreciated.
      Frank in Boston

  • Following up … SED command works as entered. Open the file in notepad++, and it’s unreadable.

    HOWEVER… open in QSH ‘cat fileout.csv’, or open in ‘wrklnk…’ {browse}… and it’s fine – and the double-quote characters are removed.

    Thanks.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips

    >