提取行时K栏是空的有AWK / Perl的 [英] Extract Lines when Column K is empty with AWK/Perl

查看:91
本文介绍了提取行时K栏是空的有AWK / Perl的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据,看起来像这样:

  78 foo的XXX
酒吧YYY
qux 99 ZZZ
xuq XYZ

他们是制表符分隔。
我怎样才能提取线,其中第2栏是空的,产生

 酒吧YYY
xuq XYZ

我试过,但似乎并没有工作:

 的awk'$ 2 =='myfile.txt的


解决方案

您需要专门设置字段分隔符为<大骨节病>标签字符:

 &GT;猫qq.in
  富78 XXX
  酒吧YYY
  qux 99 ZZZ
  xuq XYZ
&GT;猫qq.in | awk的'BEGIN {FS =\\ t的} $ 2 =={}打印
  酒吧YYY
  xuq XYZ

有关的默认行为 AWK 是治疗的 FS <大骨节病> SPACE (中缺省值)作为一个特例。从手册页:


  

FS 是单个空格,字段由分隔的特殊情况下的运行的空间和/或制表符和/或换行符的。的(我斜体)


I have data that looks like this:

foo 78 xxx
bar    yyy
qux 99 zzz
xuq    xyz

They are tab delimited. How can I extract lines where column 2 is empty, yielding

bar    yyy
xuq    xyz

I tried this but doesn't seem to work:

awk '$2==""' myfile.txt 

解决方案

You need to specifically set the field separator to a TAB character:

> cat qq.in
  foo     78      xxx
  bar             yyy
  qux     99      zzz
  xuq             xyz
> cat qq.in | awk 'BEGIN {FS="\t"} $2=="" {print}'
  bar             yyy
  xuq             xyz

The default behaviour for awk is to treat an FS of SPACE (the default) as a special case. From the man page:

In the special case that FS is a single space, fields are separated by runs of spaces and/or tabs and/or newlines. (my italics)

这篇关于提取行时K栏是空的有AWK / Perl的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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