0

If I have a simple text file, containing numbers of all negative value, how do I make all of those values positive?

So as an example, if test.txt has the following values:

-4
-5
-6

How do I make it so that the values are positive?

4
5
6
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • What if a line contains a positive number? Or zero? What if it contains something that's not a number? – ClickRick Dec 22 '16 at 22:52
  • @ClickRick - The file does not contain anything that is not a negative number. –  Dec 22 '16 at 22:55
  • 1
    Ed Morton's answer is the best since no computation is needed. – helloV Dec 22 '16 at 23:37
  • 2
    You're getting a lot of answers that use shell loops - see [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice) for some, but not all, of the reasons not to do that. – Ed Morton Dec 23 '16 at 00:05

5 Answers5

4
tr -d '-' < file

......................

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2

So one of the MATHs rule if we multiply -1 with any number it will be changed to positive number of it's own(in case it is already negative), so similarly we could do it here as follows.

awk '{$0=$0 * -1} 1' Input_file

Hope this helps you.

So let's say your Input_fie has negative and positives both the values as follows.

cat file1
-4
-5
-6
7

Then following code we could use to avoid making everything negative(specially those which values are already positive).

awk '{$0=$0<0?$0 * -1:$0} 1'  Input_file

Hope this helps you.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

If your intention is to convert to positive only when the number is negative:

while read -r number; do
   [[ number -lt 0 ]] && number=$((-1 * number))
   echo $number
done < test.txt
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    Using the shell's `while read` to loop over the lines in a file is frequently an antipattern. A simple Awk or Perl script would be my solution, and probably an order of magnitude faster. – tripleee Dec 23 '16 at 04:31
  • @tripleee: I agree with you. Thanks to SO, I am learning the intricacies of bash. I thought I was strong in bash. But after spending some quality time on this site, boy, I know how far I was from being strong! A number of years ago, I picked Perl over awk and for small and throw-away code, I tend to use pure bash. – codeforester Dec 23 '16 at 04:39
  • I've gravitated the other way, swinging back from Perl to Awk for small throw-away code (and generally now to Python for anything where I would still have used Perl). Anyway, welcome to the site, and feel free to check out the [bash chat room](http://chat.stackoverflow.com/rooms/98569/bin-bash) and/or the [Stack Overflow `bash` tag wiki](/tags/bash/info)! – tripleee Dec 23 '16 at 05:08
-1

It seems you don’t want to do any math, but simply remove all minus signs. In this case,

while read line; do echo ${line//-/}; done <test.txt

does the job without invoking external programs. It works also in the case that there is more than one number per line.

Dario
  • 2,673
  • 20
  • 24
-1

OK, I make the following assumptions:

  • you actually want the mathematical opposite of the number in the file
#!/bin/bash
while read a
do
    echo -1*$a | bc
done <test.txt
Matt
  • 1