AWK子字符串一个字符 [英] Awk substring a single character

查看:74
本文介绍了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

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

这篇关于AWK子字符串一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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