Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all of the words appearing between `\word{}` in BASH?

Tags:

bash

I have a file like this:

This \word{is} some text.
This is some \word{more text}.
\word{This} is \word{yet} some more \word{text}.

I need to create a list of all of the text that appears between \word{ and the matching closing brace, }, e.g.:

is
more text
This
yet
text
  • The opening and closing braces always appear on the same line, never across multiple lines.
  • Other braces are present in the document, but none appear inside \word{}.

How can I print a list of all of the text appearing in \word{}?

like image 275
Village Avatar asked Nov 28 '22 11:11

Village


1 Answers

It seems you're handling a TeX file... so why not use TeX to do this directly? Then you'll be sure there won't be any problems and side effects, e.g.,

\word {there's a space between \verb=\word= and the curly bracket}

this would still work! It will still work for multi-line stuff:

\word{this is
    a multiline stuff \emph{and you can even add more groupings in it,}
    it'll still work fine!}

In your (La)TeX preamble, just add:

\newwrite\file
\immediate\openout\file=output.txt

\def\word#1{\immediate\write\file{#1}}

or use \newcommand if you're using LaTeX and not plainTeX.

You can also put the \immediate\write\file{#1} inside your \word definition macro. If you don't have access to the \word macro (e.g., it's in a class or style file) you can:

\let\oldword\word
\def\word#1{\immediate\write\file{#1}\oldword{#1}}

Hope this helps!

like image 180
gniourf_gniourf Avatar answered Dec 23 '22 08:12

gniourf_gniourf