0
Thanks

A few words of thanks would be greatly appreciated.

Linux - Displaying a file without the commentary lines




It may be needed to view a file without displaying the numerous commentary lines attached to it (especially for the configuration files of your OS) and also considering removal of the empty lines.

Grep

  • Making use of "egrep" (or "grep-E"):
    • egrep -v '^(#|$)'  /etc/samba/smb.conf 
    • grep -E -v '^(#|$)'  /etc/samba/smb.conf
  • Lines starting with a hash (#) or the ending symbol dollar($) shall not be displayed.
  • In this case the delimiter comment is not placed at the start of line but behind (space or tab), but you can change your expression as follows:
    • grep -E -v '^(#|;|$|[ ]*#)' /etc/samba/smb.conf

Sed

  • Making use of sed
    • sed -e '/^[ ]*#/d' -e '/^$/d' /etc/samba/smb.conf
  • Here it removes firstly, lines begining with a space or a pound sign, then removes all blank lines.
  • You can improve the expression like:
    • sed -e '/^[ ]*#/d' -e '/^[ ]*;/d' -e '/^$/d' /etc/samba/smb.conf

Perl

  • Making use of perl.
  • Making use of perl will imply,considering the implementation of regex based on the engine used by the utilities.
  • Using the NFA (Nondeterministic Finite Automation) engine, though slower than DFA (Deterministic Finite Automation) engine,allows you to refine and manage the regex to get a specific result:
    • perl -ne 'print unless /^\s*[;\$#]|^$/' file_config
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Linux - Displaying a file without the commentary lines », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).

Subscribe To Our Newsletter!

The Best of CCM in Your Inbox

Subscribe To Our Newsletter!