[augeas-devel] How to match/delete commented lines with specific text?

Dan Mahoney (Gushi) augeas at gushi.org
Mon Nov 16 11:24:29 UTC 2020


On Mon, 16 Nov 2020, Raphaël Pinson wrote:

Hey Raphael (sorry if I miss the diacritics in your name).  Thanks for 
replying -- this doesn't seem to be a very busy list.  I've given it a try 
and something is still not clicking.  I think it stems from the fact that 
comments are somehow "special".

> Hi Dan,
> 
> You're not too far. You just missing the right operator for `regexp()`: https://github.com/hercules-team/augeas/wiki/Path-expressions#tips--tricks
>
>   /files/etc/hosts/*[ipaddr =~ regexp("192\..*")]

Yes, I did find that.  Let's use /etc/hosts as an example: The XML dump 
for a true /etc/hosts entry on my system is:

    <node label="2">
       <node label="ipaddr">
         <value>127.0.0.1</value>
       </node>
       <node label="canonical">
         <value>localhost</value>
       </node>
       <node label="alias">
         <value>localhost.my.domain</value>
       </node>
     </node>

So I can match on "ipaddr" or "canonical" or "alias".

With comments, there's NO SUB NODE to match on:

     <node label="#comment">
       <value>10.0.0.2           myname.my.domain myname</value>
     </node>

And the label for all nodes is just #comment (see above how /etc/hosts 
still has a "label" (2) that you can refer to it by, even if the file 
doesn't expose it -- lines in /etc/hosts don't start with numbers like 
this.

When constructing the regex you provided, "ipaddr" refers to the node 
label, and regexp("192\..*") to the value.

When using the only label I have (#comment), I get errors.

augtool> match /files/etc/hosts/[#comment = regexp(".*")]
error: Invalid path expression
error: empty name
/files/etc/hosts/|=|[#comment = regexp(".*")]

augtool> match /files/etc/hosts/*[label() = '#comment']
/files/etc/hosts/#comment[1] = $FreeBSD: releng/11.4/etc/hosts 109997 
2003-01-28 21:29:23Z dbaker $
/files/etc/hosts/#comment[2] = Host Database
/files/etc/hosts/#comment[3] = This file should contain the addresses and 
aliases for local hosts that

Hrmm, so far as good -- at least this enumerates comments with an 
expression...But then:

augtool> match /files/etc/hosts/*[label() = '#comment'][value() = 
regexp(".*")]
error: Invalid path expression
error: empty name
/files/etc/hosts/*[label() = '#comment'][|=|value() = regexp(".*")]

I'm not sure what "|=| means in Augeas.  I think it means "everything made 
sense up to here".

Could you give me a simple example that would, say, match any comment with 
the letter A in it?

-Dan

> 
> Raphaël
> 
> 
> On Sat, Nov 14, 2020 at 10:39 PM Dan Mahoney (Gushi) <augeas at gushi.org> wrote:
>       All,
>
>       We're trying at the day job to use Augtool to clean up some
>       in-the-os-by-default comments.  Under FreeBSD, this is what you might find
>       in a stock /etc/pam.d/sshd:
>
>       match /files/etc/pam.d/sshd/*
>       /files/etc/pam.d/sshd/#comment[1] = $FreeBSD: releng/11.4/etc/pam.d/sshd
>       197769 2009-10-05 09:28:54Z des $
>       /files/etc/pam.d/sshd/#comment[2] = PAM configuration for the "sshd"
>       service
>       /files/etc/pam.d/sshd/#comment[3] = auth
>       /files/etc/pam.d/sshd/1 = (none)
>       /files/etc/pam.d/sshd/2 = (none)
>       /files/etc/pam.d/sshd/#comment[4] = auth                sufficient
>       pam_krb5.so             no_warn try_first_pass
>       /files/etc/pam.d/sshd/#comment[5] = auth                sufficient
>       pam_ssh.so              no_warn try_first_pass
>       /files/etc/pam.d/sshd/3 = (none)
>       /files/etc/pam.d/sshd/#comment[6] = account
>       /files/etc/pam.d/sshd/4 = (none)
>       /files/etc/pam.d/sshd/#comment[7] = account     required pam_krb5.so
>       /files/etc/pam.d/sshd/5 = (none)
>       /files/etc/pam.d/sshd/6 = (none)
>       /files/etc/pam.d/sshd/#comment[8] = session
>       /files/etc/pam.d/sshd/#comment[9] = session     optional        pam_ssh.so
>       want_agent
>       /files/etc/pam.d/sshd/7 = (none)
>       /files/etc/pam.d/sshd/#comment[10] = password
>       /files/etc/pam.d/sshd/#comment[11] = password   sufficient pam_krb5.so
>       no_warn try_first_pass
>       /files/etc/pam.d/sshd/8 = (none)
>
>       And what I can't seem to find out how to do is match comments by name. For
>       example, if I wanted to match any (or delete) any comment that include
>       pam_krb5, how would I do it?
>
>       As a comment, the usual parsing rules don't apply, and in all the wiki
>       docs I can't find a clean example about "This is how you match a string by
>       name".  (Call this a docbug maybe?)
>
>       Help?
>
>       -Dan
>
>       PS, here's some of the things I've been trying -- am I just barking up the
>       wrong tree here?
>
>       augtool> match /files/etc/pam.d/sshd/#comment[ value = freebsd ]
>          (no matches)
>       augtool> match /files/etc/pam.d/sshd/#comment[ value = "freebsd" ]
>          (no matches)
>       augtool> match /files/etc/pam.d/sshd/#comment[ value() = "freebsd" ]
>       error: Invalid path expression
>       error: empty name
>       /files/etc/pam.d/sshd/#comment[ |=|value() = "freebsd" ]
>       augtool> match /files/etc/pam.d/sshd/#comment[ value = regex("freebsd") ]
>       error: Invalid path expression
>       error: empty name
>       /files/etc/pam.d/sshd/#comment[ value = |=|regex("freebsd") ]
>       augtool> match /files/etc/pam.d/sshd/#comment[ value = regexp("freebsd") ]
>       error: Invalid path expression
>       error: type error
>       /files/etc/pam.d/sshd/#comment[ value = regexp("freebsd") ]|=|
>       augtool> match /files/etc/pam.d/sshd/#comment[ value = regexp("freebsd") ]
>       error: Invalid path expression
>       error: type error
>       /files/etc/pam.d/sshd/#comment[ value = regexp("freebsd") ]|=|
>       augtool> match /files/etc/pam.d/sshd/#comment[ regexp("freebsd") ]
>       error: Invalid path expression
>       error: type error
>       /files/etc/pam.d/sshd/#comment[ regexp("freebsd") ]|=|
>       augtool> match /files/etc/pam.d/sshd/#comment[ regexp(freebsd) ]
>       error: Invalid path expression
>       error: type error
>       /files/etc/pam.d/sshd/#comment[ regexp(freebsd) ]|=|
>       augtool> match /files/etc/pam.d/sshd/#comment[ regexp(/freebsd/) ]
>       error: Invalid path expression
>       error: empty name
> 
>
>       --
>
>       --------Dan Mahoney--------
>       Techie,  Sysadmin,  WebGeek
>       Gushi on efnet/undernet IRC
>       FB:  fb.com/DanielMahoneyIV
>       LI:   linkedin.com/in/gushi
>       Site:  http://www.gushi.org
>       ---------------------------
>
>       _______________________________________________
>       augeas-devel mailing list
>       augeas-devel at redhat.com
>       https://www.redhat.com/mailman/listinfo/augeas-devel
> 
> 
> 
> --
> Raphaël PinsonInfrastructure Developer & Training Leader
> +41 21 619 10 65
> Camptocamp SA
> EPFL Innovation Park, Bât A
> 1015 Lausanne
> 
> www.camptocamp.com
> 
>

-- 

"Is Gushi a person or an entity?"
"Yes"

-Bad Karma, August 25th 2001, Ezzi Computers, Quoting himself earler, referring to Gushi

--------Dan Mahoney--------
Techie,  Sysadmin,  WebGeek
Gushi on efnet/undernet IRC
FB:  fb.com/DanielMahoneyIV
LI:   linkedin.com/in/gushi
Site:  http://www.gushi.org
---------------------------


More information about the augeas-devel mailing list