-1

I'm trying to use grep to remove the comments from a file, smb.conf to be specific, and bash is giving me an error. I found this question How to remove all comments from a file? that gives the syntax for grep to do this, but it doesn't overwrite the file, it just prints it to the terminal. I would like to effectively overwrite the file. This is what's going on:

 $ sudo grep -o '^[^#]*' /etc/samba/smb.conf >> /etc/samba/smb.conf
-bash: /etc/samba/smb.conf: Permission denied

I have done something like this before, but then I was following an online guide and I vaguely remember having to start up bash before I could grep. Obviously, I can't find that guide again.

How do you use grep to strip out the comments from a file?

I'm working on a Raspberry Pi B+ running Raspbian Jessie.

  • 1
    you're getting the permissions error because the elevated sudo permissions only last until the >> redirection, at which point it is your user shell. redirect it to a file you can write to, then sudo mv it; or sudo vi the file and pipe its contents to the grep – Jeff Schaller Feb 09 '16 at 00:54
  • In any case, reading and writing to the same file would not work as intended. sed is a better approach (using -i) – Thomas Dickey Feb 09 '16 at 00:55
  • If one of the 3 answers below worked for you, please accept one of them using the check-mark. Thank you! – Jeff Schaller Feb 11 '16 at 17:21

3 Answers3

2
sed -i -e 's/#.*$//' -e '/^$/d' inputFile

in addition to removing all comments, it removes empty lines as well. I know you asked it with grep, but I thought I'd suggest this one. Functionally it is the same or even better by removing empty lines, compacting the size of the file, which I am assuming is your target.

EDIT: yes replace # with ; and you should be good. At least on my CentOS 6 box it worked. You might need to put a \ in front of the ; if it complains about a invalid character or something.

MelBurslan
  • 6,966
  • That worked. If I want to remove some stuff commented out with an ;, I just change the # in the above sed command? – YetAnotherRandomUser Feb 09 '16 at 01:01
  • Mel types faster than I do! The answer to your question is 'yes' – Jeff Schaller Feb 09 '16 at 01:02
  • You would not need to escape the ; here, as we're inside quote marks. Only thing I'd suggest is that there's no need to $ anchor after a .* The * is greedy and will gobble to EOL by itself. – Jeff Schaller Feb 09 '16 at 01:26
  • Normally, I wouldn't escape either. As I indicated, the command works without any problem with either # or ; as the comment delimeters, on my CentOS 6 server. But since user indicated his target platform is raspbian, I thought the shell interpretation might be different as I haven't had the pleasure of dealing with any RasPi server yet. – MelBurslan Feb 09 '16 at 01:34
2

sed is one option that can "directly" edit a file (by making a temporary copy of it). This command will tell sed to edit /etc/samba/smb.conf -i in place, and to -n not print lines by default.

sudo sed -i -n '/^[^#]/ p' /etc/samba/smb.conf

The lines we tell it to 'p' print are the ones that match your grep regex, which I modified only slightly to remove the extraneous trailing *. Here, the regex says "match lines that start with anything other than an octothorpe".

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • For anyone stumbling for a solution to remove # and ; from smb.conf: sudo sed -i -n '/^[^#;]/ p' /etc/samba/smb.conf does just that, or (safer) sed -n '/^[^#]/ p' /etc/samba/smb.conf to inspect before changes.

    This is the only solution where the addition of ; as a comment character is clear and easy, or working at all.

    – emk2203 Oct 12 '23 at 07:09
2

The pattern should be specific, to avoid deleting lines containing a '#':

sudo sed -r -i 's/^[[:space:]]*#.*$//' /etc/samba/smb.conf

The question did not actually ask to delete blank lines; this command leaves them (as well as blanked lines resulting from the substitution).

Like the other solutions, this relies on the -i option, which is non-standard (see POSIX), as well as -r (also non-standard). The use of the options depends on the platform, of course.

Thomas Dickey
  • 76,765