Awk RegEx返回nada? [英] Awk RegEx returning nada?

查看:87
本文介绍了Awk RegEx返回nada?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在一个名为 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';

,而我试图让awk只从行中返回版本号:

$OC_VersionString = '20.0.1';

(到目前为止)使用以下命令:

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

但是,当我使用它时,什么也没有返回?

这是我第一次使用awk,太糟糕了……BRE感到"awk-warrrd" nerk nerk nerk

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

有人能够解决这个问题吗?

Anyone able to sort this out?

推荐答案

能否请您尝试以下操作.用GNU awk中显示的示例编写和测试.

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



更通用的解决方案:添加了一个更通用的解决方案,该解决方案已在GNU awk中进行了测试,如果它具有更次要的版本,例如-> ;,它将捕获版本号. 20.2.0.1(例如)



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

简要说明: 检查条件/^\$OC_VersionString/以检查行是否从/$OC_VersionString/开始,然后检查awkmatch函数中是否存在正则表达式. matchawk的默认函数,我们提供要匹配的正则表达式,一旦我们匹配了正则表达式,它将设置RSTARTRLENGTH默认变量的值.其中RSTART是匹配的正则表达式的起始索引点,而RLENGTH是匹配的正则表达式的长度.然后从起始行为RSTART的当前行打印子字符串,直到RLENGTH的值.

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.

这篇关于Awk RegEx返回nada?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆