2

I have a tab delimited file with column headers like ID, A, B, C, D as shown in the pic below, and row labels too like a, b, c, d, e show below in pic. How do I remove all the empty lines (rows) while preserving the row labels with non-empty rows? Example input and desired output is shown below. Thank you for any help!

Sample input:

ID A B C D a 1 2 b c 1 1 d e 1 

Desired output:

ID A B C D a 1 2 c 1 1 e 1 

[sample input and output transcribed from original image]

4
  • 2
    When you have text output, don't take a picture but copy paste the output in your POST Commented Oct 17, 2022 at 23:26
  • Thank you Gilles. I am sorry and won't repeat it in the future. I had difficulty in formatting tables so I posted a pic instead. Can you point me to how to format a table here? Commented Oct 17, 2022 at 23:32
  • Just select all off your code, then in the editor click {} Commented Oct 17, 2022 at 23:34
  • 1
    See "Code Blocks" at stackoverflow.com/help/formatting Commented Oct 18, 2022 at 0:08

2 Answers 2

7

What I suggest:

$ awk 'NF>1' file I A B C D a 1 2 c 1 1 e 1 
3
  • The nitpicker in me would recommend adding -F'\t' for clarity (although the default FS of course also includes TAB in "contiguous runs of whitespace"). Commented Oct 18, 2022 at 10:24
  • @AdminBee, well that might not work, actually. If the lines still have the tabs between each column (e.g. the third line would be b\t\t\t\t), awk -F '\t' would see the empty columns there and NF would be set accordingly. Then you'd need to loop over $2 to $NF to see if any of them is non-empty. Commented Oct 18, 2022 at 16:38
  • 1
    @ilkkachu True, it depends on the definitive meaning of "empty" line. It remains a task for the OP to clarify that here. Commented Oct 21, 2022 at 8:20
0

Using sed or grep

$ sed -En '/[^ \t]*[ \t]/p' input_file ID A B C D a 1 2 c 1 1 e 1 
$ grep -E '[^ \t]*[ \t]' input_file ID A B C D a 1 2 c 1 1 e 1 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.