0
$\begingroup$

I have a file like this with 2 columns. I want to separate the columns based on plus and minus signs into separate files. How do I do this with awk or sed. Kindly help.

enter image description here

$\endgroup$

2 Answers 2

3
$\begingroup$

What do you want to do with NA values?

The below scripts ignore them (h3ab74's answer will print them to positiveExpression)

awk '$2 ~ /^[0-9]+$/  {print $0}' expressionFile.txt > positiveExpression.txt
awk '$2 ~ /^-[0-9]+$/ {print $0}' expressionFile.txt > negativeExpression.txt

These do a regex match to respectively, positive and negative numbers. If there's anything else in field 2 the line won't be printed.

$\endgroup$
1
$\begingroup$
awk '{if($2>0) print $0}' expressionFile.txt >> positiveExpression.txt
awk '{if($2<0) print $0}' expressionFile.txt >> negativeExpression.txt

Assuming that the file with gene expressions has the title: expressionFile.txt, this should do the trick.

Explanation of first line of code: if values in the 2nd column - $2 - are greater than 0, print all columns ($0) and write to a file (>>) titled, positiveExpression.txt. The use of awk '{}' is just syntactical.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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