1

What will be grep regex to extract version number like

 Version: 3.1.5
 * Version: 3.1.5

Will output 3.1.5

But It should not catch likes

MIME-Version: 1.0\n

Here is my grep command

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

not working

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

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|^\*)+(Version\s*:\s*)\K([0-9]|\.)*(?=\s|$)" /home/test/public_html/wp-content/plugins/attachments/
1
  • The command is working well....while checking for files inside a directory run it as grep -rPo .... /directory Commented May 7, 2015 at 11:21

3 Answers 3

1

You can use:

grep -Po "(?<=Version: )([0-9]|\.)*(?=\s|$)"

If you have MIME-Version: 1.0 instead of MIME-Version: 1.0\n:

grep -Po "(^|\s)+(Version: )\K([0-9]|\.)*(?=\s|$)"

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

Test:

$ cat file.txt 
 Version: 3.1.5
 * Version: 3.1.5
MIME-Version: 1.0\n

$ grep -Po "(?<=Version: )([0-9]|\.)*(?=\s|$)" file.txt 
3.1.5
3.1.5



$ cat file.txt 
Version: 3.1.5
 * Version: 3.1.5
MIME-Version: 1.0

$ grep -Po "(^|\s)+(Version: )\K([0-9]|\.)*(?=\s|$)" file.txt 
3.1.5
3.1.5
9
  • This will still catch the MIME-Version: if it doesn't have \n on the end,which I think must be a typo as it seems very odd. Commented May 7, 2015 at 11:03
  • @JID: well, i have taken OP's examples literally..anyway i have modified the answer.. Commented May 7, 2015 at 11:11
  • i want to search in all files under folder plugin_name. I have updated question for it. Commented May 7, 2015 at 11:12
  • @WaseemShaikh Then use grep -rPo ...... /path/to/plugin_name Commented May 7, 2015 at 11:15
  • @WaseemShaikh: It must work..check my example..i have shown that already.. Commented May 7, 2015 at 11:37
0

you can use something like

grep -w Version: /home/test/public_html/wp-content/plugins/plugin_name |grep -v -- -Version|awk '{print $NF}'
12
  • It is not working for either of valid string Commented May 7, 2015 at 10:26
  • You are right, it will work for Version: 3.1.5, but not for * Version: 3.1.5. Check my updated answer Commented May 7, 2015 at 10:31
  • @JID, your code maybe need small correction. You expect to have space symbol before Version keyword Commented May 7, 2015 at 10:40
  • @RomeoNinov 0 or more matches,your will match MIME-Version: tbh though it would probably be better to use awk '/[^[:graph:]]Version/{print $NF}' Commented May 7, 2015 at 10:44
  • @JID no, you do not read the key I add to grep!!! Commented May 7, 2015 at 10:46
0

This will work

awk '/[^[:graph:]]Version/{print $NF}' file

Example

Input

 Version: 3.1.5
 * Version: 3.1.5
MIME-Version: 1.0
J_Version: 7.25
A.Version: 50

Output

3.1.5
3.1.5

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.