IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

USS: Find and replace string within all files in a directory


IBM Mainframe Forums -> All Other Mainframe Topics
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1742
Location: Tirupur, India

PostPosted: Thu Aug 02, 2018 4:01 am
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: 1742
Location: Tirupur, India

PostPosted: Thu Aug 02, 2018 4:13 am
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
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: 1742
Location: Tirupur, India

PostPosted: Thu Aug 02, 2018 11:19 pm
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
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: 1742
Location: Tirupur, India

PostPosted: Fri Aug 03, 2018 5:42 am
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> All Other Mainframe Topics

 


Similar Topics
Topic Forum Replies
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Write line by line from two files DFSORT/ICETOOL 7
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
Search our Forums:

Back to Top