0

I am trying to make my second column as non negative as some of the values are positive and some negative, so trying AWK function.

I have six columns in my bed file I am using the following command

awk -F"\t"  '{print $1"\t"$2<0?$2*-1:$2"\t"$3"\t"$4"\t"$5"\t"$6}' CTCF_first_coordinates > CTCF_first_coordinates_new

My problem is when I run this, then my first column is not printed and the values are not converted to non negative. However if i don't print my first column the code works.

Can anybody suggest how can I improve my code to get the proper output?

Below is the input bed file which is tab delimited and second column contain negative values together with positive values

chr18 576980 586980 CETN1 CLUL1 M1 
chr18 -8280 1720 NA USP14 M1

and the output i need is below where I want the negative value to be positive and rest everything remains same

chr18 576980 586980 CETN1 CLUL1 M1 
chr18 8280 1720 NA USP14 M1

In addition to the above problem I have some more issue where the output file as shown above the first coordinate is larger than the second coordinate. Is there any method to interchange the selected values of the two columns in the file i.e column 2 and 3. I have 192 such instances in the whole file. So my output should be something like

chr18 576980 586980 CETN1 CLUL1 M1 chr18 1720 8280 NA USP14 M1

3
  • 1
    please show some input file together with the desired output
    – fedorqui
    Apr 28, 2015 at 11:46
  • The example below is the input file chr17 78614606 78624606 B3GNTL1 METRNL M1 chr17 78364327 78374327 FN3K ZNF750 M1 chr17 78618175 78628175 B3GNTL1 METRNL M1 chr18 576980 586980 CETN1 CLUL1 M1 chr18 -8280 1720 NA USP14 M1 Apr 28, 2015 at 11:51
  • Please edit your question with such information. It is difficult to track files and code in comments.
    – fedorqui
    Apr 28, 2015 at 11:53

1 Answer 1

0

Just change the sign if it is negative:

$ awk '$2<0 {$2=-$2}1' file
chr18 576980 586980 CETN1 CLUL1 M1 
chr18 8280 1720 NA USP14 M1

The 1 at the end of the command is a way to set something to True in awk, so that it performs its default behaviour: print the current line.

1
  • @PrasoonAgarwal I guess it was a matter of parentheses, since '{print $1"\t"($2<0?$2*-1:$2)"\t"$3"\t"$4"\t"$5"\t"$6}' works. Note also that you can set the output field separator like -v OFS="\t" so that you don't have to hardcode it in the print expression.
    – fedorqui
    Apr 28, 2015 at 12:15

Your Answer

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

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