awk - remove row if specific field is empty/blank

I have this

text.file

charles    darwin    sam    delight
george    washington    johnson culper
    darwin    sam    delight
micheal    jackson    penny    lite

and would like to remove the row, if the first field is blank. so the result would be:

result.file

charles    darwin    sam    delight
george    washington    johnson culper
micheal    jackson    penny    lite

The field separator is a tab. And typically it's the first field, but it would be nice to specify the field, like $1 or $2.

I tried this:

awk -F\t "NF > 1" OFS="\t" text.file > result.file

didn't work.

Any ideas?

Some options:

awk -F'\t' '$1!=""' infile

or

awk -F'\t' 'x$1' infile

You can use single quotes to protect \t from interpretation by the shell..

-or just-

awk '!/^\t/' infile

for the first field.

And to leave out lines where any of the fields are empty, try this:

awk '!/^\t|\t\t|\t$/' infile
1 Like

Scrutinizer thanks for reply

I tried the first code ( I thought for sure this one was going to work) but didn't:

awk -F'\t' '$1!=""' infile

I tried the second code:

awk -F'\t' 'x$1' infile

but didn't work, so I thought about adding a blank row to test it, and it does remove a blank row, but does not remove the row which is empty/blank in the first field.

I tried the third code:

awk '!/^\t/' infile

but it didn't work

The last one you gave worked BUT it removed all the lines (just like you said) which have empty/blank fields

awk '!/^\t|\t\t|\t$/' infile

I don't know maybe i'm doing something wrong, becuase the first code looks the most promising. I did verify there is a space, right before the first tab. I did verify that line contains 3 tab field separators. What is going on. Let me check with another text.file - i will report.

Maybe there is another way instead of using awk.

awk obviously does what you want:

$ printf "\tASDF\n" | awk -F'\t' '$1' # Ignore lines where first field is blank

$

So, your data doesn't seem to be what you think it is.As such, using a different utility won't do what you want either -- not until you figure out what you actually need.

Could you attach a sample of your actual data?

1 Like

Scrutinizer

Thanks very much. It was an error on my end - The one line had four tabs (actually it was just a bad data file to begin with); I fixed it then ran your code and it worked GREAT!!!

Code #1, #2 and #3 all worked like you described. and of course code #4 works wonders!

Charles

Thanks Corona688 for the affirmation. You are correct. It is working as Scrutinizer described .... THANKS :wink: Yahoooooo...