cancel
Showing results for 
Search instead for 
Did you mean: 
ricardodacosta
Moderator
Moderator
  • 5,750 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 #.

 

 

 

 

 

 

 

 

 

Tags (3)
11 Replies
oldbenko
Moderator
Moderator
  • 5,205 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!

A black cat crossing the street signifies that the animal is going somewhere.
[don't forget to kudo a helpful post or mark it as a solution!]
kaiser
Flight Engineer
Flight Engineer
  • 5,165 Views

Piping the 'sort' and 'tr' is nice but same output without the 'cut' command

DavidOBrien
Starfighter Starfighter
Starfighter
  • 5,125 Views

I use this all the time to spell-check all my XML files in courseware:

 

find . -iregex .*keyword.*.xml -exec aspell -c '{}' \; 
kubeadm
Flight Engineer Flight Engineer
Flight Engineer
  • 5,118 Views

Mine is "captain hook"  -    (/.*)?

RJ
Flight Engineer Flight Engineer
Flight Engineer
  • 5,041 Views

Hey Kubeadm, 

Can you give us an example of that "captain hook"?

 

Thanks!

 

RJ

RJ
kubeadm
Flight Engineer Flight Engineer
Flight Engineer
  • 2,073 Views

Hi RJ,

Here's an example:

semanage fcontext -a -t httpd_sys_content_t '/virtual(/.*)?'

Cheers,
Adrian

RJ
Flight Engineer Flight Engineer
Flight Engineer
  • 5,045 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} 

 

RJ
kaiser
Flight Engineer
Flight Engineer
  • 1,934 Views

Nice examples guys,

but I got lazy over the years. now I use notepad++ ... 

Chetan_Tiwary_
Community Manager
Community Manager
  • 1,767 Views

@kaiser for what - please elaborate :xD 

0 Kudos
Join the discussion
You must log in to join this conversation.