Ignore records with no values through awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore records with no values through awk
# 8  
Old 02-09-2011
An awk statement has the form:
Code:
condition {action}

Conditions in awk control the execution of actions and actions are executed when the condition is true.
If the condition is true (1 is true) and there are no actions between braces, awk prints the current record by default.
Code:
awk NF-1 infile

The NF variable gives the number of fields in a line.
If a line has 1 column (NF=1) the condition NF-1 (1-1=0) is false, so only lines with more then one field should be printed.
Code:
awk '$2' infile

This condition is true if a second column exists, all lines with one field are ignored.
# 9  
Old 02-09-2011
If there is only one field then NF equals 1, 1-1=0 . So the default action (print $0) is not performed. If there are 2 or more fields the line does not get printed.. If NF>1 then the result is >=1 so the line does get printed.

The same goes for awk '$2' . If $2 exists, in other words there are two or more fields then the line gets printed.

The difference is the first solution prints empty lines. The second solution discards them and also does not print the line if $2 contains the value 0.

To skip empty lines and print $2 if it is 0, one could use one of these:
Code:
awk '$2""' infile

Code:
awk 'NF>1' infile


Last edited by Scrutinizer; 02-09-2011 at 10:14 AM..
# 10  
Old 02-09-2011
awk NF-1 means
show just 1st(or more) fields. if the line has 2 fields (2 columns) it will show just NF-1=2-1=1 field.

so, NF-1 will show only lines having 2 or more fields

Last edited by gc_sw; 02-09-2011 at 11:05 AM..
# 11  
Old 02-09-2011
hi

thanks a lot for ur reply
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting cut to ignore cols in middle of records

I recently had to remove a number of columns from a sorted copy of a file, but couldn't get the cut command to take fields out, just what to keep. This is the only thing I could find as an example, but could it be simplified? tstamp=`date +%H%M%S` grep -v "T$" filename |egrep -v "^$" |sort... (3 Replies)
Discussion started by: wbport
3 Replies

2. Shell Programming and Scripting

awk to ignore whitespace in field

The awk below executes and update the desired field in my first awk. However, the white space between nonsynonymous SNV in $9 is being split into tabs and my attempt to correct this does not update the field unless it is removed. I am not sure what I am doing wrong? Thank you :). file1 ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Programming

Subtract values based on records

Hi Guys, I am having below tables in oracle T1 ID F_TYPE F_AMT DATE_COL 1 F 6 11-Feb-16 1 D 2 11-Feb-16 1 D 2 11-Feb-16 1 F 6 11-Feb-16 1 F 2 12-Mar-16 1 D 3 12-Mar-16 1 F 4 10-Apr-16 1 F 4 11-Apr-16 1 D 1 11-Apr-16 T2 ID START_DATE END_DATE F_ID FLAG... (0 Replies)
Discussion started by: rohit_shinez
0 Replies

4. Shell Programming and Scripting

Clean values in range of duplicate records

Dear Gents, Could you please help me to solve this problem. I am getting the average of a column for same duplicate records in this case locate in column 1. The average is computed from column 5 from all duplicate records in column 1. Normallly the values in column 5 is constant so the... (3 Replies)
Discussion started by: jiam912
3 Replies

5. Shell Programming and Scripting

Copy header values into records

I'm using a shell script to manipulate a data file. I have a large file with two sets of data samples (tracking memory consumption) taken over a long period of time, so I have many samples. The problem is that all the data is in the same file so that each sample contains two sets of data.... (2 Replies)
Discussion started by: abercrom
2 Replies

6. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

7. Shell Programming and Scripting

awk to ignore the text before a particular word

Hi I am new to Awk programming , i would appreciate if anyone help me with the below scenario i have text file arranged in rows and columns like below 11004 04493384 26798 CASSI0000I Server manager initialization started 111004 04493486 26798 CASSI4005I Retrieving ES... (7 Replies)
Discussion started by: rakeshkumar
7 Replies

8. Shell Programming and Scripting

verifying column2 for same kind of records and adding corresponding values in column3

Hi am having a file which looks like this i want to get unique values in column2 and sum up the corresponding column3 values and discard the column4 and then write the output in different file. i.e the output has to be like i.e 07-Jun-2009 919449829088 52 lessrv1 07-Jun-2009... (2 Replies)
Discussion started by: aemunathan
2 Replies

9. Shell Programming and Scripting

awk, ignore first x number of lines.

Is there a way to tell awk to ignore the first 11 lines of a file?? example, I have a csv file with all the heading information in the first lines. I want to split the file into 5-6 different files but I want to retain the the first 11 lines of the file. As it is now I run this command: ... (8 Replies)
Discussion started by: trey85stang
8 Replies

10. UNIX for Dummies Questions & Answers

Generating key values for leader records

All, I have a file with text as shown below. I want the o/p file with generated values in the first column as shown in the o/p file. Pls note that the size of my file is 6 GB. How do i do this ? Input file 999999abcdef 999999ghijkl 999999mnopq 777777rosesarered 777777skyisblue Output... (1 Reply)
Discussion started by: ajfaq
1 Replies
Login or Register to Ask a Question