Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Escaping grep

4 views
Skip to first unread message

Michael F. Stemper

unread,
Feb 21, 2021, 4:36:04 PM2/21/21
to
I wanted to see which LaTeX packages I was using for a particular
project, so I did:

user@host$ grep -h "\\usep" *tex | sort | uniq
% \usepackage{array} % for math mode columns in tabular
% \usepackage{graphicx}
% \usepackage{longtable}
% \usepackage{mathrsfs} % for mathscr
% \usepackage{tikz-cd} % for commutative diagrams
\usepackage{amsmath} % for implication and align*
\usepackage{amssymb} % for black-board set names
\usepackage{fancyhdr}
\usepackage{geometry}
\usepackage{parskip}
user@host$

However, that showed unused packages as well. So I tried
anchoring my grep to the beginning of the line, and got:

user@host$ grep -h "^\\usep" *tex | sort | uniq
user@host$

Why isn't this working? What am I overlooking? How
should I have said it?

--
Michael F. Stemper
The name of the story is "A Sound of Thunder".
It was written by Ray Bradbury. You're welcome.

Lewis

unread,
Feb 22, 2021, 1:37:10 AM2/22/21
to
In message <s0ujo2$1i0$1...@dont-email.me> Michael F. Stemper <mste...@gmail.com> wrote:
> user@host$ grep -h "^\\usep" *tex | sort | uniq
> user@host$

This is a grep question, not a bash question.

You need to use egrep or grep -E if you want to use ^ I believe.


--
"Are you pondering what I'm pondering?"
"I think so, Brain! But do I have what it take to be the 'Lord of the
Dance'?"

bodiccea

unread,
Apr 17, 2021, 9:36:22 AM4/17/21
to
On Sun, 21 Feb 2021 15:35:55 -0600, "Michael F. Stemper"
<mste...@gmail.com> wrote:

> I wanted to see which LaTeX packages I was using for a particular
> project, so I did:
>
> user@host$ grep -h "\\usep" *tex | sort | uniq
> % \usepackage{array} % for math mode columns in tabular
> % \usepackage{graphicx}
> % \usepackage{longtable}
> % \usepackage{mathrsfs} % for mathscr
> % \usepackage{tikz-cd} % for commutative diagrams
> \usepackage{amsmath} % for implication and align*
> \usepackage{amssymb} % for black-board set names
> \usepackage{fancyhdr}
> \usepackage{geometry}
> \usepackage{parskip}
> user@host$
>
> However, that showed unused packages as well. So I tried
> anchoring my grep to the beginning of the line, and got:
>
> user@host$ grep -h "^\\usep" *tex | sort | uniq
> user@host$
>
> Why isn't this working? What am I overlooking? How
> should I have said it?

There is no issue with the grep expression itself. The problem is
with bash handling of strings: You use " (double quotes) around
your expression, asking bash to interpret the contents.

bash will replace "^\\usep" with "^\usep" *before passing it to grep*.
grep then will remove special meaning for following char (u) - no
special meaning in this case.
The result is that grep will just look for lines starting with "u".

You have 2 solutions:
1) use quotes instead of double quotes: '^\\usep', so that bash does
not change anything.
2) using double quotes, force bash to preserve 2 backslash with the
string "^\\\\usep"

Sorry for late answer, I just joined this group.

And yes, it is a pure 'bash' question, not a 'grep' question as I saw in
another answer :)

bodi.

bodiccea

unread,
Apr 17, 2021, 11:58:40 AM4/17/21
to
On Sat, 17 Apr 2021 15:36:19 +0200, bodiccea <bodi...@gmail.com> wrote:
> You have 2 solutions:
> 1) use quotes instead of double quotes: '^\\usep', so that bash does
> not change anything.
> 2) using double quotes, force bash to preserve 2 backslash with the
> string "^\\\\usep"

As a general rule, when you are unsure about "real" strings sent to a
program (like grep in your case), you can always test them with printf,
I do it sometimes with complicated patterns.

Examples:
# your initial string
$ printf "%s\n" "^\\usep"
^\usep <-- passed to grep, incorrect

# with quotes
br@lorien:~/.claws-mail$ printf "%s\n" "^\\\\usep"
^\\usep <-- passed to grep, correct

# with double quotes
br@lorien:~/.claws-mail$ printf "%s\n" '^\\usep'
^\\usep <-- passed to grep, correct

bodi.

Michael F. Stemper

unread,
Apr 17, 2021, 12:06:43 PM4/17/21
to
Thank you. I've been using various *IX variants for three decades now
and I still can't get the single-quote v double-quote distinction
through my head.

> You have 2 solutions:
> 1) use quotes instead of double quotes: '^\\usep', so that bash does
> not change anything.

Works fine, lasts a long time!

> Sorry for late answer, I just joined this group.
>
> And yes, it is a pure 'bash' question, not a 'grep' question as I saw in
> another answer :)

Thought so!

--
Michael F. Stemper
Outside of a dog, a book is man's best friend.
Inside of a dog, it's too dark to read.

Michael F. Stemper

unread,
Apr 17, 2021, 12:08:37 PM4/17/21
to
That's a really good idea. Thanks for the pro-tip. (But I think
that your last comment has a typo.)

bodiccea

unread,
Apr 17, 2021, 1:21:30 PM4/17/21
to
On Sat, 17 Apr 2021 11:08:36 -0500, "Michael F. Stemper"
<mste...@gmail.com> wrote:
> > # with quotes
> > br@lorien:~/.claws-mail$ printf "%s\n" "^\\\\usep"
> > ^\\usep <-- passed to grep, correct
> >
> > # with double quotes
> > br@lorien:~/.claws-mail$ printf "%s\n" '^\\usep'
> > ^\\usep <-- passed to grep, correct
>
> That's a really good idea. Thanks for the pro-tip. (But I think
> that your last comment has a typo.)

indeed, the 2 comments are messed up :*)

--
Dragons Don't Believe In You Either.

0 new messages