“awk oneliner提供了这个错误 ‘awk: line 1: syntax error at or near ,'”

huangapple go评论129阅读模式
英文:

awk oneliner provide this error "awk: line 1: syntax error at or near ,"

问题

echo "24_02_2022.rar" | awk '{match($0,/.+_(.+).rar/,a); print a[1]}'

我想从字符串中提取年份。

英文:

Can someone tell me why this simple awk command gives the above error

echo "24_02_2022.rar" | awk '{match($0,/.+_(.+).rar/,a); print a[1]}'

I would like to extract the year from a string

答案1

得分: 3

使用GNU的match函数,其中的array参数是gawk的扩展。

awk中的一个选项是将字段分隔符设置为._,然后匹配字符串末尾的最后一个下划线后跟.rar

然后打印倒数第二个字段。

echo "24_02_2022.rar" | awk -F"[._]" '/_[^_]*\.rar$/ {print $(NF-1)}';

输出:

2022
英文:

Using the gnu match function with the array argument:

> The array argument to match() is a gawk extension.

One option with awk could be setting the field separators to . and _ and then match the last underscore followed by .rar at the end of the string.

Then print the second to last field.

echo "24_02_2022.rar" | awk -F"[._]" '/_[^_]*\.rar$/ {print $(NF-1)}'

Output

2022

答案2

得分: 2

echo "24_02_2022.rar" | awk -F "[_.]" '{print $3}' 的输出是 2022

英文:

How about a simple echo "24_02_2022.rar" | awk -F "[_.]" '{print $3}' ?

Output:

2022

答案3

得分: 1

以下是翻译好的部分:

IFS=._ read -ra a <<< "24_02_2022.rar"
echo "${a[-2]}"
英文:

A pure bash solution, assuming the bash version used is greater than or equal to 4.2, could be

IFS=._ read -ra a &lt;&lt;&lt; &quot;24_02_2022.rar&quot;
echo &quot;${a[-2]}&quot;

答案4

得分: 1

以下是翻译好的部分:

"24_02_2022.rar" | gawk '{match($0,/.+_(.+).rar/,a); print a[1]}';

如果这不起作用,并且出现 gawk 不存在的投诉,请尝试安装(如果您使用的是流行的发行版,您的系统可能默认包管理器中提供了它)。如果不允许安装,您的解决方案可能可以转移到 GNU sed,如下所示:

"24_02_2022.rar" | sed 's/._(..).rar.*/\1/';

但请注意,与 GNU AWK 中使用的正则表达式相比,我添加了尾随的 .* 以删除后面的字符(如果有的话)。

(在 GNU sed 4.7 中测试过)

英文:

As already pointed you need to use GNU AWK, firstly try

echo &quot;24_02_2022.rar&quot; | gawk &#39;{match($0,/.+_(.+).rar/,a); print a[1]}&#39;

if that does not work and you get complaint that gawk is not present try installing (if you are using popular distro there is good chance it is available in default packaging manager of your system), if you are not allowed to install your solution might be ported to GNU sed following way

echo &quot;24_02_2022.rar&quot; | sed &#39;s/.*_\(..*\).rar.*//&#39;

However keep in mind that all dotes match any character including that before rar, if this is not what you want then use

echo &quot;24_02_2022.rar&quot; | sed &#39;s/.*_\(..*\)[.]rar.*//&#39;

Observe that in comparison to regular expression used in GNU AWK I added trailing .* to remove following characters if any.

(tested in GNU sed 4.7)

答案5

得分: 0

在这种特殊情况下,可能最简单的方式是

basename '24_02_2022.rar' .rar


这将返回

24_02_2022


<details>
<summary>英文:</summary>

In this particular case, probably the simplest way is

basename '24_02_2022.rar' .rar


that returns

24_02_2022


</details>



# 答案6
**得分**: 0

无需依赖`match()`函数的第三个参数:

```shell
echo "24_02_2022.rar" | 

> gawk -F'_' '{ print substr($NF,++_,-_+index($NF,".")) }' # 太啰嗦
— 或
> nawk 'NF+=OFS=_' FS='^.+_|.rar'
> mawk NF=NF OFS= FS='^.+_|.rar'  # 稍微短一点

— 或
> mawk 'gsub("^.+_|.rar",_)^_ '    # 最简洁的方式


2022

如果你愿意赌一下没有空行,那么:

> mawk -F'[_.]' '$!NF=$--NF' FS=
> gawk -F'[_.]' '$_=$--NF'

或者一种非常特殊的方式:

> nawk NF RS='.+_|[.].+'
2022
英文:

absolutely no need to rely on that 3rd argument for match():

echo &quot;24_02_2022.rar&quot; | 

> gawk -F'' '{ print substr($NF,++,-+index($NF,".")) }' # too verbose
— or
> nawk 'NF+=OFS=
' FS='^.+|.rar'
> mawk NF=NF OFS= FS='^.+
|.rar' # shorter by tiny bit

— or
> mawk 'gsub("^.+|.rar",)^_' # most succinct

2022

If you wanna gamble there aren't empty lines, then :

> mawk -F'[.]' '$!NF=$--NF' FS=
> gawk -F'[
.]' '$_=$--NF'

or a VERY fringe way of doing it :

> nawk NF RS='.+_|[.].+'

2022

huangapple
  • 本文由 发表于 2023年2月24日 15:40:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定