When i am trying to run below code it is giving error of cp: target "Featurespath" is not a directory

I have tried multiple option but not working.

Featurespath=/permanent/jag/media-*/*/print/cooked/*Features.xml
for file in $(ls $Featurespath);
do
cat $Featurespath | sed "/pB-/s/Direction=\"unidir\"/Direction=\"bidir\"/" $Featurespath > /permanent/jag/temp.xml
cp -rf /permanent/jag/temp.xml $Featurespath
rm /permanent/jag/temp.xml
done

i want modified xml to be pasted in same xml file.

1 Answers

1
Hai Vu On

The error you received was because of the cp line: bash expands$Featurespath into a list of files. When cp sees more than 2 parameters, it assumes the last parameter to be a directory, which is not in this case. Here is my suggested fix:

Featurespath=/permanent/jag/media-*/*/print/cooked/*Features.xml
for file in $Featurespath
do
    sed "/pB-/s/Direction=\"unidir\"/Direction=\"bidir\"/" "$file" > /permanent/jag/temp.xml
    mv -f /permanent/jag/temp.xml "$file"
done

Notes

  • Do not use ls: bash can expand the wildcards just fine
  • Within the loop, you are now dealing with individual files $file, not the list of file $Featurespath
  • Do not need to use the cat command, the sed command can take a file name
  • sed has an inline editing option, which eliminate the need for temp file. You might want to look into it.
  • Replace cp/rm combination with mv
  • Ultimately, like others have said, sed is not the right tool to edit XML contents, but it might work for simple cases