1

I want to know how to remove the first occurrence of a word in a file.

Example: I want to remove the admin, from the first occurrence only.

account    required       pam_opendirectory.so
account    sufficient     pam_self.so
account    required       pam_group.so no_warn group=admin,wheel fail_safe
account    required       pam_group.so no_warn deny group=admin,wheel ruser fail_safe

I have tried:

sudo sed -i.bak "s/admin,//1" /etc/pam.d/screensaver

But that removes both cases, any ideas?

Technic1an
  • 2,697
  • 5
  • 20
  • 22

2 Answers2

4

Using sed

To remove the first occurrence in the file:

$ sed -e ':a' -e '$!{N;ba;}' -e 's/admin,//1' file
account    required       pam_opendirectory.so
account    sufficient     pam_self.so
account    required       pam_group.so no_warn group=wheel fail_safe
account    required       pam_group.so no_warn deny group=admin,wheel ruser fail_safe

(The above was tested with GNU sed.)

How it works

  • -e ':a' -e '$!{N;ba;}'

    This reads the whole file in at once to the pattern space. (If your file is too large for memory, this is not a good approach.)

  • -e 's/admin,//1'

    This performs the substitution on the first occurrence of admin and only the first occurrence.

Using BSD (OSX) sed

Wintermute reports that BSD sed cannot do branch instructions in one-liners and suggests this alternative for changing the file in place:

sed -i.bak -n '1h; 1!H; $ { x; s/admin,//; p; }' file

This reads the whole file in at once and then does the substitution once the last line has been read.

In more detail:

  • -n

    By default, sed would print each line. This turns that off.

  • 1h

    This places the first line in the hold buffer.

  • 1!H

    For all subsequent lines, this appends them to the hold buffer.

  • $ { x; s/admin,//; p; }

    For the last line ($), this exchanges the hold and pattern buffer so that the pattern buffer now has the complete file. s/admin,// does the substitution and p prints the result.

Using awk

$ awk '/admin/ && !f{sub(/admin,/, ""); f=1} 1' file >file.tmp && mv file.tmp file

This results in:

account    required       pam_opendirectory.so
account    sufficient     pam_self.so
account    required       pam_group.so no_warn group=wheel fail_safe
account    required       pam_group.so no_warn deny group=admin,wheel ruser fail_safe

How it works

  • /admin,/ && !f{sub(/admin,/, ""); f=1}

    For each line, check to see if it contains the word admin, and if the flag f still has its default value of zero. If so, remove the first occurrence of admin, and set the flag to one.

  • 1

    Print each line. 1 is awk's cryptic shorthand for {print $0}.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Gives me this error: sed: 2: "s/admin,//1 ": unexpected EOF (pending }'s) – Technic1an Mar 30 '15 at 21:28
  • @Technic1an Please try the `awk` solution that I just added. For the `sed` approach, I cannot reproduce that. In the command as written there are no double-quotes and there is no space after `1`. From the message, it appears that a `}` is missing but, in the command as written, the braces are balanced. Would you show me exactly what command you are running and the complete output (errors and all) from that command? – John1024 Mar 30 '15 at 21:36
  • 2
    OP is using MacOS X, which comes with BSD sed. With BSD sed, you cannot (or at least I have not found a way) use `b` instructions in one-liners. Try `sed -i.bak -n '1h; 1!H; $ { x; s/admin,//; p; }' file` (that works essentially the same way but uses a different way to assemble the whole file before matching). – Wintermute Mar 30 '15 at 21:38
  • @Wintermute Thanks much! (I have no access to BSD.) I added your approach to the answer. Just one question: doesn't BSD required a space between `-i` and `.bak`? I remember this because I thought that the `-i` options between GNU and BSD were hopelessly incompatible and your version works for me on GNU. – John1024 Mar 30 '15 at 21:45
  • 1
    Either works. BSD sed does not accept just `-i` without an argument, though (but `-i ''` works). By the way: the reason `b` instructions don't work in one-liners in BSD sed is that something like `ba; }; stuff` takes everything after the `b` until the end of the line as a label name and attempts to jump to a label `a; }; stuff`. This is very silly because I don't think it is possible with BSD sed to declare labels that have a semicolon in their name, but I have the error message to prove it: `sed 'ba; stuff'` gives `sed: 1: "ba; stuff": undefined label 'a; stuff'`. – Wintermute Mar 30 '15 at 21:52
  • @Wintermute People have also told me that BSD sed does not accept instructions (outside of braces) chained together with semicolons. Yet, your command has such: `'1h; 1!H; ...`. Does this depend on the sed version? Or, is there a rule for when BSD sed accepts `;`? (Apologies if I am asking too many questions.) – John1024 Mar 30 '15 at 22:06
  • @Wintermute was right it ended up being the "sed -i.bak -n '1h; 1!H; $ { x; s/admin,//; p; }'" ..... The awk seemed to edit it and display it in terminal burn to actual edit the file. But thanks to both of you guys you are awesome! Wintermute can you please explain how it works for me. – Technic1an Mar 30 '15 at 22:07
  • 2
    Chaining commands with `;` in BSD sed is fine. Vis-a-vis `{}`, the thing to know is that with BSD sed, you need a `;` before the `}` that GNU sed doesn't require (`{p}` vs. `{p;}`). As for the code: `1h` copies the current line to the hold buffer if it is the first. `1!H` appends it there if it isn't the first. `$ { ... }` does something if the current line is the last (at that point the whole file is in the hold buffer). `x` swaps hold buffer and pattern space, `s/admin,//` removes the first `admin,` from the PS, `p` prints the PS. The `-n` option disables auto-printing. – Wintermute Mar 30 '15 at 22:12
  • 1
    @Technic1an I added an explanation for Wintermute's command and updated the awk command to change the file in-place. – John1024 Mar 30 '15 at 22:19
0

Using sed, without reading the whole file:

sed 's/admin,//;tl;b;:l;n;bl' file

This waits for a successful substitution and then goes into a loop of printing the rest of the file.

  • s/admin,//;tl;b;: substitute admin with empty string, if substitution occurs jump to label l otherwise print the line and exit (next line will be processed the same way).

  • :l;n;bl: define label l, read line, jump to l.

perreal
  • 94,503
  • 21
  • 155
  • 181