I troubleshoot botched configs very often and I hate seeing the annotations (comment lines), they distract from the main lines in a configuration file, and makes it harder to find the erronous line.
I use this:
grep '^[^#]' /path/to/file
or
grep -vE ‘^\s*(#|$)’ /path/to/file
----------------------------------------------------------------------------------------------------------------------
If you're satisfied with the solutions provided please mark the solution as ACCEPTED.
Don't forget to thank those who helped you out with kudos!Both will strip your config file of lines beginning with a # and blank lines, but the latter also strips those lines which have spaces then the #.
Hey, Ricardo!
I like that negative match in the first regex. I usually do it in a slightly longer way with tr:
$ grep -v '^[[:space:]]*#' /some/file | tr -s '\n'
The reason I commonly go for the tr command at the end is that I may be processing several files at the same time, but in a different way, and then simply just squeeze consecutive newlines (i.e. an empty line is simply two newline characters in a row) in the combined output.
$ (grep -v '^[[:space:]]*#' /some/file ; grep -v '^[[:space:]]*"' /another/file) | tr -s '\n'
And although it isn't really a regular expression, my favourite has got to be producing a list of directories sorted by their disk usage, but using human-friendly suffixes like M/G (which are otherwise unsortable):
$ du -s /some/parent/dir/* | sort -rgk1 | cut -f 2- | tr '\n' '\0' | xargs -0 du -sh
The use of tr here will prevent any problems due to spaces in filenames - I know GNU xargs has an option to specify the delimiter, but unfortunately sometimes I still have to work with other systems which don't come with GNU xargs.
Cheers!
Piping the 'sort' and 'tr' is nice but same output without the 'cut' command
I use this all the time to spell-check all my XML files in courseware:
find . -iregex .*keyword.*.xml -exec aspell -c '{}' \;
Mine is "captain hook" - (/.*)?
Hey Kubeadm,
Can you give us an example of that "captain hook"?
Thanks!
RJ
Hi RJ,
Here's an example:
semanage fcontext -a -t httpd_sys_content_t '/virtual(/.*)?'
Cheers,
Adrian
Nice methods. I also used:
egrep -v \# /path/to/config | grep .
That drops all comments, and empty lines as well. I like the examples presented earlier too. Strictly speaking, it's not a regex, but it functions well.
I do however do things like this often in kickstarts for instance:
/bin/mkdir -p /{cdrom,mnt,iso}/notmounted
mydate=`date '+%Y%m%d_%H%M%S'`;echo $mydate
/bin/cp -v /etc/pam.d/system-auth-ac{,.$mydate}
then run whatever script I plan on knowing I have a backup of the file with a good time date stamp.
The other day, it was:
tail -f /var/log/{messages,secure,sssd/*log}
Nice examples guys,
but I got lazy over the years.
@kaiser for what - please elaborate :xD
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.