Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

How to check for column content in Bash

I am stuck with this problem. Using Bash, we have to check if the .txt file presents data for two columns , and if not, annotations have to be emptied . Data is a txt file as follows :

#pacId  locusName   Best-hit-arabi-name arabi-defline
23158591    Lus10000002.g   AT1G75330.1 ornithine carbamoyltransferase
23170978    Lus10000003.g   AT1G14540.1 Peroxidase superfamily protein

I have to Empty annotations with no "Best-hit" & "arabi-defline" columns

I am thinking of doing a while script reading each line , but I don't know what would be the code to check if the columns are empty.

Thanks for helping me out !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have to Empty annotations with no "Best-hit" & "arabi-defline" columns

I'll assume that you mean:

I have to remove the line that doesn't contain value for the Best-hit and arabi-defline

So if it's the case, here a simple solution using awk:

awk '{if ($3 && $4){print $0}}' test.txt

I think awk is a better fit than bash in this case but you can also do it using bash with something like:

while read -r pacId locusName bHAN aD; do [[ $bHAN && $aD ]] && echo "${pacId} ${locusName} ${bHAN} ${aD}"; done < test.txt

Of course if you want to change the default separator by something else than any blank, you can just override the IFS like this:

while IFS='' read -r pacId locusName bHAN aD; do [[ $bHAN && $aD ]] && echo -e "${pacId}${locusName}${bHAN}${aD}"; done < test.txt

Same thing for awk, you'll just have to use -F to change the default separator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...