Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've got a text file and I'm trying to figure out how to filter out any lines with data in only 1 field, leaving lines with 2 fields. I came across the following code: awk '$NF > 1' but I can't get it to work as I want it to. Any ideas what I might be doing wrong?

Here is a sample of my data:

simpledaddy11,EastOfBrussels
simpledaddy11,thomaswatkins
simpledaddy11,
simpledaddy11,
simpledaddy11,
simpledaddy11,
simpledaddy11,
simpledaddy11,
simpledaddy11
Moral_Animal,EastOfBrussels
Moral_Animal,
Moral_Animal,
Moral_Animal,
Moral_Animal,
Moral_Animal,
Moral_Animal,
Moral_Animal,
Moral_Animal
skilur_ua,EastOfBrussels
skilur_ua,thomaswatkins
skilur_ua,
skilur_ua,
skilur_ua,
skilur_ua,

Here is what I want to end up with:

simpledaddy11,EastOfBrussels
simpledaddy11,thomaswatkins
Moral_Animal,EastOfBrussels
skilur_ua,EastOfBrussels
skilur_ua,thomaswatkins

Thanks for the help!

share|improve this question

2 Answers 2

If you want to keep lines that have data in at least 2 fields:

grep '[^,].*,[^,]'
share|improve this answer
    
+1, Would grep '[^,].*,[^,]' suffice? I see no reason for the second .*? –  grebneke Mar 6 at 22:04
    
@grebneke, you're right. Edited. Thanks. –  Stéphane Chazelas Mar 6 at 22:06
1  
This is a good answer. I believe egrep '[^,],+[^,]' would also work. But, if the sample data in the question are representative of the actual data, grep ',.' is enough. –  Scott Mar 7 at 0:21

You need to change the field separator to a comma, also you should not have a $ before the NF if you want the number of fields ($NF is the last field). Finally lines ending in a comma are considered to have an empty second field, so you should exclude lines where the last field is blank:

awk -F, 'NF>1 && $NF!=""'

Update

From Stephane's comment, I obviously wasn't reading the question properly. The awk to print lines containing more than one non-blank field is more complicated:

awk -F, '{ nonblank=0;
  for(i=1;i<=NF;i++){
    if($i!="") nonblank++
  }
  if(nonblank>1) print $0
}'

For this purpose, Stephane's grep approach is much better.

share|improve this answer
    
whats the difference between NF and $NF? –  FDinoff Mar 6 at 20:56
    
@FDinoff NF is the number of fields in the current input record. $NF is the last field. Given a record with three fields a b c, NF will be 3 and $NF is equivalent to $3 which will be c –  grebneke Mar 6 at 21:09
    
If the requirement is really to filter out lines with data in only one field, then that fails to exclude ,foo, and that wrongly excludes foo,bar,. –  Stéphane Chazelas Mar 6 at 21:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.