7

I want to change a word in a .docx file using a shell command. I tried using the sed command, but it is not working. Does anyone know a solution for this?

For example, I want to change a word (e.g. exp5) and replace that with another (exp3) in the file exo.docx.

| improve this question | | | | |
  • sorry , exp5 with exp3 – deepshikha Oct 17 '16 at 6:44
  • 3
    You can edit your questions. – Raphael Ahrens Oct 17 '16 at 6:55
  • 7
    .docx is actually a zip format archive. Extract the word/document.xml file from it, apply the sed to it, and add it back to the archive. Wikipedia has more information on the format. – Nominal Animal Oct 17 '16 at 6:56
  • 2
    @Dmitry there are lots of valid reasons that would take us far off topic. I'm sure google will help you – Darren H Oct 17 '16 at 13:36
  • 1
    @DarrenH I didn't ask for "lots of valid reasons", just for the reason the OP had. – Dmitry Grigoryev Oct 17 '16 at 13:42
19

So, you want to replace things in a brand-specific format? At the first look it looks bad, but the new docx format is a bit better for that than the old doc format, because it's actually a ZIP file containing XML files.

So the answer lies in unzipping it, then you'll have to rummage through the files and figure out on which one to call sed and zip it up again.

Check out the file word/document.xml in the ZIP file.

| improve this answer | | | | |
  • a side note: "mc" (midnight commande) is able to open & go "inside" the directories and files of a zip file, seamlessly. You should be able to start mc (mc) , then navigate to the file, Enter on it to "enter" its content, then find the relevant file you need to change, edit it (F3, if my memory is correct? the menu should be at the bottom of your screen) save it, and exit (navigate up until you "exit" the .zip file) : it will ask if you want to save the modifications. For this to work you may have to tell "mc" to handle .docx and .xlsx files as zip files, though – Olivier Dulac Oct 17 '16 at 16:25
  • 4
    I have nightmares about sed on XML.... – cat Oct 17 '16 at 17:31
  • 4
    @cat something like this, I assume – Digital Trauma Oct 17 '16 at 17:37
  • @DigitalTrauma Yes, rather like that, although sed can describe non-regular grammars, right? So maybe it's not quite so bad (but use a parser!) – cat Oct 17 '16 at 18:31
  • 1
    Replacing a single word should probably be ok, if it doesn't occur as a tag name, but in general, using regex on xml is inappropriate and incorrect from the grammar point of view. I don't know what OP wants - if it needs to be an automatic script, then using a file manager and text editor is out of a question. – orion Oct 18 '16 at 6:37
3

try this script:

FILE=$1    
RETPATH=`pwd`    
rm -rf /var/tmp/docx    
mkdir /var/tmp/docx    
cp $FILE /var/tmp/docx
cd /var/tmp/docx    
mkdir tmp
unzip $FILE -d tmp
cd tmp/word
sed -i "s/${2}/${3}/" document.xml
cd ..
zip -r ../${FILE} *
cp /var/tmp/docx/${FILE} ${RETPATH}
cd $RETPATH
rm -rf /var/tmp/docx 

and call the script as follows:

./repdocx FILE_NAME OLD_STRING NEW_STRING
| improve this answer | | | | |
  • If using OSX don't forget to add sed -i '' – Ian Newland Dec 21 '17 at 15:40
1

Modifying and cleaning up schraubenkarl's answer (must be in the directory of the docx):

find_and_replace() {
    FILE=$1
    FIND=$2
    REPLACE=$3

    unzip $FILE -d tmp #unzip
    sed -i '' -e "s/${FIND}/${REPLACE}/g" tmp/word/document.xml #find/replace
    cd tmp && zip -r ../$i * && cd .. #zip
    rm -rf tmp
}
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.