Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Lines when Column K is empty with AWK/Perl

Tags:

linux

unix

awk

perl

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 
like image 878
neversaint Avatar asked May 25 '10 06:05

neversaint


People also ask

What is awk '{ print $1 }'?

The awk is a very powerful command or interpreted scripting language to process different text or string data. The awk is generally used to process command output or text or configuration files. The awk provides '{print $1}' command in order to print the first column for the specified file or output.

What awk $0?

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces.


1 Answers

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)

like image 153
paxdiablo Avatar answered Oct 14 '22 22:10

paxdiablo