awk - remove row if specific field is empty/blank


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - remove row if specific field is empty/blank
# 1  
Old 04-02-2012
awk - remove row if specific field is empty/blank

I have this

text.file
Code:
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
Code:
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:

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

didn't work.

Any ideas?
# 2  
Old 04-02-2012
Some options:
Code:
awk -F'\t' '$1!=""' infile

or
Code:
awk -F'\t' 'x$1' infile

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

-or just-
Code:
awk '!/^\t/' infile

for the first field.

And to leave out lines where any of the fields are empty, try this:
Code:
awk '!/^\t|\t\t|\t$/' infile


Last edited by Scrutinizer; 04-02-2012 at 04:53 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-02-2012
Scrutinizer thanks for reply

I tried the first code ( I thought for sure this one was going to work) but didn't:
Code:
awk -F'\t' '$1!=""' infile

I tried the second code:
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:
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
Code:
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.
# 4  
Old 04-02-2012
awk obviously does what you want:

Code:
$ 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?
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-02-2012
Works

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 Smilie Yahoooooo...

Last edited by charles33; 04-02-2012 at 06:05 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

4. Shell Programming and Scripting

How to remove empty field in a text file?

Hi all, I want to remove empty field in a text file. I tried to used sed. But it failed. Input: LG10_PM_map_19_LEnd 1000560 G AG AG LG10_PM_map_19_LEnd 1005621 G AG LG10_PM_map_19_LEnd 1011214 A AG AG LG10_PM_map_19_LEnd 1011673 T CT CT ... (3 Replies)
Discussion started by: huiyee1
3 Replies

5. Shell Programming and Scripting

Trying to remove duplicates based on field and row

I am trying to see if I can use awk to remove duplicates from a file. This is the file: -==> Listvol <== deleting /vol/eng_rmd_0941 deleting /vol/eng_rmd_0943 deleting /vol/eng_rmd_0943 deleting /vol/eng_rmd_1006 deleting /vol/eng_rmd_1012 rearrange /vol/eng_rmd_0943 ... (6 Replies)
Discussion started by: newbie2010
6 Replies

6. Shell Programming and Scripting

how to remove tab space only in the column of a specific row

Hi, I need help to remove tab delimited space in the $2 of a specific row. My file is like this:- file1.txt No_1 4 139 156 No_1 5 161 205 No_4 91 227 212 No_19 254 243 263 No_19 645 249 258 No_19 101 2492 2635 No_90 8 277 288... (5 Replies)
Discussion started by: redse171
5 Replies

7. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

8. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

10. Shell Programming and Scripting

Awk scrip to remove empty field

Hi I have a file which looks like this name: Sally group: Group4 name: Tim group: Group1 name: Dan group: Group2 name: Chris group: Group3 name: Peter group: name: Fred group: name: Mary group: Group2 Well I want to get rid of the... (4 Replies)
Discussion started by: bombcan
4 Replies
Login or Register to Ask a Question