16

Grep in Microsoft Word?

I'd like to pull all lines with a given string from a word document. In unix world... grep does this without a glitch. Windows is less than obvious for me.

0

7 Answers 7

12

With Cygwin (or access to a Linux machine) you could

antiword file.doc | grep "my phrase"

or

catdoc file.doc | grep "my phrase"

There are lots of command-line file format converters out there to grep in a similar fashion.

Purely in-Word solution could be to Ctrl+F (Find), and then Find All - however, I'm not sure if all versions of MS Word have Find All button.

4
  • 2
    When I saw the question title I thought "Ha! That'd be nice, wouldn't it". Never again should I underestimate GNU programmers.
    – Phoshi
    Nov 14, 2009 at 0:08
  • 1
    The most recent version of catdoc segfaults on every .doc/.docx file I give it, and antiword just tells me my document "is not a Word Document". Do you know of any other options?
    – detly
    Nov 1, 2014 at 1:53
  • 1
    Nothing that I've used... Quick search shows that docx2txt exists in Debian repositories - might work. I'd also look into the OpenOffice/LibreOffice command-line format conversion utility (unoconv), which could be used for the same purpose.
    – chronos
    Nov 11, 2014 at 18:56
  • 1
    @detly antiword doesn't work with .docx files, only the older MS Word formats. That's probably why it didn't recognise your document.
    – AJM
    Sep 22, 2020 at 17:03
3

I know this sounds primitive, but what's stopping you from saving the file as .txt and then ripping it apart to your liking.

1
  • 6
    Having hundreds of them to do it for, is what.
    – tchrist
    Jan 30, 2013 at 16:42
2

Not got enough rep to comment but I can see this doc vs docx issue discussed so anyone chasing the thread (like I was) may find this helpful.

You do not need a special tool for docx files. docx are zipped XML files.

To extract and strip the XML try something based on

unzip -p "*.docx" word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'

from command line fu

1

What does "line" mean in a Word context? The displayed line, which changes if you do anything to the page formatting? The paragraph? Something else?

You can do a bunch of stuff with Word's find-and-replace functions, including changing the formatting and other non-obvious things, but all of them will only act on the find-what text itself, not on any surrounding text.

1
  • grep's got that regex lovin', though!
    – Phoshi
    Nov 14, 2009 at 0:13
1

There is support for MS documents - Word, PowerPoint, Excel - in CRGREP which I've developed as a free opensource tool. It also greps other hard to search stuff like database tables, images, audio, archives, PDF and combinations of these. Have fun.

0

PowerGREP will do exactly that for you, and fast - but not free. It's worth every penny, though, in my opinion. Plus, there is a 30-day free trial.

Screenshot of PowerGREP searching through a Word file

0

Fast, free, open-source, and cross-platform solution: https://github.com/phiresky/ripgrep-all

You must log in to answer this question.