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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use:
sed -e '/^;/d' php.ini
Method 2
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
Method 3
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
Method 4
egrep -v '^;|^$' $file
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.
Method 5
grep -Ev ^'(#|$)' file.txt
Strips all comments and empty lines from file.txt
Updated answer per Yokai’s comment. “Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.” ref: https://www.gnu.org/software/grep/manual/grep.html
Method 6
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]
Method 7
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.
Method 8
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 regexpS, which is a character class matching all non-whitespace characters.
Method 9
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.
Method 10
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
Method 11
Will strip also empty lines
grep -E -v "^s*($|;)" php.ini
Method 12
you can use the following command to save the lines, excluding empty lines and lines starting with # in a new file
cat <file to be read> | egrep -v '^#|^$' > <file to be written at>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0