Awk RegEx returning nada?

允我心安 提交于 2020-12-15 06:44:24

问题


So I have this text in a file named version.php:

<?php
$OC_Version = array(20,0,1,1);
$OC_VersionString = '20.0.1';
$OC_Edition = '';
$OC_Channel = 'stable';
$OC_VersionCanBeUpgradedFrom = array (
  'nextcloud' =>
  array (
    '19.0' => true,
    '20.0' => true,
  ),
  'owncloud' =>
  array (
  ),
);
$OC_Build = '2020-10-24T08:39:55+00:00 89d88b3ea5b4049355e3c49d121f82e5e62bfc44';
$vendor = 'nextcloud';

and I'm trying to get awk to return just the version number from the line:

$OC_VersionString = '20.0.1';

using (so far) this command:

awk -e '$1 ~ /[:digit:]+[:punct:][:digit:]+[:punct:][:digit:]+/ {print $0}' version.php

But, when I use it, nothing gets returned?

This is my first time using awk, sooo, ... BRE feels "awk-warrrd" nerk nerk nerk

Anyone able to sort this out?


回答1:


Could you please try following. Written and tested with shown samples in GNU awk.

awk '/^\$OC_VersionString/ && match($0,/\047[0-9]+\.[0-9]+\.[0-9]+\047/){
  print substr($0,RSTART+1,RLENGTH-2)
}' Input_file


More generic solution: Adding one more generic solution, tested in GNU awk which will capture version number in case it has more minor version eg--> 20.2.0.1(as an example) too.

awk '
/^\$OC_VersionString/ && match($0,/\047([0-9]+\.){1,}[0-9]+\047/){
  print substr($0,RSTART+1,RLENGTH-2)
}' Input_file

Brief explanation: Checking condition /^\$OC_VersionString/ to check if a line starts from /$OC_VersionString/ then checking that regex present in match function of awk. match is a default function of awk where we provide regex to be matched and once we have matched regex it sets values of RSTART and RLENGTH default variables. Where RSTART is starting index point of matched regex AND RLENGTH is length of matched regex. Then printing sub string from current line whose starting point is RSTART till value of RLENGTH.




回答2:


You can't use bare POSIX character classes, you should wrap them with bracket expressions, e.g. [:digit:] => [[:digit:]], etc.

Then, you can see the value you want is located in Field 3, while you use $1 to search for the match. And lastly, you are not extracting the field, you print the whole record with {print $0}. Also, it will still contain ' and '; if you do not use gsub.

If you want to fix your approach, you can use

s="\$OC_VersionString = '20.0.1';"
awk '$3 ~ /[[:digit:]]+[[:punct:]][[:digit:]]+[[:punct:]][[:digit:]]+/{gsub(/['"'"';]/,"", $3);print $3}' <<< "$s"
# => 20.0.1

See the online awk demo.

However, sed solution solution looks simpler here:

sed -n "s/.*'\\(.*\\)'.*/\\1/p" <<< "$s"
sed -n "s/.*'\\([0-9.]*\\)'.*/\\1/p" <<< "$s"



回答3:


You may consider this awk:

awk -F '[[:blank:]]*=[[:blank:]]*' '$1 == "$OC_VersionString" {
   gsub(/[^.0-9]+/, "", $2)
   print $2
}' file.php
20.0.1


来源:https://stackoverflow.com/questions/64609792/awk-regex-returning-nada

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!