Bash Regex multiple optional captures

I’m trying to get information out of wordpress plugins, the plugin name and the version.

I’m finding that the line I’m looking for could be Plugin Name:, Plugin Name:, Plugin Name: , or * Plugin Name: or variations of space. Since the line I”m looking for is in the comments part, it may also include the *

Read More

I’ve gotten as far as getting the whitespace optional, I think, but i can’t figure out capturing the * with space around it.

grep "^s*Plugin Name:" $files

which is working for most of the plugins but not all, since some have other characters to watch out for.

I’ve tried

(s*)?|( * )?  
[ *]|[ * ]? 
( *| * )?

but I’m having a hard time wrapping my head around stringing it all together.

Edit:
Using

grep -E '^s**?s*Plugin Name:' $files

will find everything, but two of the plugins in this case now, that have * Plugin Name: will print out an entirely different directory in addition to the correct information.

$plugPath is the wordpress plugins folder – /wp-content/plugins

folders=`find $plugPath -maxdepth 1 -type d`
for plugin_folder in $folders
do
    echo $plugin_folder
    files=`find $plugin_folder -maxdepth 1 -name "*.php"`

    n=`grep -hE '^s**?s*Plugin Name:' $files`
    v=`grep -hE '^s**?s*Version:' $files`
    echo " -- "$n
    echo " -- "$v
done

Result I’m expecting:

/home/test/accounts/account-one/public_html/wp-content/plugins/flexible-recent-posts
 -- Plugin Name: Flexible Recent Posts
 -- Version: 1.0.4

Example that is working:

Plugin Name: Flexible Recent Posts
Plugin URI: http://steelrat.info/
Description: Displays recent posts using flexible template system.
Version: 1.0.4
Author: SteelRat

Where it’s screwing up:

/home/test/accounts/account-one/public_html/wp-content/plugins/portfolio-post-type
 --  2015.11.13-AccountInfo.log.csv accounts bin Desktop Documents Downloads latest.tar.gz Music Pictures Public Templates test-info.log.csv Videos wordpress Plugin Name: Portfolio Post Type
 --  2015.11.13-AccountInfo.log.csv accounts bin Desktop Documents Downloads latest.tar.gz Music Pictures Public Templates test-info.log.csv Videos wordpress Version: 0.9.1

The file it finds these in is like:

 * Plugin Name: Portfolio Post Type
 * Plugin URI:  http://wptheming.com/portfolio-post-type/
 * Description: Enables a portfolio post type and taxonomies.
 * Version:     0.9.1

Related posts

2 comments

  1. A simple:

    grep "Plugin Name:"
    

    is not enough?

    UPDATE

    Then try this version:

    grep -E '^s*[*]?s*Plugin Name:'
    

    Or an equivalent:

    grep -E '^s**?s*Plugin Name:'
    

    UPDATE 2

    Probably the loop in the updated question suffer the non-adequately-quoted-filename syndrome. I suggest a find one-liner to obtain almost the same functionality:

    find "${WORDPRESS_PLUGIN_DIR}" -mindepth 2 -maxdepth 2 -iname "*.php" -exec grep -E '^s**?s*(Plugin Name|Version):' {} ;
    
  2. This catches everything, including spaces prior, plus a single space past Plugin Name:

    grep -G "^.*Plugin Name:s" $files
    

Comments are closed.