AWK将双引号字符串视为一个令牌,并忽略两者之间的空格 [英] Awk consider double quoted string as one token and ignore space in between

查看:307
本文介绍了AWK将双引号字符串视为一个令牌,并忽略两者之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据文件-data.txt:

Data file - data.txt:

ABC "I am ABC" 35 DESC
DEF "I am not ABC" 42 DESC

cat data.txt | awk '{print $2}'

将得到"I"而不是被引号的字符串

will result the "I" instead of the string being quoted

如何使awk忽略引号中的空格,并认为它是单个标记?

How to make awk so that it ignore the space within the quote and think that it is one single token?

推荐答案

是的,可以在awk中很好地完成.很容易获得所有字段而没有任何严重的黑客攻击.

Yes, this can be done nicely in awk. It's easy to get all the fields without any serious hacks.

(此示例在一个真正的Awk 和gawk中都适用.)

(This example works in both The One True Awk and in gawk.)

{
  split($0, a, "\"")
  $2 = a[2]
  $3 = $(NF - 1)
  $4 = $NF
  print "and the fields are ", $1, "+", $2, "+", $3, "+", $4
}

这篇关于AWK将双引号字符串视为一个令牌,并忽略两者之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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