I've a tab-delimited file, e.g. myfile.tsv:
abc\tfoo
xyz\tbar
but sometimes, it has some blank columns, e.g.
abc\tfoo
xyz\tbar
what\t
\tthe
bleep\tsleep
i.e.
$ printf "abc\tfoo\n" > myfile.tsv
printf "xyz\tbar\n" >> myfile.tsv
printf "what\t\n" >> myfile.tsv
printf "\tthe\n" >> myfile.tsv
printf "bleep\tsleep\n" >> myfile.tsv
$ cat myfile.tsv
abc foo
xyz bar
what
the
bleep sleep
I could write a python script to remove the lines where the columns are empty, e.g.
with open('myfile.tsv') as fin:
for line in fin:
x, y = line.strip().split('\t')
x = x.strip()
y = y.strip()
if x and y:
print(line)
But how do I do the same with some unix shell commands, e.g. grep, sed, awk or something.
I've tried also something like this in grep:
grep -e ".\t." myfile.tsv
That seems to work but if the columns have spaces, it won't.
$ printf "abc\tfoo\n" > myfile.tsv
printf "xyz\tbar\n" >> myfile.tsv
printf "what\t \n" >> myfile.tsv
printf " \tthe\n" >> myfile.tsv
printf "bleep\tsleep\n" >> myfile.tsv
$ grep -e ".\t." myfile.tsv
abc foo
xyz bar
what
the
bleep sleep