AWK子字符串一个字符

编程入门 行业动态 更新时间:2024-10-09 02:23:49
本文介绍了AWK子字符串一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是columns.txt

Here is columns.txt

aaa bbb 3 ccc ddd 2 eee fff 1 3 3 g 3 hhh i jjj 3 kkk ll 3 mm nn oo 3

我可以找到第二列以"b"开头的行:

I can find the line where second column starts with "b":

awk '{if(substr($2,1,1)=="b") {print $0}}' columns.txt

我可以找到第二列以"bb"开头的行:

I can find the line where second column starts with "bb":

awk '{if(substr($2,1,2)=="bb") {print $0}}' columns.txt

为什么为什么我找不到第二列中第二个字符是"b"的行?:

Why oh why can't I find the line where the second character in the second column is "b"?:

awk '{if(substr($2,2,2)=="b") {print $0}}' columns.txt

awk -W version == GNU Awk 3.1.8

awk -W version == GNU Awk 3.1.8

推荐答案

您可以使用:

awk 'substr($2,2,1) == "b"' columns.txt aaa bbb 3

  • substr函数的第三个参数为长度字符串.
  • print是awk中的默认操作.
  • substr function's 3rd argument is length of the string.
  • print is default action in awk.
  • 更多推荐

    AWK子字符串一个字符

    本文发布于:2023-11-29 09:29:55,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646010.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   字符   AWK

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!