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?
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