Skip to main content
deleted 8 characters in body
Source Link
h.j.k.
  • 1.3k
  • 1
  • 12
  • 24

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 11 M -2 4 -9 3.2 -5 -11 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. Apparently this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 11 M -2 4 -9 3.2 -5 -11 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. Apparently this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 M -2 4 -9 3.2 -5 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. Apparently this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

added 43 characters in body
Source Link
h.j.k.
  • 1.3k
  • 1
  • 12
  • 24

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 11 M -2 4 -9 3.2 -5 -11 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hopeThe only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. Apparently this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 11 M -2 4 -9 3.2 -5 -11 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 11 M -2 4 -9 3.2 -5 -11 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. Apparently this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

added 126 characters in body
Source Link
h.j.k.
  • 1.3k
  • 1
  • 12
  • 24

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=1;i<=NF-1;i+=2i=2;i<=NF;i+=2){printf "%4d""%4s",($i<0$(i-1)<0?-1:1)*$(i+1)*$i}print ""}' -1 2 3 4 -5 9  2 3.2 -4 5 -6 11 M -2 4 -9   3.2 -5 -11 
  • Loop from the firstsecond (i=1i=2) to the second-lastlast field (i<=NF-1).
  • Multiply the nextprevious field ($(i+1i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4d""%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=1;i<=NF-1;i+=2){printf "%4d",($i<0?-1:1)*$(i+1)}print ""}' -1 2 3 4 -5 9  2 3 -4 5 -6 11 M -2 4 -9   3 -5 -11 
  • Loop from the first (i=1) to the second-last field (i<=NF-1).
  • Multiply the next field ($(i+1)) with either -1 or 1.
  • Format the output nicely (printf "%4d"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. :)

A simple mathematics-based awk solution:

$ cat <<M | awk '{for(i=2;i<=NF;i+=2){printf "%4s",($(i-1)<0?-1:1)*$i}print ""}' -1 2 3 4 -5 9 2 3.2 -4 5 -6 11 M -2 4 -9 3.2 -5 -11 
  • Loop from the second (i=2) to the last field (i<=NF).
  • Multiply the previous field ($(i-1)) with either -1 or 1.
  • Format the output nicely (printf "%4s"), and print a trailing newline (print "").

The only caveat to this is that if you have an odd number of columns, the last field will not display anything at all. I hope this is what you expect. :)

(edited to work with decimal values, and to make the loop conditions more aligned with the question while saving 2 characters.)

Source Link
h.j.k.
  • 1.3k
  • 1
  • 12
  • 24
Loading