I have a tab separated file, which has 6 columns, but some rows are not so: enter image description here

There are many such lines in my file like the 7th. When I write

awk '{print $1"\t"$2"\t"$3"\t"$5"\t"$6}' filename

it still gets retained. How do I get rid of such entries ? Kindly help.

  • 5
    Please, don't post screenshots of text. They are often difficult to read, and contributors willing to help you will have to type-copy all example content when trying to reproduce your problem or improve your attempt. Instead, copy-and-paste the file with proper formatting. – AdminBee Jul 27 '20 at 10:10
  • 2
    You were asked not to post images when you posted your last question too. Please see How to Ask. – Ed Morton Jul 27 '20 at 19:30

The problem with your attempt is that you are simply printing the first 6 columns of every line of your input file to the standard output. If there are less columns in any given line, awk interprets the missing ones as "empty", so the output of your 7th line should actually be something like

chr1 \t 290338 \t 290378 \t NS500455:80:HG7TMBGXB:$:13404:14651:1128 \t + \t \n

(spaces around \t added for readability; note the "empty" column at the end).

In order to filter out malformed lines based on column count, you can try

awk -F'\t' -v OFS='\t' 'NF==6' file.txt > output.txt

It will only print those lines that have 6 fields (=columns).

  • can you kindly write the full code? – user1738234 Jul 27 '20 at 11:27
  • @user1738234 Actually that is the full code. There is only one condition, NF==6, in the awk program. If the condition is true and there are 6 columns, the line will be printed. If not, the it will be omitted from the output. Note, that this assumes that the malformed input lines really have a different number of column separators than required, and no "empty" columns (i.e. there must be no ... \t \t ... without anything in between the tabs). If that is the case, you may want to look at this question on how to identify empty fields. – AdminBee Jul 27 '20 at 11:37
  • no what I mean is : awk '{print $1"\t"$2"\t"$3"\t"$5"\t"$6}' | awk -F'\t' -v OFS='\t' 'NF==6' file.txt – user1738234 Jul 27 '20 at 11:48
  • From your question, I assumed that you have an input file as shown, where alle columns are tab-separated, and want to filter out all lines that don't have 6 columns, and that the awk '{print $1 ... $6}' filename was your attempt at doing so. If that is the case, you can replace that command by my proposed command. If something else is to be done, you need to edit your question to clarify that. – AdminBee Jul 27 '20 at 11:54
  • 1
    @user1738234 Look at how I wrote it: There are no curly braces. – AdminBee Jul 27 '20 at 12:20

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