1834804 Members
2244 Online
110070 Solutions
New Discussion

Re: for loop help

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

for loop help

Hi,

I would need help on the for loop to complete a file search,edit and save procedure.

I can make one command line working as expected as:

sed 's/'filesystem,null,null'/'filesystem,"''",null'/' aaa.fmb >bbb.fmb

But I cannot make it search and replace each of hundred of *.fmb files by using a shell script:

"for i in 'find -name *.fmb'
do sed 's/'filesystem,null,null'/'filesystem,"''",null'/' > /disk2/test2/i
done"

It should find next *.fmb file, execute sed, save the fmb file, then find next. I can figure out what parameter should be used as a majac.

Please help

Very appreciated.

Steven
Steve
6 REPLIES 6
Andreas Voss
Honored Contributor
Solution

Re: for loop help

Hi,

try this

for i in `find -name *.fmb`
do sed 's/'filesystem,null,null'/'filesystem,"''",null'/' $i > /disk2/test2/$i
done

Regards
Andreas Voss
Honored Contributor

Re: for loop help

Hi again,

just saw tha you have no directory parameter within the find, so it should/could be:

for i in `find . -name *.fmb`
do sed 's/'filesystem,null,null'/'filesystem,"''",null'/' $i > /disk2/test2/$i
done

Regards
RikTytgat
Honored Contributor

Re: for loop help

Hi,

If you know perl, you can use perl:

perl -p -i -e 's/searchstring/replacestring/' $(find . -name \*.fmb)

As you see, no need for a for loop. The above command does an in-place search and replace. No need to copy/re-copy your files.

If you require a backup of the original file, use the command

perl -p -i.bak -e 's/searchstring/replacestring/' $(find . -name \*.fmb)

This keeps the original file as .bak.

Hope this helps,
Rik.
Steven Chen_1
Super Advisor

Re: for loop help

Thanks a lot for Andreas and Rik's help.

Andreas:
I get error of:
sed: Cannot find or open file find.
sed: Cannot find or open file -name.
and one /disk2/test2/ I got a file name as
"find . -name *.fmb".
Please help.

Rik:
I could try Linux box that may have perl. Please tell me the how to use it, such as unix shell script first line as "#!/bin/sh, etc..

Very appreciated. I just want to have it worked!

Steven
Steve
Andreas Voss
Honored Contributor

Re: for loop help

Hi,

you have to take care what character you use at the find command:
The character has to be ` NOT '
If you use ' the command will not be executed.
Instead you have to use the ` character to execute the command.

Regards
Steven Chen_1
Super Advisor

Re: for loop help

Thanks for all the help.

I tested a few files that were changed as expected, which is good. But I can not have form program to open it, even though the filename not changed. I do see the file size become smaller.

Is there anyway to have sed to keep original pattern after changing just a word? There could be some parameter, g? or sth?

while *.fmb file can be open as text file, it should be OK with sed.

Thanks a lot,

Steven
Steve