Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
USS: Find and replace string within all files in a directory

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> All Other Mainframe Topics
View previous topic :: :: View next topic  
Author Message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1618
Location: Oregon

PostPosted: Thu Aug 02, 2018 4:01 am    Post subject: USS: Find and replace string within all files in a directory
Reply with quote

Hi,

On OMVS, I have a requirement to change all occurences of a string to another, within all files in a directory.
Most of the Unix help sites suggest Unix Stream edit command with -i
Code:
sed -i -- 's/apple/orange/g' *

But OMVS gives the below error,
Code:
FSUMA930 sed: Unknown option -i
Usage: sed [-BEn] [-W option[,option]...] script [file...]
       sed [-BEn] [-e script] ... [-f scriptfile] ... [-W option[,option]...] [file...]

Using the command without -i, replaces the string OK and displays it on screen, but does not write the output to file.
Could you please suggest how this could be done.

Thanks in advance,
Vasanth.S
Back to top
View user's profile Send private message

vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1618
Location: Oregon

PostPosted: Thu Aug 02, 2018 4:13 am    Post subject:
Reply with quote

Also tried using > /filename

But it writes the contents of all files into one single file, multiplied the number of files in the directory.
Back to top
View user's profile Send private message
phunsoft

New User


Joined: 19 Jul 2018
Posts: 11
Location: Switzerland

PostPosted: Thu Aug 02, 2018 12:10 pm    Post subject:
Reply with quote

Note this comment from the z/OS Unix Command Reference:

sed
...
If more than one file is specified, they are concatenated and treated as a single large file.



So sed on z/OS UNIX cannot do what you need. I suggest you use find with the -exec option. Something like this:

Code:
find . -name "*" -exec sed 's/apple/orange/g' {} > {} \;


Unfortunately, I was not yet able to come up with a working solution redirecting the result back to the same file. I googled for suggestions, but they don't work on z/OS. Not sure if this is a defect of z/OS's find.

As an alternative, I suggest to store the sed command in a short shell script, say sed.sh (and save it in some other directory, so find will not find it)

Code:
#!/bin/sh
sed "s/apples/oranges/g" $1 > $1


Then run the follwing find command

Code:
find . -type f -exec /somedirectory/sed.sh {} \;
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1618
Location: Oregon

PostPosted: Thu Aug 02, 2018 11:19 pm    Post subject:
Reply with quote

Hi Peter,

Thank you for looking at this and the information you provided.

I tried your suggestion of putting these lines in a separate executable file
Code:
#!/bin/sh
sed "s/apples/oranges/g" $1 > $1

And then call the script from the directory where the strings needs to be replaced like
Code:
find . -type f -exec /somedirectory/sed.sh {} \;


After the command executes, all the files in the directory are empty after that.
Am I missing something?

Regards,
Vasanth.S
Back to top
View user's profile Send private message
phunsoft

New User


Joined: 19 Jul 2018
Posts: 11
Location: Switzerland

PostPosted: Fri Aug 03, 2018 12:16 am    Post subject:
Reply with quote

I of course tried before posting, and it did work for me. Arghhhh... I'm sorry, I dropped something when typing the shell script... The redirection must go to a file different from the input. So the complete code looks like this:

Code:
#!/bin/sh
sed "s/apples/oranges/g" $1 > $1.changed


Use the suffix of your choice, or redirect to a different, empty directory and keep the file name.

Apologies.
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1618
Location: Oregon

PostPosted: Fri Aug 03, 2018 5:42 am    Post subject:
Reply with quote

Thanks Peter & welcome to the forum.

Your solution worked perfectly.Yay! The script was able to find and replace all the occurrences.

Just routed the output to a different directory and copied it to source directory
Code:
#!/bin/sh                                 
sed "s/http/https/g" $1 > /changed/$1

& invoked it with
Code:
find . -type f -exec /some/place/sed.sh {} \;


Thanks,
Vasanth.S
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> All Other Mainframe Topics All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Anyone know how to copy Unix files? John Poulakos JCL & VSAM 7 Wed Oct 10, 2018 12:57 am
No new posts PL/I read error for HFS files Joe Kirsch PL/I & Assembler 6 Fri Sep 28, 2018 9:13 pm
No new posts Find out which PSB is currently sched... Andi1982 IMS DB/DC 0 Mon Sep 24, 2018 3:16 pm
No new posts How to find if segment is read in any... Priyanka AR IMS DB/DC 12 Thu Sep 20, 2018 1:34 pm
No new posts UNSTRING a big string Apurva Ghogale COBOL Programming 16 Thu Sep 20, 2018 12:12 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us