View previous topic :: View next topic
|
Author |
Message |
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
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 |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
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 |
|
|
phunsoft
New User
Joined: 19 Jul 2018 Posts: 11 Location: Switzerland
|
|
|
|
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 |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
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 |
|
|
phunsoft
New User
Joined: 19 Jul 2018 Posts: 11 Location: Switzerland
|
|
|
|
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 |
|
|
vasanthz
Global Moderator
Joined: 28 Aug 2007 Posts: 1742 Location: Tirupur, India
|
|
|
|
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 |
|
|
|