To grep version number from WordPress plugin folder

What will be grep regex to extract version number like

 Version: 3.1.5
 * Version: 3.1.5

Will output 3.1.5

Read More

But It should not catch likes

MIME-Version: 1.0n

Here is my grep command

grep -ri 'versions*:s*[0-9.]w*' /home/test/public_html/wp-content/plugins/plugin_name 

@anubhava
woocommerce is a folder. There is file woocommerce.php under this folder.

**Content of woocommerce.php **

 * Plugin Name: WooCommerce
 * Plugin URI: http://www.woothemes.com/woocommerce/
 * Description: An e-commerce toolkit that helps you sell anything. Beautifully.
 * Version: 2.3.8
 * Author: WooThemes
 * Author URI: http://woothemes.com
 * Requires at least: 4.0
 * Tested up to: 4.2

Working

grep -rPo "(^|s|^*)+(Versions*:s*)K([0-9]|.)*(?=s|$)" /home/test/public_html/wp-content/plugins/woocommerce/

Related posts

Leave a Reply

2 comments

  1. You can use grep -oP (PERL style regex):

    grep -oP 'Version: *KS+' file
    3.1.5
    3.1.5
    

    Version: * matches literal Version: followed by 0 or more spaces. K resets the match information and S+ matches version number.

  2. grep -rPo "(^|s|^*)+(Versions*:s*)K([0-9]|.)*(?=s|$)" /home/test/public_html/wp-content/plugins/attachments/
    

    Explanation :

    • -P stands for PCRE, -o for taking only the matched portion of the line

    • (^|s)+(Version: ) will match Version at the start or one or more whitespaces, the K will then discard the match

    • ([0-9]|.)* will match any digit or . zero or more times, this is what we want

    • The previous token will be followed by either any whitespace character or end of the line