1

I have a set of commands to change the contents of files called MenuSettings which work fine when directed to a folder.

cd /Applications/snowflake/table-updater/Testing/Applications/Salford_100

sed -e '1r MenuSettings.bak' -e '1,2d' MenuSettings.txt >> MenuSettings2.txt
cat MenuSettings2.txt | sed '/^AppName: */{h;s///;x;}; /^\(AppName_.._..:\).*/{s//\1/;G;s/\n/ /;}' >> MenuSettings3.txt
rm -v MenuSettings2.txt
mv -v MenuSettings3.txt MenuSettings.txt

However when I try to run them through multiple folders writing this

for dir in $(find salford_* -type d);do 

sed -e '1r MenuSettings.bak' -e '1,2d' MenuSettings.txt >> MenuSettings2.txt
cat MenuSettings2.txt | sed '/^AppName: */{h;s///;x;}; /^\(AppName_.._..:\).*/{s//\1/;G;s/\n/ /;}' >> MenuSettings3.txt
rm -v MenuSettings2.txt
mv -v MenuSettings3.txt MenuSettings.txt

done

I get the error

sed: MenuSettings.txt: No such file or directory

Which renders all the following commands ineffective, and I end up with a blank text file on the root of my search. MenuSettings.txt is in every directory with the Salford name. And when i test find to echo what its searching for it shows everything fine. Ive tried a number of find methods but to no avail, can anyone help

2
  • 1
    Welcome to SuperUser. You never change into that directory, right?
    – Run CMD
    Feb 4, 2016 at 16:01
  • Thank you, I go into every dir named salford_xxx and edit the file MenuSettings.txt where I need a number of segments replacing, hence sed being used.
    – Eggfoot
    Feb 4, 2016 at 16:09

1 Answer 1

0

for does not change directories for you. You should put ${dir}/ in front of your file references,and change some ' quotes to " to allow shell variable interpretation

for dir in $(find salford_* -type d);do 

sed -e "1r ${dir}/MenuSettings.bak" -e '1,2d' ${dir}/MenuSettings.txt >> ${dir}/MenuSettings2.txt
sed '/^AppName: */{h;s///;x;}; /^\(AppName_.._..:\).*/{s//\1/;G;s/\n/ /;}' ${dir}/MenuSettings2.txt >> ${dir}/MenuSettings3.txt
rm -v ${dir}/MenuSettings2.txt
mv -v ${dir}/MenuSettings3.txt ${dir}/MenuSettings.txt

done

I also removed a "Useless Use of cat". Google that.

3
  • Thanks, this was really helpful. Given that I also had subdirectories in the folders, I put -maxdepth parameter in find too.
    – Eggfoot
    Feb 4, 2016 at 16:46
  • Then please Accept my answer, so my reputation will grow and I can end up ruling the world.
    – waltinator
    Feb 4, 2016 at 16:50
  • Ive made it so!
    – Eggfoot
    Feb 4, 2016 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .