Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to know if there is a way that I could cat file like php.ini and remove all lines starting with ;

For example, if the file contained this:

;   - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE
;
;   - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR
;
;   - Show all errors except for notices
;
error_reporting  =  E_ALL & ~E_NOTICE

and I ran the correct command cat | {remove comments command}, then I would end up with:

error_reporting  =  E_ALL & ~E_NOTICE

Note - I assumed that cat would be the best way to do this but I'm actually fine with the answer using another utility like awk, sed, egrep, etc.

share|improve this question
    
What about something like error_reporting = E_ALL & E_NOTICE ; Show all errors, except for notices? Should the comment be removed in that case as well? –  Michael Kjörling Dec 7 '11 at 8:29
    
@MichaelKjörling - I'm really fine with just lines starting with comments being removed –  cwd Dec 7 '11 at 14:16
    
cat is the tool to concatenate files. grep is the tool to filter lines based on patterns. sed and awk can also modify those lines. –  Stéphane Chazelas Jan 28 '13 at 12:32

11 Answers 11

up vote 20 down vote accepted

You can use:

sed -e '/^;/d' php.ini
share|improve this answer

You don't need to pipe a file thru grep, grep takes filename(s) as command line args.

grep -v '^#' file1 file2 file3

will print all lines EXCEPT those that begin with a # char. you can change the comment char to whatever you wish.

If you have more than one comment char (assuming its at the beginning of a line)

egrep -v '^(;|#|//)' filelist
share|improve this answer
    
Another one I've done that always stuck with me was grep '^[^;]' filename. I can't speak to its portability though! –  JodieC Dec 8 '12 at 13:57
    
@JodieC, that's portable but also removes empty lines (Which is often desired). The standard equivalent of egrep is grep -E. One can also use grep -ve '^[;#]' -e '^//' –  Stéphane Chazelas Jan 28 '13 at 12:30

egrep can save you the use of cat. In other words, create less processes (egrep vs cat+egrep) and use less buffers (pipe from cat to egrep vs no pipe).

It is generally a good idea to limit the use of cat if you simply want to pass a file to a command that can read it on its own.

With this said, the following command will remove comments, even if they are indented with spaces or tabs:

egrep -v '^[[:blank:]]*;' file.ini
share|improve this answer
    
Funny that you combine the newer [[ character class ]] regex format with the use of the egrep command deprecated for at least a decade or so. –  mikeserv Mar 13 at 16:25
cat $file | egrep -v '^;|^$'

that will exclude lines that begin with the ';', and empty lines.

in regex, ^ indicates the beginning of a line, and $ the end of a line, so ^$ specifies lines where the start of line character and the end of line character are right next to each other.

share|improve this answer
    
so if i'm reading this right this can remove commented lines, but also blank lines? –  cwd Dec 7 '11 at 5:26
1  
@cwd Yes. I'm not sure why he included both, but if you only want to remove commented lines just use egrep -v '^;' –  Michael Mrozek Dec 7 '11 at 5:44
4  
egrep likes files too (less processes and buffers used), and a little bonus would be to remove indented comments too: egrep -v '^[[:blank:]]*;' file.ini –  nrolans Dec 7 '11 at 6:55
9  
We need a "useless use of cat" badge. –  Simon Richter Dec 7 '11 at 11:02
    
@nrolans - looks like a popular comment, why not make it an answer? –  cwd Dec 7 '11 at 14:13

A simple awk one-liner awk '/^;/{next}1' input_file should do the trick.

[jaypal:~/Temp] cat file
;   - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE
;
;   - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR
;
;   - Show all errors except for notices
;
error_reporting  =  E_ALL & ~E_NOTICE

[jaypal:~/Temp] awk '/^;/{next}1' file
error_reporting  =  E_ALL & ~E_NOTICE
[jaypal:~/Temp] 
share|improve this answer
3  
Correct, but verbose. Just awk '!/^;/' input_file is enough. –  manatwork Dec 8 '11 at 9:32
    
Thanks @manatwork. You are right! :) –  jaypal singh Dec 8 '11 at 15:00

As well as Jaypal, I also most probably would use awk for these purposes. It worse to mention that perl is sometimes quite handy for such purposes:

cat data.txt | perl -lne "print unless /^;/"

Perl regexps are more powerful compared to awk's one and sometimes you might need them.

share|improve this answer
    
+1 for perl, although the cat and the -l are both redundant, so a simpler invocation is perl -ne 'print unless /^;/' data.txt –  Simon Whitaker Dec 8 '11 at 3:59
    
@Simone Whitaker, yes, you are right - it is just a habit to write it the way i write, and it worth to mention it. –  shabunc Dec 8 '11 at 4:18
1  
Sure thing. In fact, I think cat works fine in these examples if you consider it as a proxy for the more generic "anything generating text on STDOUT". Unix pipes are the best thing since sliced bread, imho. :) –  Simon Whitaker Dec 8 '11 at 9:43

An elaboration on @shabunc's answer, this uses Perl to strip comments (including inline comments), then print any lines containing anything other than whitespace.

$ perl -ne 's/;.*//; print if /\S/' data.txt

Explanation:

  • s/;.*// uses the substitution operator (s/<regex>/<replacement>/) to replace instances of a semi-colon and everything following it on a line with the empty string.
  • print if /\S/ prints the line if it matches the regexp \S, which is a character class matching all non-whitespace characters.
share|improve this answer

Here's one that I use, just substitute ';' with the comment character (e.g. '#' for many UNIX service configuration files):

grep -Ev '^[[:space:]]*;|^$' chan_dahdi.conf.sample | sed 's/;.*$//'

That gets rid of all whole-line comments (even if they have leading whitespace), and any comments that end non-comment lines, and succinctly removes blank lines from the output as well. This may be possible without the pipeline (my sed- or awk-fu is admittedly not great), but it's so easy for me to understand (and remember), I figured I'd post it here.

share|improve this answer

Example show only lines + do not show new lines or emtpy lines:

$ egrep -v '^(;|#|//)' /etc/ssh/sshd_config | tr '\n' ' '

 Protocol 2    SyslogFacility AUTHPRIV      PasswordAuthentication yes  ChallengeResponseAuthentication no   GSSAPIAuthentication yes GSSAPICleanupCredentials yes  UsePAM yes  AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS  X11Forwarding yes   Subsystem sftp    /usr/libexec/openssh/sftp-server 

OR

$ egrep -v '^(;|#|//|$)' /etc/ssh/sshd_config    

Protocol 2
SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem   sftp    /usr/libexec/openssh/sftp-server
share|improve this answer
    
The use of egrep is long deprecated. grep -E is the command you're looking for here. –  mikeserv Mar 13 at 16:23
egrep -v ^'(#|$)' file.txt

Strips all comments and empty lines from file.txt

share|improve this answer
2  
You should consider expanding your answer with a little more information for the uninitiated (e.g., exactly what the regex you use does). –  HalosGhost Jul 26 at 21:01

Will strip also empty lines

grep -E -v "^\s*($|;)" php.ini
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.