Trying to setup autofs

Btw, if you just want to delete a certain line, the following expression would work:
sed -i '/expression/d' filename
This removes all lines containing “expression” no matter where.

In the case the line to be removed is immediately starting with “cifs” it would look like this:
sed -i '/^cifs/d' /etc/auto.master

In the case the line starting with “cifs” has multiple empty spaces before the expression it would look like this:
sed -i '/^ *cifs/d' /etc/auto.master
(Notice the empty space between the circumflex and asterisk. The asterisks means “any number of the sign before” which in this case is an empty space.)

For testing purposes, you can leave “-i” and it won’t overwrite the file, only giving standard output to the shell.

Be careful using sed. Its extremely easy to screw everything up. :wink:

What if I want to delete a line starting with “/cifs” that could be followed by a space or a tab, but not another word character. That is, either of the following:

/cifs<space>
/cifs<tab>

But not the following:

/cifscal

And not a line that simply contains the word “cifs” such as something like the following comment:

# cifs is another name for microsoft's smb protocol.

:slightly_smiling_face:

1 Like

Challenge accepted.
I’d come to this solution:
sed '/^[ \t]*\/cifs[ \t]\{1,\}/d' testile.txt

It means
^[ \t]* when it starts with any number of tabs or empty spaces (both none or many),
\/cifs then matching the exact term “/cifs” (the slash is escaped by the backslash)
[ \t]\{1,\} followed by at least one tabs or spaces
d then delete the entire line.
The slashes around just enclose the pattern, the regular expression.

This would at least work with a testfile like this:

Start
/cifs Leerzeile
/cifs	Tab
/cifscal
 /cifs	Eingerückt
# cifs is another name for microsoft's smb protocol.
End

Frankly, I still try to understand your solution.
This is new to me. → \%^/cifs\b% d
Haven’t seen it yet, and this however works too, except that part with the leading spaces.
Really interesting. Would you please explain it to me? :pleading_face:

@zany130
Sorry for being a bit of-topic.
How are the mounts doing? :upside_down_face:

If what you are trying to match happens to contain forward slashes, then you can use a different delimiter so that you don’t have to escape all the forward slashes in your pattern.

That is, if you are trying to match something like /etc/pki/tls/certs, you can use \%/etc/pki/tls/certs% instead of /\/etc\/pki\/tls\/certs/. The character after the first slash can be any character. When that character is seen again, it terminates the pattern. Admittedly, if there is only one slash to be escaped, then just escaping it rather than changing the delimiter is perfectly reasonable.

The \b is a special escape that matches any word boundary. It means that the next character (or the previous character) must not be a word character. So something like foo\b will match “foo=” or "foo " but not “food” (“d” is a word character).

4 Likes

Ah, i see, really cool. Didn’t know that. Thank you!

Edit
I just was really confused by your command. I read a 200 pages book once more which explains sed in every detail but this \% and b% really confused me and was not mentioned at all in said book. So finally this is a mixed in bash function. Learned this once never used it, that however is really handy. Sometimes I’m so narrowed to such a single thing like this. Thanks anyway.

1 Like

That sed syntax was new to me too. Thank you for the explanation @glb.
I also found it described in the GNU sed manual here:

1 Like

Glad you found it useful. This information (and much more) can also be found in the man pages:

$ man sed
$ man perlre

The perlre manpage is a particularly good resource if you really want to know all the regular expression codes. I’ve been using them for over 20 years and I still find new tricks there from time to time. :slightly_smiling_face:

4 Likes

True, you never stop learning.