Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk to remove lines where field count is greather than 1 in two fields

Shell Programming and Scripting


Tags
awk

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-09-2017
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 13 September 2017, 7:30 PM EDT
Location: Chicago
Posts: 1,176
Thanks: 705
Thanked 15 Times in 14 Posts
awk to remove lines where field count is greather than 1 in two fields

I am trying to remove all the lines and spaces where the count in $4 or $5 is greater than 1 (more than 1 letter). The file and the output are tab-delimited. Thank you Linux.

file

Code:
X    5811530    .    G    C    NLGN4X
17    10544696    .    GA    G    MYH3
9    96439004    .    C    CCCTGCCTCCACCACA    PHF2
11    47463483    .    AG    A    RAPSN
15    58988898    .    A    C    AAAA
6    32006214    .    CCTG    C    CYP21A2

desired output

Code:
X    5811530    .    G    C    NLGN4X
15    58988898    .    A    C    AAAA

awk

Code:
awk -F'\t' -v OFS='\t' '(NR>1) && ($4 > 1 ) && ($5 > 1) ' file


Last edited by cmccabe; 01-09-2017 at 02:11 PM.. Reason: added details
Sponsored Links
    #2  
Old Unix and Linux 01-09-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 September 2017, 6:16 AM EDT
Location: San Jose, CA, USA
Posts: 10,507
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
Try:

Code:
awk -F'\t' 'length($4) == 1 && length($5) == 1' file

Note that:
  • the condition $4 > 1 looks for a numeric value at the start of the 4th field evaluating to a number greater than 1 no matter how many characters are in the 4th input field,
  • there is no need to set OFS if you only print copies of some of your input lines without changing the contents of any of the lines you print, and
  • there is no need to use the condition NR>1 when there is no header line to discard.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to count lines of CSV file where 2 fields match variables? nmoore2843 UNIX for Beginners Questions & Answers 4 07-07-2016 01:15 PM
Count of unique lines in field 4 cmccabe Shell Programming and Scripting 8 11-12-2015 12:46 PM
Remove zeros from first field, but print all fields Scottie1954 UNIX for Dummies Questions & Answers 9 05-14-2014 08:32 AM
Count Fields with Quoted Field ethanr100 UNIX for Dummies Questions & Answers 1 01-10-2012 01:29 AM
AWK : Add Fields of lines with matching field DerSeb Shell Programming and Scripting 6 01-16-2011 08:41 PM



All times are GMT -4. The time now is 07:35 AM.