-1

I have a csv file and I need to filter it out into two files based on whether the last column contains the word "ecDNA". I already have two more copies of the file to edit without changing the original file. Is there any way I can delete all the lines that do not contain "ecDNA" from one file and only retain lines that contain "ecDNA" from another copy of the file?

1
  • 4
    Please edit your question to include minimal working input file and how you are trying. – Prabhjot Singh Jun 15 at 5:08
0
awk -F, '$NF ~ /ecDNA/' oldfile > newfile

NF is the number of fields (columns) on the current input line, so $NF is the value (contents) of the last field. If $NF contains "ecDNA", then print the the line. Otherwise, ignore it.

If you need the match to be case-insensitive (and you're using GNU awk), use:

awk -F, -v IGNORECASE=1 '$NF ~ /ecDNA/' oldfile > newfile

For the inverted match (lines without ecDNA in the last field), negate the condition operator:

awk -F, '$NF !~ /ecDNA/' oldfile > newfile2
2
  • Thanks a lot, it worked! Sorry for not replying. – Michelle Yang Jun 25 at 19:18
  • @MichelleYang. If this worked, you may accept the answer. – Prabhjot Singh Jun 25 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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