This script removes blank lines and commented lines so that configuration files can be displayed with details only.
This is very useful if you're trying to get a system working and have one that is working. You'd run this script on both and compare
using either diff or cmp, or just viewing the files in vi.
annotated version with some history...
#!/bin/bash
################################################################################
# 24 jun 2016 - john meister copyright 2016 http://johnmeister.com/linux
# cat's file, then greps out blank lines and comments, offers option to save
# 22 jul 2016 - modified to save file in user's home directory
# 6 jun 2017 - finally got the white space string working: grep -E -v '(^\s*#|^$)'
# changed CMD to VFL - view file
# added sed string to strip leading slash off echo /etc | sed 's/^\///'
# NOTE: create ~/bin directory, chmod to 755, add ~/bin to PATH - test:
# ex: --> which catgrep
# /home/luser/bin/catgrep
################################################################################
USAGE="catgrep filename # removes commented and blank lines, option to save"
### BEGIN #############################################################################
echo ' --------------------------------------------------------------------'
echo " DISPLAYING file: --> $1 <-- without commented or blank lines"
echo ' --------------------------------------------------------------------'
################################################################################
#### cat $1 | grep -E -v '(^#|^$)' # (this didn't cover white space prior to a comment)
################################################################################
cat $1 | grep -E -v '(^\s*#|^$)' # the \s is for white spaces
################################################################################
# note: this is without -E (extended grep) # the last grep expression may be redundant
# cat $1 | grep -v '^\s*#' | grep -v '^$' | grep -v '^#'
################################################################################
VFL="`echo "$1" | sed 's/^\///' | tr '/' '-'`" ; export VFL
echo '-----------------------------------------------------------------'
echo "save info in local directory? if yes, type: y"
echo '-----------------------------------------------------------------'
read answer ; if [ $answer = "y" ] # note, adding brackets for upper/lower case would be [Y,y]
then
echo "$1" | tee ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt
cat $1 | grep -E -v '(^\s*#|^$)' | tee -a ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt
################################################################################
# cat $1 | grep -E -v '(^#|^$)' | tee -a ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt
# cat $1 | grep -E -v '(^#|^[space ctrl-v-tab]^#$|^$)' | tee -a ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt # using ^\s*# works the best, see current line
################################################################################
## NOTE: adding within brackets a space and tab prior to a comment will remove lines with empty space before comment ###
# cat $1 | grep -E -v '(^[space ctrl-v-tab]^#$|^$)' | tee -a ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt
## NOTE: adding the bracket info makes the ^# redundant ###
echo '-----------------------------------------------------------------'
echo " info written to: ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt "
echo '-----------------------------------------------------------------'
else
echo ' ### ------------------------------------'
echo ' ### not written.'
echo ' ### ------------------------------------'
fi
## END ######################################
clean copy to copy...
# this is the key string: grep -E -v '(^\s*#|^$)'
# this is the key string optimized: grep -E -v '^(\s*#|$)'
#!/bin/bash
################################################################################
# 24 jun 2016 - john meister copyright 2016 http://johnmeister.com/linux
# cat's file, then greps out blank lines and comments, offers option to save
# 22 jul 2016 - added sed string to strip leading slash off echo /etc | sed 's/^\///'
# 6 jun 2017 - finally got the white space string working: grep -E -v '(^\s*#|^$)'
# NOTE: create ~/bin directory, chmod to 755, add ~/bin to PATH - test:
# ex: --> which catgrep
# /home/luser/bin/catgrep
################################################################################
USAGE="catgrep filename # removes commented and blank lines, option to save"
echo ' --------------------------------------------------------------------'
echo " DISPLAYING file: --> $1 <-- without commented or blank lines"
echo ' --------------------------------------------------------------------'
cat $1 | grep -E -v '^(\s*#|$)' # the \s is for white spaces
VFL="`echo "$1" | sed 's/^\///' | tr '/' '-'`" ; export VFL
echo '-----------------------------------------------------------------'
echo "save info in local directory? if yes, type: y"
echo '-----------------------------------------------------------------'
read answer ; if [ $answer = "y" ] # note, adding brackets for upper/lower case would be [Y,y]
then
echo "$1" | tee ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt
cat $1 | grep -E -v '^(\s*#|$)' | tee -a ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt
#### NOTE: use redirect or append on line above instead of tee -a to reduce screen clutter
echo '-----------------------------------------------------------------'
echo " info written to: ~/FYI-$VFL-content-`date +%Y%b%d-%H%M`.txt "
echo '-----------------------------------------------------------------'
else
echo ' ### ------------------------------------'
echo ' ### not written.'
echo ' ### ------------------------------------'
fi
an example of it's use, viewing displaymanager on a SuSE system running a MATE desktop
(note: this system would not be considered "secure" (this device is physically secured fwiw...)
as it is configured for an autologin without a password, YMMV... FYI) - change it if you're concerned
------------------------------------------------
--> cat /etc/sysconfig/displaymanager | wc -l
119
###### As you'll note below, only 10 lines have meaning...
###### What would you rather do? view core content or wade through 119 lines?
------------------------------------------------
luser@linuxbox [/home/luser]
------------------------------------------------
--> catgrep /etc/sysconfig/displaymanager
--------------------------------------------------------------------
DISPLAYING file: --> /etc/sysconfig/displaymanager <-- without commented or blank lines
--------------------------------------------------------------------
DISPLAYMANAGER_XSERVER="Xorg"
DISPLAYMANAGER="sddm"
DISPLAYMANAGER_REMOTE_ACCESS="yes"
DISPLAYMANAGER_ROOT_LOGIN_REMOTE="no"
DISPLAYMANAGER_STARTS_XSERVER="yes"
DISPLAYMANAGER_XSERVER_TCP_PORT_6000_OPEN="yes"
DISPLAYMANAGER_AUTOLOGIN="luser"
DISPLAYMANAGER_PASSWORD_LESS_LOGIN="yes"
DISPLAYMANAGER_AD_INTEGRATION="no"
DISPLAYMANAGER_SHUTDOWN="auto"
-----------------------------------------------------------------
save info in local directory? if yes, type: y
-----------------------------------------------------------------
y
/etc/sysconfig/displaymanager
DISPLAYMANAGER_XSERVER="Xorg"
DISPLAYMANAGER="sddm"
DISPLAYMANAGER_REMOTE_ACCESS="yes"
DISPLAYMANAGER_ROOT_LOGIN_REMOTE="no"
DISPLAYMANAGER_STARTS_XSERVER="yes"
DISPLAYMANAGER_XSERVER_TCP_PORT_6000_OPEN="yes"
DISPLAYMANAGER_AUTOLOGIN="luser"
DISPLAYMANAGER_PASSWORD_LESS_LOGIN="yes"
DISPLAYMANAGER_AD_INTEGRATION="no"
DISPLAYMANAGER_SHUTDOWN="auto"
-----------------------------------------------------------------
info written to: ~/FYI-etc-sysconfig-displaymanager-content-2017Jun06-1036.txt
-----------------------------------------------------------------
|