
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 6,707 Views
I show you mine, you show me yours: What is your favorite regex to use?
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 #.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 5,980 Views
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!
[don't forget to kudo a helpful post or mark it as a solution!]

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 5,940 Views
Piping the 'sort' and 'tr' is nice but same output without the 'cut' command


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 5,900 Views
I use this all the time to spell-check all my XML files in courseware:
find . -iregex .*keyword.*.xml -exec aspell -c '{}' \;


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 5,893 Views
Mine is "captain hook" - (/.*)?


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 5,816 Views
Hey Kubeadm,
Can you give us an example of that "captain hook"?
Thanks!
RJ


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,848 Views
Hi RJ,
Here's an example:
semanage fcontext -a -t httpd_sys_content_t '/virtual(/.*)?'
Cheers,
Adrian


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 5,820 Views
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}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,709 Views
Nice examples guys,
but I got lazy over the years.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- 2,542 Views
@kaiser for what - please elaborate :xD